Fix word-based selection in filter list editor/viewer

This commit fixes mouse double-click-and-drag operations,
which was broken due to the implementation of a custom
word selection in the filter list editor/viewer.
This commit is contained in:
Raymond Hill 2020-12-27 09:32:50 -05:00
parent f80371e844
commit 6d3ad553b4
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 49 additions and 46 deletions

View File

@ -336,12 +336,20 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
return 'comment'; return 'comment';
} }
if ( parser.category === parser.CATStaticExtFilter ) { if ( parser.category === parser.CATStaticExtFilter ) {
const style = colorExtSpan(stream); const style = colorExtSpan(stream) || '';
return style ? `ext ${style}` : 'ext'; let flavor = '';
if ( (parser.flavorBits & parser.BITFlavorExtCosmetic) !== 0 ) {
flavor = 'line-cm-ext-dom';
} else if ( (parser.flavorBits & parser.BITFlavorExtScriptlet) !== 0 ) {
flavor = 'line-cm-ext-js';
} else if ( (parser.flavorBits & parser.BITFlavorExtHTML) !== 0 ) {
flavor = 'line-cm-ext-html';
}
return `${flavor} ${style}`.trim();
} }
if ( parser.category === parser.CATStaticNetFilter ) { if ( parser.category === parser.CATStaticNetFilter ) {
const style = colorNetSpan(stream); const style = colorNetSpan(stream);
return style ? `net ${style}` : 'net'; return style ? `line-cm-net ${style}` : 'line-cm-net';
} }
stream.skipToEnd(); stream.skipToEnd();
return null; return null;
@ -679,25 +687,10 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
// Enhanced word selection // Enhanced word selection
{ {
const Pass = CodeMirror.Pass;
const selectWordAt = function(cm, pos) { const selectWordAt = function(cm, pos) {
const { line, ch } = pos; const { line, ch } = pos;
// Leave current selection alone
if ( cm.somethingSelected() ) {
const from = cm.getCursor('from');
const to = cm.getCursor('to');
if (
(line > from.line || line === from.line && ch > from.ch) &&
(line < to.line || line === to.line && ch < to.ch)
) {
return Pass;
}
}
const s = cm.getLine(line); const s = cm.getLine(line);
const token = cm.getTokenTypeAt(pos); const { type: token } = cm.getTokenAt(pos);
let beg, end; let beg, end;
// Select URL in comments // Select URL in comments
@ -712,17 +705,22 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
} }
} }
// Better word selection for cosmetic filters // Better word selection for extended filters: prefix
else if ( /\bext\b/.test(token) ) { else if (
if ( /\bvalue\b/.test(token) ) { /\bline-cm-ext-(?:dom|html|js)\b/.test(token) &&
/\bvalue\b/.test(token)
) {
const l = /[^,.]*$/i.exec(s.slice(0, ch)); const l = /[^,.]*$/i.exec(s.slice(0, ch));
const r = /^[^#,]*/i.exec(s.slice(ch)); const r = /^[^#,]*/i.exec(s.slice(ch));
if ( l && r ) { if ( l && r ) {
beg = l.index; beg = l.index;
end = ch + r[0].length; end = ch + r[0].length;
} }
} else if ( /\bvariable\b/.test(token) ) { }
const l = /[#.][a-z0-9_-]+$/i.exec(s.slice(0, ch));
// Better word selection for cosmetic and HTML filters: suffix
else if ( /\bline-cm-ext-(?:dom|html)\b/.test(token) ) {
const l = /[#.]?[a-z0-9_-]+$/i.exec(s.slice(0, ch));
const r = /^[a-z0-9_-]+/i.exec(s.slice(ch)); const r = /^[a-z0-9_-]+/i.exec(s.slice(ch));
if ( l && r ) { if ( l && r ) {
beg = l.index; beg = l.index;
@ -732,10 +730,9 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
} }
} }
} }
}
// Better word selection for network filters // Better word selection for network filters
else if ( /\bnet\b/.test(token) ) { else if ( /\bline-cm-net\b/.test(token) ) {
if ( /\bvalue\b/.test(token) ) { if ( /\bvalue\b/.test(token) ) {
const l = /[^ ,.=|]*$/i.exec(s.slice(0, ch)); const l = /[^ ,.=|]*$/i.exec(s.slice(0, ch));
const r = /^[^ #,|]*/i.exec(s.slice(ch)); const r = /^[^ #,|]*/i.exec(s.slice(ch));
@ -753,16 +750,22 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
} }
} }
if ( beg === undefined ) { return Pass; } if ( beg === undefined ) {
cm.setSelection( const { anchor, head } = cm.findWordAt(pos);
{ line, ch: beg }, return { from: anchor, to: head };
{ line, ch: end } }
);
return {
from: { line, ch: beg },
to: { line, ch: end },
};
}; };
CodeMirror.defineInitHook(cm => { CodeMirror.defineInitHook(cm => {
cm.addKeyMap({ cm.setOption('configureMouse', function(cm, repeat) {
'LeftDoubleClick': selectWordAt, return {
unit: repeat === 'double' ? selectWordAt : null,
};
}); });
}); });
} }