From eca8b359f23c1a970f36dd768564ec4fa686081a Mon Sep 17 00:00:00 2001 From: kotaindah55 Date: Fri, 7 Mar 2025 13:52:09 +0200 Subject: [PATCH] feat: cache last selected item when provided --- src/editor-mode/ui-components/ColorMenu.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/editor-mode/ui-components/ColorMenu.ts b/src/editor-mode/ui-components/ColorMenu.ts index 82872a9..72f7627 100644 --- a/src/editor-mode/ui-components/ColorMenu.ts +++ b/src/editor-mode/ui-components/ColorMenu.ts @@ -1,6 +1,6 @@ import { EditorView } from "@codemirror/view"; import { Menu } from "obsidian"; -import { ColorConfig, PlainRange, Token } from "src/types"; +import { ColorConfig, IndexCache, PlainRange, Token } from "src/types"; import { appFacet, settingsFacet } from "src/editor-mode/facets"; import { getActiveCanvasNodeCoords } from "src/editor-mode/utils"; @@ -8,6 +8,7 @@ export class ColorMenu extends Menu { openRange: PlainRange; tagRange: PlainRange; closeRange: PlainRange; + itemIndexCache: IndexCache; view: EditorView; private constructor(view: EditorView, openRange: PlainRange, tagRange: PlainRange, closeRange: PlainRange) { super(); @@ -16,6 +17,7 @@ export class ColorMenu extends Menu { this.tagRange = tagRange; this.closeRange = closeRange; this.dom.addClass("highlight-colors-modal"); + this.itemIndexCache = itemIndexCache; } get openLen() { return this.openRange.to - this.openRange.from; @@ -46,7 +48,11 @@ export class ColorMenu extends Menu { item.setTitle(title); item.setIcon(icon); item.dom.addClass(cls); - item.onClick(callback); + item.onClick(evt => { + let index = this.items.findIndex(i => i == item); + this.itemIndexCache.number = index; + callback(evt); + }); }); } showMenu() { @@ -114,6 +120,11 @@ export class ColorMenu extends Menu { (this.view as unknown as null) = null; super.onunload(); } + checkItemIndexCache() { + if (this.itemIndexCache.number >= this.items.length) { + this.itemIndexCache.number = this.items.length - 1; + } + } static create( view: EditorView, openRange: PlainRange,