From 7d3e6591c26815482d5fcad28093af4a8e013bb9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 19 Jul 2026 08:56:35 -0400 Subject: [PATCH] feat: Add onNotice callback to decouple shared editor from toast library (#13046) * feat: Add onNotice callback to decouple shared editor from toast library Shared editor code called sonner's toast directly. Introduce an onNotice callback (defaulting to a toast) so the shared editor stays agnostic of the host app's notification library. * Add lint rule to prevent regression --------- Co-authored-by: Claude Opus 4.8 --- .oxlintrc.json | 26 +++++++++++++++++++ app/components/Editor.tsx | 2 ++ app/editor/components/SuggestionsMenu.tsx | 2 ++ app/editor/index.tsx | 8 ++++++ app/editor/toastNotice.ts | 25 ++++++++++++++++++ shared/editor/commands/insertFiles.ts | 9 ++++--- shared/editor/commands/link.ts | 20 +++++++------- shared/editor/components/ColorSwatch.tsx | 10 ++++--- .../editor/extensions/ColorSwatchPreview.ts | 1 + shared/editor/extensions/Mermaid.ts | 8 +++--- shared/editor/marks/Link.tsx | 14 +++++++--- shared/editor/nodes/Attachment.tsx | 2 ++ shared/editor/nodes/CodeFence.ts | 5 ++-- shared/editor/nodes/Heading.ts | 8 +++--- shared/editor/nodes/SimpleImage.tsx | 2 ++ shared/editor/types/index.ts | 10 +++++++ 16 files changed, 123 insertions(+), 29 deletions(-) create mode 100644 app/editor/toastNotice.ts diff --git a/.oxlintrc.json b/.oxlintrc.json index 1fb98f54a6..b2d659fa76 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -123,6 +123,32 @@ "react/rules-of-hooks": "error" }, "plugins": ["eslint", "oxc", "react", "typescript", "import"] + }, + { + "files": ["shared/**/*.{js,jsx,ts,tsx}"], + "rules": { + "no-restricted-imports": [ + "error", + { + "paths": [ + { + "name": "prosemirror-tables", + "importNames": [ + "addRowBefore", + "addRowAfter", + "addColumnBefore", + "addColumnAfter" + ], + "message": "Use the wrappers from shared/editor/commands/table instead, which respect the target index and place the cursor in the inserted cell." + }, + { + "name": "sonner", + "message": "Do not import a toast library within shared; surface notifications through the onNotice callback so shared code stays decoupled from the app's toast implementation." + } + ] + } + ] + } } ] } diff --git a/app/components/Editor.tsx b/app/components/Editor.tsx index c1ed16153d..61cec7ed26 100644 --- a/app/components/Editor.tsx +++ b/app/components/Editor.tsx @@ -16,6 +16,7 @@ import { AttachmentValidation } from "@shared/validations"; import ClickablePadding from "~/components/ClickablePadding"; import ErrorBoundary from "~/components/ErrorBoundary"; import type { Props as EditorProps, Editor as SharedEditor } from "~/editor"; +import { toastNotice } from "~/editor/toastNotice"; import useCurrentUser from "~/hooks/useCurrentUser"; import useEditorClickHandlers from "~/hooks/useEditorClickHandlers"; import useEmbeds from "~/hooks/useEmbeds"; @@ -178,6 +179,7 @@ function Editor(props: Props, ref: React.RefObject | null) { onFileUploadStart: handleFileUploadStart, onFileUploadStop: handleFileUploadStop, onFileUploadProgress: handleFileUploadProgress, + onNotice: toastNotice, isAttachment, }); }, diff --git a/app/editor/components/SuggestionsMenu.tsx b/app/editor/components/SuggestionsMenu.tsx index ccf148d15e..9f36529462 100644 --- a/app/editor/components/SuggestionsMenu.tsx +++ b/app/editor/components/SuggestionsMenu.tsx @@ -12,6 +12,7 @@ import { EmbedDescriptor } from "@shared/editor/embeds"; import filterExcessSeparators from "@shared/editor/lib/filterExcessSeparators"; import { findParentNode } from "@shared/editor/queries/findParentNode"; import type { MenuItem } from "@shared/editor/types"; +import { toastNotice } from "~/editor/toastNotice"; import { s } from "@shared/styles"; import { getEventFiles } from "@shared/utils/files"; import { AttachmentValidation } from "@shared/validations"; @@ -501,6 +502,7 @@ function SuggestionsMenu(props: Props) { onFileUploadStart, onFileUploadStop, onFileUploadProgress, + onNotice: toastNotice, isAttachment: inputRef.current?.accept === "*", attrs, }); diff --git a/app/editor/index.tsx b/app/editor/index.tsx index eda88f6100..79e43e3d49 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -38,6 +38,7 @@ import { basicExtensions as extensions } from "@shared/editor/nodes"; import type ReactNode from "@shared/editor/nodes/ReactNode"; import type { ComponentProps, + EditorNotice, SelectionToolbarMenuDescriptor, } from "@shared/editor/types"; import type { @@ -63,6 +64,7 @@ import type { LightboxImage } from "@shared/editor/lib/Lightbox"; import { LightboxImageFactory } from "@shared/editor/lib/Lightbox"; import Lightbox from "~/components/Lightbox"; import { anchorPlugin } from "@shared/editor/plugins/AnchorPlugin"; +import { toastNotice } from "./toastNotice"; export type Props = { /** An optional identifier for the editor context. It is used to persist local settings */ @@ -161,6 +163,11 @@ export type Props = { ) => void; /** Callback when user presses any key with document focused */ onKeyDown?: (event: React.KeyboardEvent) => void; + /** + * Callback used to surface a short notice to the user. Defaults to rendering + * a toast so that shared editor code stays agnostic of the toast library. + */ + onNotice?: EditorNotice; /** Collection of embed types to render in the document */ embeds: EmbedDescriptor[]; /** Display preferences for the logged in user, if any. */ @@ -206,6 +213,7 @@ export class Editor extends React.PureComponent< onFileUploadStop: () => { // no default behavior }, + onNotice: toastNotice, embeds: [], extensions, }; diff --git a/app/editor/toastNotice.ts b/app/editor/toastNotice.ts new file mode 100644 index 0000000000..df4811ae75 --- /dev/null +++ b/app/editor/toastNotice.ts @@ -0,0 +1,25 @@ +import { toast } from "sonner"; +import type { EditorNotice } from "@shared/editor/types"; + +/** + * The default editor notice handler, rendering a toast. This keeps knowledge of + * the toast library at the application layer, out of shared editor code. + * + * @param message - the message to display. + * @param type - the severity of the notice. + */ +export const toastNotice: EditorNotice = (message, type = "info") => { + switch (type) { + case "error": + toast.error(message); + return; + case "success": + toast.success(message); + return; + case "warning": + toast.warning(message); + return; + default: + toast.message(message); + } +}; diff --git a/shared/editor/commands/insertFiles.ts b/shared/editor/commands/insertFiles.ts index 354d44db33..c472017262 100644 --- a/shared/editor/commands/insertFiles.ts +++ b/shared/editor/commands/insertFiles.ts @@ -1,7 +1,7 @@ import { t } from "i18next"; import { v4 as uuidv4 } from "uuid"; import type { EditorView } from "prosemirror-view"; -import { toast } from "sonner"; +import type { EditorNotice } from "../types"; import FileHelper from "../lib/FileHelper"; import uploadPlaceholderPlugin, { findPlaceholder, @@ -26,6 +26,8 @@ export type Options = { onFileUploadStop?: () => void; /** Callback fired when file upload progress changes */ onFileUploadProgress?: (id: string, fractionComplete: number) => void; + /** Callback fired to surface a notice to the user */ + onNotice?: EditorNotice; /** Attributes to overwrite */ attrs?: { /** Width to use when inserting image */ @@ -247,8 +249,9 @@ const insertFiles = async function ( }) ); - toast.error( - error.message || t("Sorry, an error occurred uploading the file") + options.onNotice?.( + error.message || t("Sorry, an error occurred uploading the file"), + "error" ); }) .finally(() => { diff --git a/shared/editor/commands/link.ts b/shared/editor/commands/link.ts index 954d3eb6cb..0badb064e6 100644 --- a/shared/editor/commands/link.ts +++ b/shared/editor/commands/link.ts @@ -5,10 +5,9 @@ import type { Command } from "prosemirror-state"; import { NodeSelection, Selection, TextSelection } from "prosemirror-state"; import type * as React from "react"; import { getMarkRange } from "../queries/getMarkRange"; -import { toast } from "sonner"; import { sanitizeUrl } from "@shared/utils/urls"; import { getMarkRangeNodeSelection } from "../queries/getMarkRange"; -import type { NodeAttrMark } from "@shared/editor/types"; +import type { EditorNotice, NodeAttrMark } from "@shared/editor/types"; const addLinkTextSelection = (attrs: Attrs): Command => @@ -59,7 +58,8 @@ const openLinkTextSelection = | MouseEvent | React.MouseEvent ) => void) - | undefined + | undefined, + onNotice?: EditorNotice ): Command => (state) => { if (!(state.selection instanceof TextSelection)) { @@ -72,7 +72,7 @@ const openLinkTextSelection = const event = new KeyboardEvent("keydown", { metaKey: false }); onClickLink(sanitizeUrl(range.mark.attrs.href) ?? "", event); } catch (_err) { - toast.error(t("Sorry, that type of link is not supported")); + onNotice?.(t("Sorry, that type of link is not supported"), "error"); } return true; } @@ -89,7 +89,8 @@ const openLinkNodeSelection = | MouseEvent | React.MouseEvent ) => void) - | undefined + | undefined, + onNotice?: EditorNotice ): Command => (state) => { if (!(state.selection instanceof NodeSelection)) { @@ -110,7 +111,7 @@ const openLinkNodeSelection = const event = new KeyboardEvent("keydown", { metaKey: false }); onClickLink(sanitizeUrl(linkMark.attrs.href) ?? "", event); } catch (_err) { - toast.error(t("Sorry, that type of link is not supported")); + onNotice?.(t("Sorry, that type of link is not supported"), "error"); } return true; }; @@ -259,11 +260,12 @@ export const openLink = ( url: string, event?: KeyboardEvent | MouseEvent | React.MouseEvent ) => void) - | undefined + | undefined, + onNotice?: EditorNotice ): Command => chainCommands( - openLinkTextSelection(onClickLink), - openLinkNodeSelection(onClickLink) + openLinkTextSelection(onClickLink, onNotice), + openLinkNodeSelection(onClickLink, onNotice) ); export const updateLink = (attrs: Attrs): Command => diff --git a/shared/editor/components/ColorSwatch.tsx b/shared/editor/components/ColorSwatch.tsx index a510f11fed..92a7ec1697 100644 --- a/shared/editor/components/ColorSwatch.tsx +++ b/shared/editor/components/ColorSwatch.tsx @@ -2,21 +2,23 @@ import copy from "copy-to-clipboard"; import type { MouseEvent } from "react"; import { useCallback } from "react"; import { useTranslation } from "react-i18next"; -import { toast } from "sonner"; import styled, { css } from "styled-components"; +import type { EditorNotice } from "../types"; interface Props { /** The CSS color the swatch represents, in its original notation. */ color: string; /** The relative luminance of the color, used to pick an outline. */ luminance: number; + /** Callback used to surface a notice to the user. */ + onNotice?: EditorNotice; } /** * A small colored circle rendered after a CSS color inside inline code. Clicking * it copies the color to the clipboard. */ -export function ColorSwatch({ color, luminance }: Props) { +export function ColorSwatch({ color, luminance, onNotice }: Props) { const { t } = useTranslation(); const handleMouseDown = useCallback((event: MouseEvent) => { @@ -29,9 +31,9 @@ export function ColorSwatch({ color, luminance }: Props) { event.preventDefault(); event.stopPropagation(); copy(color); - toast.message(t("Copied to clipboard")); + onNotice?.(t("Copied to clipboard")); }, - [color, t] + [color, onNotice, t] ); return ( diff --git a/shared/editor/extensions/ColorSwatchPreview.ts b/shared/editor/extensions/ColorSwatchPreview.ts index 2a2caf5d2c..9bb293cc19 100644 --- a/shared/editor/extensions/ColorSwatchPreview.ts +++ b/shared/editor/extensions/ColorSwatchPreview.ts @@ -120,6 +120,7 @@ export default class ColorSwatchPreview extends Extension { const element = this.editor.renderToPortal(ColorSwatch, { color, luminance, + onNotice: this.editor.props.onNotice, }); // Keep the mount point layout-neutral; the swatch styles itself. element.style.display = "contents"; diff --git a/shared/editor/extensions/Mermaid.ts b/shared/editor/extensions/Mermaid.ts index 18808ee252..a74876d6bb 100644 --- a/shared/editor/extensions/Mermaid.ts +++ b/shared/editor/extensions/Mermaid.ts @@ -7,7 +7,6 @@ import type { Node } from "prosemirror-model"; import type { Transaction } from "prosemirror-state"; import { NodeSelection, Plugin, PluginKey } from "prosemirror-state"; import { Decoration, DecorationSet } from "prosemirror-view"; -import { toast } from "sonner"; import { errToString } from "../../utils/error"; import { isCode, isMermaid } from "../lib/isCode"; import { isRemoteTransaction, mapDecorations } from "../lib/multiplayer"; @@ -397,7 +396,7 @@ export default function Mermaid({ isDark: boolean; editor: Editor; }) { - const { onClickLink } = editor.props; + const { onClickLink, onNotice } = editor.props; return new Plugin({ key: pluginKey, @@ -633,7 +632,10 @@ export default function Mermaid({ onClickLink(sanitizeUrl(href) ?? ""); } } catch (_err) { - toast.error(t("Sorry, that type of link is not supported")); + onNotice?.( + t("Sorry, that type of link is not supported"), + "error" + ); } } diff --git a/shared/editor/marks/Link.tsx b/shared/editor/marks/Link.tsx index df9d484e1f..0333dfbcf4 100644 --- a/shared/editor/marks/Link.tsx +++ b/shared/editor/marks/Link.tsx @@ -12,7 +12,6 @@ import type { import type { Command, EditorState } from "prosemirror-state"; import { Plugin, TextSelection } from "prosemirror-state"; import type { EditorView } from "prosemirror-view"; -import { toast } from "sonner"; import { isUrl, sanitizeUrl } from "../../utils/urls"; import { getMarkRange } from "../queries/getMarkRange"; import Mark from "./Mark"; @@ -126,7 +125,10 @@ export default class Link extends Mark { keys(): Record { return { - "Mod-Enter": openLink(this.options.onClickLink), + "Mod-Enter": openLink( + this.options.onClickLink, + this.editor.props.onNotice + ), }; } @@ -135,7 +137,8 @@ export default class Link extends Mark { link: (attrs: Attrs) => toggleLink(attrs), addLink, updateLink, - openLink: (): Command => openLink(this.options.onClickLink), + openLink: (): Command => + openLink(this.options.onClickLink, this.editor.props.onNotice), removeLink, }; } @@ -226,7 +229,10 @@ export default class Link extends Mark { this.options.onClickLink(sanitized, event); } } catch (_err) { - toast.error(t("Sorry, that type of link is not supported")); + this.editor.props.onNotice?.( + t("Sorry, that type of link is not supported"), + "error" + ); } return true; diff --git a/shared/editor/nodes/Attachment.tsx b/shared/editor/nodes/Attachment.tsx index e754eeb716..8d9c77717c 100644 --- a/shared/editor/nodes/Attachment.tsx +++ b/shared/editor/nodes/Attachment.tsx @@ -178,6 +178,7 @@ export default class Attachment extends Node { onFileUploadStart, onFileUploadStop, onFileUploadProgress, + onNotice, } = this.editor.props; if (!uploadFile) { @@ -205,6 +206,7 @@ export default class Attachment extends Node { onFileUploadStart, onFileUploadStop, onFileUploadProgress, + onNotice, replaceExisting: true, attrs: { preview: node.attrs.preview, diff --git a/shared/editor/nodes/CodeFence.ts b/shared/editor/nodes/CodeFence.ts index fc9043b93d..a25a2964d7 100644 --- a/shared/editor/nodes/CodeFence.ts +++ b/shared/editor/nodes/CodeFence.ts @@ -16,7 +16,6 @@ import { TextSelection, } from "prosemirror-state"; import { Decoration, DecorationSet, type EditorView } from "prosemirror-view"; -import { toast } from "sonner"; import type { Primitive } from "utility-types"; import type { UserPreferences } from "../../types"; import { isBrowser, isMac } from "../../utils/browser"; @@ -351,7 +350,7 @@ export default class CodeFence extends Node { if (codeBlock) { copy(codeBlock.node.textContent); - toast.message(t("Copied to clipboard")); + this.editor.props.onNotice?.(t("Copied to clipboard")); return true; } @@ -372,7 +371,7 @@ export default class CodeFence extends Node { dispatch?.(tr); copy(tr.doc.textBetween(state.selection.from, state.selection.to)); - toast.message(t("Copied to clipboard")); + this.editor.props.onNotice?.(t("Copied to clipboard")); return true; } diff --git a/shared/editor/nodes/Heading.ts b/shared/editor/nodes/Heading.ts index 44010c3821..7876657135 100644 --- a/shared/editor/nodes/Heading.ts +++ b/shared/editor/nodes/Heading.ts @@ -11,7 +11,6 @@ import type { import type { Command } from "prosemirror-state"; import { Plugin, TextSelection } from "prosemirror-state"; import { Decoration, DecorationSet } from "prosemirror-view"; -import { toast } from "sonner"; import type { Primitive } from "utility-types"; import { isSafari } from "../../utils/browser"; import backspaceToParagraph from "../commands/backspaceToParagraph"; @@ -131,11 +130,14 @@ export default class Heading extends Node { .replace("/edit", ""); try { copy(normalizedUrl + hash); - toast.message(t("Link copied to clipboard")); + this.editor.props.onNotice?.(t("Link copied to clipboard")); } catch (_err) { // Some browser contexts disable the prompt() fallback used by // copy-to-clipboard, causing it to throw – surface it rather than crash. - toast.error(t("Sorry, the link could not be copied")); + this.editor.props.onNotice?.( + t("Sorry, the link could not be copied"), + "error" + ); } }; diff --git a/shared/editor/nodes/SimpleImage.tsx b/shared/editor/nodes/SimpleImage.tsx index 895fc50216..6be5bc666a 100644 --- a/shared/editor/nodes/SimpleImage.tsx +++ b/shared/editor/nodes/SimpleImage.tsx @@ -163,6 +163,7 @@ export default class SimpleImage extends Node { onFileUploadStart, onFileUploadStop, onFileUploadProgress, + onNotice, } = this.editor.props; if (!uploadFile) { @@ -184,6 +185,7 @@ export default class SimpleImage extends Node { onFileUploadStart, onFileUploadStop, onFileUploadProgress, + onNotice, replaceExisting: true, attrs: { width: node.attrs.width, diff --git a/shared/editor/types/index.ts b/shared/editor/types/index.ts index 3ad48a1ec6..f12b90486d 100644 --- a/shared/editor/types/index.ts +++ b/shared/editor/types/index.ts @@ -13,6 +13,16 @@ export type NodeWithPos = { export type PlainTextSerializer = (node: ProsemirrorNode) => string; +/** The severity of a notice surfaced to the user from the editor. */ +export type EditorNoticeType = "info" | "success" | "warning" | "error"; + +/** + * Callback used by the editor to surface a short notice (e.g. a toast) to the + * user. Provided by the host application so shared editor code stays agnostic + * of any specific notification library. + */ +export type EditorNotice = (message: string, type?: EditorNoticeType) => void; + export enum TableLayout { fullWidth = "full-width", }