Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(ckeditor): simplify inline code toolbar significantly
Elian Doran7a45c43e02f6
diff --git a/packages/ckeditor5/src/plugins/inline_code_toolbar.ts b/packages/ckeditor5/src/plugins/inline_code_toolbar.tsindex 9cee0b783f..ac76a307fc 100644--- a/packages/ckeditor5/src/plugins/inline_code_toolbar.ts+++ b/packages/ckeditor5/src/plugins/inline_code_toolbar.ts@@ -1,6 +1,5 @@-import { BalloonPanelView, ButtonView, Plugin, ToolbarView } from "ckeditor5";+import { Plugin, ViewDocumentFragment, WidgetToolbarRepository, type ViewNode } from "ckeditor5";import CopyToClipboardButton from "./copy_to_clipboard_button";-import copyIcon from "../icons/copy.svg?raw";/*** Shows a small toolbar with a copy button when the cursor is on inline code.@@ -8,116 +7,33 @@ import copyIcon from "../icons/copy.svg?raw";export default class InlineCodeToolbar extends Plugin {static get requires() {- return [CopyToClipboardButton] as const;+ return [WidgetToolbarRepository, CopyToClipboardButton] as const;}- private balloon?: BalloonPanelView;- private toolbar?: ToolbarView;-- init() {+ afterInit() {const editor = this.editor;-- // Create toolbar with copy button- this.toolbar = new ToolbarView(editor.locale);- const copyButton = new ButtonView(editor.locale);- copyButton.set({- icon: copyIcon,- tooltip: "Copy to clipboard"- });- copyButton.on("execute", () => {- editor.execute("copyToClipboard");- this.hideToolbar();- });- this.toolbar.items.add(copyButton);-- // Create balloon panel- this.balloon = new BalloonPanelView(editor.locale);- this.balloon.content.add(this.toolbar);- this.balloon.class = "ck-toolbar-container";-- editor.ui.view.body.add(this.balloon);-- // Show/hide after the UI has fully updated (selection, commands, DOM- // are all settled), so the code command's value and native selection- // are both current.- this.listenTo(editor.ui, "update", () => {- this.updateToolbarVisibility();- });-- // Hide on editor blur- this.listenTo(editor.ui.focusTracker, "change:isFocused", (_evt, _name, isFocused) => {- if (!isFocused) {- this.hideToolbar();+ const widgetToolbarRepository = editor.plugins.get(WidgetToolbarRepository);++ widgetToolbarRepository.register("inlineCode", {+ items: ["copyToClipboard"],+ balloonClassName: "ck-toolbar-container",+ getRelatedElement(selection) {+ const selectionPosition = selection.getFirstPosition();+ if (!selectionPosition) {+ return null;+ }++ let parent: ViewNode | ViewDocumentFragment | null = selectionPosition.parent;+ while (parent) {+ if (parent.is("attributeElement", "code")) {+ return parent;+ }+ parent = parent.parent;+ }++ return null;}});}- private updateToolbarVisibility() {- const editor = this.editor;- const position = editor.model.document.selection.getFirstPosition();-- // Don't show for code blocks (they have their own toolbar)- if (position?.findAncestor("codeBlock")) {- this.hideToolbar();- return;- }-- // Use the code command's value — it reliably reflects whether the- // cursor is inside inline code, including at boundary positions.- const codeCommand = editor.commands.get("code");- if (codeCommand?.value) {- this.showToolbar();- } else {- this.hideToolbar();- }- }-- private showToolbar() {- if (!this.balloon) return;-- // Find the <code> DOM element from the native selection- const domSelection = window.getSelection();- const anchorNode = domSelection?.anchorNode;- if (!anchorNode) {- this.hideToolbar();- return;- }-- const domElement = (anchorNode instanceof HTMLElement ? anchorNode : anchorNode.parentElement)?.closest("code");- if (!domElement) {- this.hideToolbar();- return;- }-- const rect = domElement.getBoundingClientRect();- this.balloon.pin({- target: {- top: rect.top,- bottom: rect.bottom,- left: rect.left,- right: rect.right,- width: rect.width,- height: rect.height- }- });- this.balloon.isVisible = true;- }-- private hideToolbar() {- if (this.balloon) {- this.balloon.isVisible = false;- this.balloon.unpin();- }- }-- override destroy() {- super.destroy();- if (this.balloon) {- this.balloon.destroy();- }- if (this.toolbar) {- this.toolbar.destroy();- }- }-}