implement new highlight remove feature

This commit is contained in:
Xen 2020-05-26 21:12:31 +08:00
parent d96cd46203
commit c8e1007f59
1 changed files with 24 additions and 4 deletions

View File

@ -84,7 +84,7 @@ function decoApply(model, text) {
} }
} }
function decoRemove(model, text) { function decoRemoveOld(model, text) {
let matches = model.findMatches( let matches = model.findMatches(
text, text,
false, false,
@ -105,6 +105,16 @@ function decoRemove(model, text) {
} }
} }
function decoRemove(model, targetClassName) {
let decos = model.getAllDecorations();
for (let deco of decos) {
if (targetClassName === deco.options.className) {
model.deltaDecorations([deco.id], []);
}
}
}
function highlightToggle() { function highlightToggle() {
console.log("highligh toggle"); console.log("highligh toggle");
let model = editor.getModel(); let model = editor.getModel();
@ -123,6 +133,8 @@ function highlightToggle() {
if (text === "") return; if (text === "") return;
let applyDeco = 1; let applyDeco = 1;
let targetClassName = "";
let targetzIndex = 0;
let decos = model.getDecorationsInRange(range); let decos = model.getDecorationsInRange(range);
for (let deco of decos) { for (let deco of decos) {
if ( if (
@ -130,13 +142,21 @@ function highlightToggle() {
deco.options.className.indexOf("hl-") !== -1 deco.options.className.indexOf("hl-") !== -1
) { ) {
applyDeco = 0; applyDeco = 0;
break; if (targetzIndex === 0) {
targetzIndex = deco.options.zIndex;
targetClassName = deco.options.className;
} else {
if (deco.options.zIndex > targetzIndex) {
targetzIndex = deco.options.zIndex;
targetClassName = deco.options.className;
}
}
} }
} }
if (1 === applyDeco) { if (1 === applyDeco) {
decoApply(model, text); decoApply(model, text);
} else { } else {
decoRemove(model, text); decoRemove(model, targetClassName);
} }
return null; return null;
@ -488,7 +508,7 @@ amdRequire(["vs/editor/editor.main"], function () {
id: "highlight-clear-all", id: "highlight-clear-all",
label: "Highlight Clear All", label: "Highlight Clear All",
keybindings: [ keybindings: [
monaco.KeyMod.CtrlCmd + monaco.KeyMod.Shift + monaco.KeyCode.KEY_E, monaco.KeyMod.CtrlCmd + monaco.KeyMod.Shift + monaco.KeyCode.KEY_X,
], ],
precondition: null, precondition: null,
keybindingContext: null, keybindingContext: null,