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
client/text editor: improve the appearance of block borders
Adorian Doranf85ab6025c8e
diff --git a/apps/client/src/stylesheets/ckeditor-theme.css b/apps/client/src/stylesheets/ckeditor-theme.cssindex 5414ae923e..7884bceee5 100644--- a/apps/client/src/stylesheets/ckeditor-theme.css+++ b/apps/client/src/stylesheets/ckeditor-theme.css@@ -109,12 +109,25 @@ html:root {--ck-color-widget-type-around-button: var(--main-border-color);--ck-color-widget-type-around-button-hover: var(--main-border-color);+ --ck-color-widget-type-around-button-icon: var(--main-text-color);--ck-font-face: var(--main-font-family);--ck-input-label-height: 1.5em;--ck-mention-list-max-height: 500px;}+/* Redeclared per widget so the buttons follow a `--ck-color-focus-border` the widget sets. */+.ck-editor__editable.ck-focused .ck-widget.ck-widget_selected > .ck-widget__type-around {+ --ck-color-widget-type-around-button-active: var(--ck-color-focus-border);+ --ck-color-widget-type-around-button-icon: var(--main-background-color);+}++.ck .ck-widget > .ck-widget__type-around > .ck-widget__type-around__button:hover {+ box-shadow: 0 0 0 6px+ color-mix(in srgb, var(--ck-color-widget-type-around-button-active) 25%, transparent);+ animation: none;+}+div.ck.ck-dialog {--ck-color-dialog-background: var(--modal-background-color, var(--main-background-color));diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex e2b926ef1c..902c579c62 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1340,6 +1340,10 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-ttransition: background-color 200ms ease-out;}+.include-note > .ck-widget__selection-handle {+ margin-inline-start: var(--_include-note-top-border-radius, 8px);+}+/* On the wrapper: the editor gives the caption a border of its own for the focus ring. */.include-note-wrapper:has(+ figcaption) {border-bottom: 2px solid var(--main-background-color);@@ -1518,9 +1522,13 @@ figure.captioned-content-embed {overflow: auto;}- &:has(.include-note-content[hidden]) > .ck-widget__resize-handles- > .ck-widget__resize-handle:not(.ck-widget__resize-handle_width) {- display: none;+ &.include-note_collapsed {+ --ck-resize-corner-radius: var(--_include-note-bottom-border-radius, 8px);++ > .ck-widget__resize-handles+ > .ck-widget__resize-handle:not(.ck-widget__resize-handle_width) {+ display: none;+ }}}@@ -1565,6 +1573,8 @@ figure.captioned-content-embed {}body.mobile :is(.include-note, .link-embed)[data-position] {+ --ck-resize-corner-radius: var(--_include-note-bottom-border-radius, 8px);+float: none;clear: both;margin-inline: 0;@@ -1887,8 +1897,13 @@ pre.code-embed-editor > .cm-editor:not(.cm-focused) {/* Outlined as a selected widget in the editor. */.include-note:has(> .include-note-wrapper > .include-note-body.active) {- /* The editor shows a selected widget as blurred while the focus is in its content. */+ /* With the focus in its content, the widget is either selected in a blurred editor or not+ selected at all. */--ck-color-widget-blurred-border: var(--ck-color-focus-border);+ --ck-color-widget-type-around-button-hover: var(--ck-color-focus-border);+ --ck-color-widget-type-around-button-active: var(--ck-color-focus-border);+ --ck-color-widget-type-around-button-blurred-editable: var(--ck-color-focus-border);+ --ck-color-widget-type-around-button-icon: var(--main-background-color);outline: var(--ck-widget-outline-thickness, 3px) solid var(--ck-color-focus-border);}diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsxindex 35d9dcfcfc..92d475715f 100644--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.spec.tsx@@ -177,13 +177,14 @@ const EDITABLE_TOOLS = {*/function placeInHostEditor() {const selectEmbedAt = vi.fn((_element: Element) => true);+ const setEmbedCollapsedAt = vi.fn((_element: Element, _isCollapsed: boolean) => {});const execute = vi.fn();const focus = vi.fn();const editable = document.createElement("div");editable.className = "ck-editor__editable";Object.assign(editable, {ckeditorInstance: {- plugins: { get: () => ({ selectEmbedAt }) },+ plugins: { get: () => ({ selectEmbedAt, setEmbedCollapsedAt }) },execute,editing: { view: { focus } }}@@ -195,7 +196,7 @@ function placeInHostEditor() {document.body.append(editable);onTestFinished(() => editable.remove());- return { editor: { selectEmbedAt, execute, focus }, embed };+ return { editor: { selectEmbedAt, setEmbedCollapsedAt, execute, focus }, embed };}/** Tools of content with display modes, which the title row of its embed switches. */@@ -387,6 +388,25 @@ describe("ContentEmbed", () => {expect(contentBox().hidden).toBe(true);});+ it("marks a collapsed expandable box in the text editor until it opens or leaves", () => {+ const { editor } = placeInHostEditor();+ const marks = () => editor.setEmbedCollapsedAt.mock.calls+ .map(([ target, isCollapsed ]) => [ target === contentBox(), isCollapsed ]);+ renderBox({ boxSize: "full" });+ expect(marks()).toEqual([]);++ act(() => render(null, container));+ renderBox({ boxSize: "expandable" });+ expect(marks()).toEqual([ [ true, true ] ]);+ click(button("include-note-toggle"));+ expect(marks()).toEqual([ [ true, true ], [ true, false ] ]);+ click(button("include-note-toggle"));+ const box = contentBox();+ act(() => render(null, container));+ expect(editor.setEmbedCollapsedAt).toHaveBeenLastCalledWith(box, false);+ expect(editor.setEmbedCollapsedAt).toHaveBeenCalledTimes(4);+ });+it("opens the note in a new tab, and its context menu, from the title row", () => {renderBox({ notePath: "owner", viewScope: ATTACHMENT_SCOPE });const open = button("include-note-open");diff --git a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsxindex f33ecd7351..051a9ebd5e 100644--- a/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx+++ b/apps/client/src/widgets/type_widgets/text/ContentEmbed.tsx@@ -88,6 +88,7 @@ export default function ContentEmbed({const isOpen = isExpanded || isRevealed;const focusContentAfterRender = useFocusAfterRender(contentRef);const isExpandable = boxSize === "expandable" && !!title && !!notePath;+ const isCollapsed = isExpandable && !isOpen;const hasBackdrop = !isContentActive && !isDraggedOver&& !DIRECT_CONTENT_TYPES.has(contentType);const hasFullscreen = !isExcerpt && !OWN_FULLSCREEN_CONTENT_TYPES.has(contentType)@@ -112,6 +113,16 @@ export default function ContentEmbed({return registerEmbedFindTarget(content, new HtmlFindTarget(content));}, [ content, contentType ]);+ // The stylesheet hides the corner resize handle of an embed that the text editor marks.+ useEffect(() => {+ const box = contentRef.current;+ if (!isCollapsed || !box) return;++ const plugin = findTextEditorAround(box)?.plugins.get("ContentEmbed");+ plugin?.setEmbedCollapsedAt(box, true);+ return () => plugin?.setEmbedCollapsedAt(box, false);+ }, [ isCollapsed ]);+useLayoutEffect(() => {// The box mounts after an upload, by which time the focus can have left the editor.const editable = contentRef.current?.closest(".ck-editor__editable");diff --git a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css b/apps/client/src/widgets/type_widgets/text/LinkEmbed.cssindex d4acb8bb3f..f5daa622a8 100644--- a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css+++ b/apps/client/src/widgets/type_widgets/text/LinkEmbed.css@@ -110,6 +110,10 @@ body.light-theme .link-embed-favicon-light {--ck-color-focus-border: var(--link-color);}+.ck-widget.link-embed > .ck-widget__selection-handle {+ margin-inline-start: var(--_link-embed-top-border-radius, 8px);+}+.link-embed-preview-wrapper {display: block;}diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.tsindex 6767bd346e..72f1b7e90c 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts@@ -759,6 +759,32 @@ describe("ContentEmbed", () => {.toBe("noteMenu");});+ it("marks the embed that a DOM element is part of as collapsed, until destroyed", async () => {+ editor.setData("<p>foo</p>"+ + "<figure class=\"include-note\" data-note-id=\"noteC\" data-box-size=\"expandable\">"+ + "</figure>");+ const domRoot = editor.editing.view.getDomRoot();+ const figure = domRoot?.querySelector("figure.include-note");+ const wrapper = figure?.querySelector("div.include-note-wrapper");+ const paragraph = domRoot?.querySelector("p");+ if (!figure || !wrapper || !paragraph) {+ throw new Error("Expected a rendered embed and paragraph.");+ }+ const plugin = editor.plugins.get("ContentEmbed");+ const isCollapsed = () => figure.classList.contains("include-note_collapsed");++ plugin.setEmbedCollapsedAt(wrapper, true);+ expect(isCollapsed()).toBe(true);+ plugin.setEmbedCollapsedAt(paragraph, false);+ expect(isCollapsed()).toBe(true);+ plugin.setEmbedCollapsedAt(wrapper, false);+ expect(isCollapsed()).toBe(false);++ await editor.destroy();+ expect(() => plugin.setEmbedCollapsedAt(wrapper, true)).not.toThrow();+ expect(isCollapsed()).toBe(false);+ });+it("deletes the selected embed, with the caret in the block after it, else before it", () => {const embed = "<contentEmbed noteId=\"n1\"></contentEmbed>";const command = editor.commands.get(DELETE_COMMAND_NAME);diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.tsindex b056f3c91a..00a35966c5 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts@@ -120,6 +120,9 @@ const RESIZABLE_BOX_SIZES: unknown[] = [ 'small', 'medium', 'expandable' ];/** The least size that an embed can be resized to, in `em`. */export const MIN_EMBED_SIZE = { width: 10, height: 3 };+/** The class of an Expandable embed that shows only its title row. */+const COLLAPSED_EMBED_CLASS = 'include-note_collapsed';+/** The flags of an embed, each saved as `"true"` in a `data-*` attribute while it is on. */const FLAG_ATTRIBUTES = [[ 'hideTitle', 'data-hide-title' ],@@ -299,6 +302,27 @@ export default class ContentEmbed extends Plugin {};}+ /**+ * Marks the embed that `domElement` is part of as collapsed while `isCollapsed` is `true`, as+ * an Expandable embed that shows only its title row.+ */+ setEmbedCollapsedAt( domElement: Element, isCollapsed: boolean ) {+ const editor = this.editor;+ const embed = editor.state === 'ready' ? getContentEmbedAt( editor, domElement ) : null;+ const view = embed && editor.editing.mapper.toViewElement( embed );+ if ( !view ) {+ return;+ }++ editor.editing.view.change( writer => {+ if ( isCollapsed ) {+ writer.addClass( COLLAPSED_EMBED_CLASS, view );+ } else {+ writer.removeClass( COLLAPSED_EMBED_CLASS, view );+ }+ } );+ }+/** Whether `domElement` is a reference link that `convertLinkToEmbed` turns into an embed. */canConvertLinkToEmbed( domElement: HTMLElement ): boolean {return !!getLinkToEmbed( this.editor, domElement );diff --git a/packages/ckeditor5/src/theme/code_block_insert_paragraph.css b/packages/ckeditor5/src/theme/code_block_insert_paragraph.cssindex 8673a22466..975f741378 100644--- a/packages/ckeditor5/src/theme/code_block_insert_paragraph.css+++ b/packages/ckeditor5/src/theme/code_block_insert_paragraph.css@@ -5,8 +5,9 @@* global mousedown handler claims every `.ck-widget__type-around__button` and would crash trying* to resolve a widget from our non-widget host. So we use our own classes, but avoid duplicating* any *values*: every size/colour/timing below comes from ckeditor5-widget's own design tokens- * (the `--ck-widget-type-around-*` / `--ck-color-widget-*` custom properties) and its sonar- * `@keyframes`, so the buttons match the real widget arrows and track the editor theme.+ * (the `--ck-widget-type-around-*` / `--ck-color-widget-*` custom properties), so the buttons+ * match the real widget arrows and track the editor theme. The hover ring matches the widget+ * buttons' rule in apps/client/src/stylesheets/ckeditor-theme.css.** Source of truth: @ckeditor/ckeditor5-widget/dist/index.css (.ck-widget__type-around__*).*/@@ -68,15 +69,16 @@stroke-dasharray: 10;}-/* Reveal while hovering the code block; active colour + sonar pulse on the button itself. */+/* Reveal while hovering the code block; active colour + hover ring on the button itself. */.ck.ck-editor__editable pre:hover .ck-code-block__type-around__button {opacity: 1;pointer-events: auto;}.ck.ck-editor__editable .ck-code-block__type-around__button:hover {+ box-shadow: 0 0 0 6px+ color-mix(in srgb, var(--ck-color-widget-type-around-button-active) 25%, transparent);background: var(--ck-color-widget-type-around-button-active);- animation: 1s infinite ck-widget-type-around-button-sonar;}/* Glossy highlight overlay on hover, matching the widget. */@@ -96,10 +98,6 @@.ck.ck-editor__editable .ck-code-block__type-around__button {transition: none;}-- .ck.ck-editor__editable .ck-code-block__type-around__button:hover {- animation: none;- }}/* In read-only mode the paragraph can't be inserted, so don't offer the affordance. */diff --git a/packages/ckeditor5/src/theme/multicolumn.css b/packages/ckeditor5/src/theme/multicolumn.cssindex 4a91fd76ae..257335285b 100644--- a/packages/ckeditor5/src/theme/multicolumn.css+++ b/packages/ckeditor5/src/theme/multicolumn.css@@ -1,5 +1,7 @@:where(:root) {--multicolumn-layout-focus-border-color: var(--link-color, var(--ck-color-focus-border));+ --multicolumn-layout-focus-button-icon-color: var(--main-background-color,+ var(--ck-color-base-background));--multicolumn-layout-stacked-background: var(--accented-background-color,var(--ck-color-base-foreground));--multicolumn-layout-border-radius: 8px;@@ -50,9 +52,22 @@column-gap: 12px;border-radius: var(--multicolumn-layout-border-radius);+ & > .ck-widget__selection-handle {+ margin-inline-start: var(--multicolumn-layout-border-radius);+ }+&.trilium-multicolumn-layout--focused {outline: var(--ck-widget-outline-thickness, 3px) solidvar(--multicolumn-layout-focus-border-color);++ & > .ck-widget__type-around {+ --ck-color-widget-type-around-button-hover:+ var(--multicolumn-layout-focus-border-color);+ --ck-color-widget-type-around-button-active:+ var(--multicolumn-layout-focus-border-color);+ --ck-color-widget-type-around-button-icon:+ var(--multicolumn-layout-focus-button-icon-color);+ }}& > .ck-editor__nested-editable {diff --git a/packages/ckeditor5/src/theme/resizable_widgets.css b/packages/ckeditor5/src/theme/resizable_widgets.cssindex 7eebd8a12e..465c9910fa 100644--- a/packages/ckeditor5/src/theme/resizable_widgets.css+++ b/packages/ckeditor5/src/theme/resizable_widgets.css@@ -1,7 +1,8 @@:where(:root) {--ck-resize-handle-color: var(--ck-color-focus-border);+ --ck-resize-handle-outline-color: var(--main-border-color, var(--ck-color-base-border));--ck-resize-handle-hit-size: 12px;- --ck-resize-handle-grip-size: 8px;+ --ck-resize-handle-grip-size: 10px;--ck-resize-handle-grip-length: 32px;--ck-resize-handle-grip-thickness: 4px;}@@ -13,6 +14,9 @@}.ck .ck-widget > .ck-widget__resize-handles {+ /* Redeclared so the handles follow a `--ck-color-focus-border` the widget sets. */+ --ck-resize-handle-color: var(--ck-color-focus-border);+display: none;position: absolute;inset: 0;@@ -20,11 +24,24 @@}/* A widget stays selected while the editor is blurred. */-.ck.ck-editor__editable.ck-focused .ck-widget_resizable.ck-widget_selected,+.ck.ck-editor__editable.ck-focused:not(.ck-read-only) .ck-widget_resizable.ck-widget_selected,.ck .ck-widget_resizable.ck-widget_resizing {> .ck-widget__resize-handles {display: block;}++ /* A widget that hides its corner handle sets `--ck-resize-corner-radius` to its radius. */+ &:not(.ck-widget_resize-anchor-left, .ck-widget_resize-anchor-right) {+ border-end-end-radius: var(--ck-resize-corner-radius, 0px);+ }++ &.ck-widget_resize-anchor-left {+ border-bottom-right-radius: var(--ck-resize-corner-radius, 0px);+ }++ &.ck-widget_resize-anchor-right {+ border-bottom-left-radius: var(--ck-resize-corner-radius, 0px);+ }}.ck.ck-editor__editable.ck-read-only .ck-widget > .ck-widget__resize-handles {@@ -97,10 +114,17 @@&::after {content: "";+ outline: 2px solid var(--ck-resize-handle-outline-color);border-radius: 50%;width: var(--ck-resize-handle-grip-size);height: var(--ck-resize-handle-grip-size);}++ &:hover::after {+ box-shadow: 0 0 0 6px+ color-mix(in srgb, var(--ck-resize-handle-color) 25%, transparent);+ outline-color: transparent;+ }}}diff --git a/packages/ckeditor5/src/theme/tabs.css b/packages/ckeditor5/src/theme/tabs.cssindex ccf5a46295..3585150cc9 100644--- a/packages/ckeditor5/src/theme/tabs.css+++ b/packages/ckeditor5/src/theme/tabs.css@@ -1,5 +1,6 @@:where(:root) {--tabs-focus-border-color: var(--link-color, var(--ck-color-focus-border));+ --tabs-focus-button-icon-color: var(--main-background-color, var(--ck-color-base-background));}/* Saved HTML without a marked tab, such as a preview or a printout, reads as a sequence of titled@@ -149,8 +150,18 @@}}+.ck-editor__editable .trilium-tabs.ck-widget > .ck-widget__selection-handle {+ margin-inline-start: var(--_tabs-border-radius, 0px);+}+.ck-editor__editable .trilium-tabs.ck-widget.trilium-tabs--focused {outline: var(--ck-widget-outline-thickness, 3px) solid var(--tabs-focus-border-color);++ & > .ck-widget__type-around {+ --ck-color-widget-type-around-button-hover: var(--tabs-focus-border-color);+ --ck-color-widget-type-around-button-active: var(--tabs-focus-border-color);+ --ck-color-widget-type-around-button-icon: var(--tabs-focus-button-icon-color);+ }}.ck-editor__editable .trilium-tabs > .trilium-tab > .trilium-tab-panel {