perf: ColorButton no longer held token ranges information

All of this can be done with Formatter instance through menu item onClick event.
This commit is contained in:
kotaindah55 2025-03-17 10:38:26 +02:00
parent a33f9f83e1
commit cf701cad4d

View file

@ -1,30 +1,24 @@
import { WidgetType, EditorView, Decoration } from "@codemirror/view"
import { Menu } from "obsidian";
import { ColorMenu } from "src/editor-mode/ui-components";
import { PlainRange, Token } from "src/types";
import { TagMenu } from "src/editor-mode/ui-components";
import { Format } from "src/enums";
import { Token } from "src/types";
/**
* These code snippets are taken from
* https://github.com/Superschnizel/obisdian-fast-text-color/blob/master/src/widgets/ColorWidget.ts
* with some modifications.
*/
*/
export class ColorButton extends WidgetType {
menu: Menu;
colorMenu: ColorMenu;
tagRange: PlainRange;
openRange: PlainRange;
closeRange: PlainRange;
btnPos: number;
colorMenu: TagMenu;
constructor(token: Token) {
super();
this.openRange = { from: token.from, to: token.from + token.openLen };
this.tagRange = { from: this.openRange.to, to: this.openRange.to + token.tagLen };
this.closeRange = { from: token.to - token.closeLen, to: token.to };
this.btnPos = token.from + token.openLen;
}
eq(other: ColorButton) {
return (
other.tagRange.from == this.tagRange.from &&
other.tagRange.to == this.tagRange.to
);
return this.btnPos == other.btnPos;
}
toDOM(view: EditorView): HTMLElement {
let btn = document.createElement("span");
@ -32,12 +26,9 @@ export class ColorButton extends WidgetType {
btn.className = "cm-highlight-color-btn";
btn.onclick = () => {
view.dispatch({
selection: {
anchor: this.tagRange.from,
head: this.tagRange.to
}
selection: { anchor: this.btnPos }
});
this.colorMenu = ColorMenu.create(view, this.openRange, this.tagRange, this.closeRange, true);
this.colorMenu ||= TagMenu.create(view, Format.HIGHLIGHT);
this.colorMenu.showMenu();
}
return btn;