diff --git a/app/editor/components/SelectionToolbar.tsx b/app/editor/components/SelectionToolbar.tsx
index 04d85b5583..798f984344 100644
--- a/app/editor/components/SelectionToolbar.tsx
+++ b/app/editor/components/SelectionToolbar.tsx
@@ -276,7 +276,7 @@ export function SelectionToolbar(props: Props) {
items = filterExcessSeparators(items);
items = items.map((item) => {
- if (item.children) {
+ if (item.children && Array.isArray(item.children)) {
item.children = item.children.map((child) => {
if (child.name === "editImageUrl") {
child.onClick = () => {
diff --git a/app/editor/components/ToolbarMenu.tsx b/app/editor/components/ToolbarMenu.tsx
index f506ef4ac3..be1d6e4f69 100644
--- a/app/editor/components/ToolbarMenu.tsx
+++ b/app/editor/components/ToolbarMenu.tsx
@@ -44,6 +44,10 @@ function ToolbarDropdown(props: ToolbarDropdownProps) {
}, []);
const items: TMenuItem[] = useMemo(() => {
+ if (!isOpen) {
+ return [];
+ }
+
const handleClick = (menuItem: MenuItem) => () => {
if (!menuItem.name) {
return;
@@ -60,6 +64,11 @@ function ToolbarDropdown(props: ToolbarDropdownProps) {
}
};
+ const resolveChildren = (
+ children: MenuItem[] | (() => MenuItem[]) | undefined
+ ): MenuItem[] | undefined =>
+ typeof children === "function" ? children() : children;
+
const mapChildren = (children: MenuItem[]): TMenuItem[] =>
children.map((child) => {
if (child.name === "separator") {
@@ -72,8 +81,9 @@ function ToolbarDropdown(props: ToolbarDropdownProps) {
content: child.content,
};
}
- if (child.children) {
- const childWithPreventClose = child.children.find(
+ const resolvedChildren = resolveChildren(child.children);
+ if (resolvedChildren) {
+ const childWithPreventClose = resolvedChildren.find(
(c) => "preventCloseCondition" in c
);
return {
@@ -82,7 +92,7 @@ function ToolbarDropdown(props: ToolbarDropdownProps) {
icon: child.icon,
visible: child.visible,
preventCloseCondition: childWithPreventClose?.preventCloseCondition,
- items: mapChildren(child.children),
+ items: mapChildren(resolvedChildren),
};
}
return {
@@ -97,7 +107,8 @@ function ToolbarDropdown(props: ToolbarDropdownProps) {
};
});
- return item.children ? mapChildren(item.children) : [];
+ const resolvedItemChildren = resolveChildren(item.children);
+ return resolvedItemChildren ? mapChildren(resolvedItemChildren) : [];
}, [isOpen, commands]);
const handleCloseAutoFocus = useCallback((ev: Event) => {
diff --git a/app/editor/menus/formatting.tsx b/app/editor/menus/formatting.tsx
index 92cccff956..38fa33534d 100644
--- a/app/editor/menus/formatting.tsx
+++ b/app/editor/menus/formatting.tsx
@@ -27,6 +27,7 @@ import CellBackgroundColorPicker from "../components/CellBackgroundColorPicker";
import HighlightColorPicker from "../components/HighlightColorPicker";
import type { EditorState } from "prosemirror-state";
+import { getDocumentHighlightColors } from "@shared/editor/queries/getDocumentHighlightColors";
import { getMarksBetween } from "@shared/editor/queries/getMarksBetween";
import { isInCode } from "@shared/editor/queries/isInCode";
import { isInList } from "@shared/editor/queries/isInList";
@@ -42,6 +43,7 @@ import {
} from "@shared/utils/browser";
import {
getColorSetForSelectedCells,
+ getDocumentTableBackgroundColors,
hasNodeAttrMarkCellSelection,
hasNodeAttrMarkWithAttrsCellSelection,
isMergedCellSelection,
@@ -132,66 +134,85 @@ export default function formattingMenuItems(
),
visible: !isCode && (!isMobile || !isEmpty) && isTableCell,
- children: [
- {
- name: "toggleCellSelectionBackgroundAndCollapseSelection",
- label: dictionary.none,
- icon: ,
- active: () => (cellSelectionHasBackground ? false : true),
- attrs: { color: null },
- },
- ...TableCell.presetColors.map((preset) => ({
- name: "toggleCellSelectionBackgroundAndCollapseSelection",
- label: preset.name,
- icon: ,
- active: () =>
- hasNodeAttrMarkWithAttrsCellSelection(
- state.selection as CellSelection,
- "background",
- { color: preset.hex }
- ),
- attrs: { color: preset.hex },
- })),
- ...(selectedCellsColorSet.size === 1 &&
- !TableCell.isPresetColor(selectedCellsColorSet.values().next().value)
- ? [
+ children: (): MenuItem[] => {
+ // Get all unique background colors used in table cells (lazily computed when menu opens)
+ const documentTableColors = getDocumentTableBackgroundColors(state);
+
+ // Filter out preset colors and currently selected colors
+ const nonPresetDocumentColors = documentTableColors.filter(
+ (color: string) =>
+ !TableCell.isPresetColor(color) && !selectedCellsColorSet.has(color)
+ );
+
+ return [
+ {
+ name: "toggleCellSelectionBackgroundAndCollapseSelection",
+ label: dictionary.none,
+ icon: ,
+ active: () => (cellSelectionHasBackground ? false : true),
+ attrs: { color: null },
+ },
+ ...TableCell.presetColors.map((preset) => ({
+ name: "toggleCellSelectionBackgroundAndCollapseSelection",
+ label: preset.name,
+ icon: ,
+ active: () =>
+ hasNodeAttrMarkWithAttrsCellSelection(
+ state.selection as CellSelection,
+ "background",
+ { color: preset.hex }
+ ),
+ attrs: { color: preset.hex },
+ })),
+ ...(selectedCellsColorSet.size === 1 &&
+ !TableCell.isPresetColor(selectedCellsColorSet.values().next().value)
+ ? [
+ {
+ name: "toggleCellSelectionBackgroundAndCollapseSelection",
+ label: selectedCellsColorSet.values().next().value,
+ icon: (
+
+ ),
+ active: () => true,
+ attrs: { color: selectedCellsColorSet.values().next().value },
+ },
+ ]
+ : []),
+ // Add all other document table background colors
+ ...nonPresetDocumentColors.map((color: string) => ({
+ name: "toggleCellSelectionBackgroundAndCollapseSelection",
+ label: color,
+ icon: ,
+ active: () => selectedCellsColorSet.has(color),
+ attrs: { color },
+ })),
+ {
+ icon: ,
+ label: "Custom",
+ children: [
{
- name: "toggleCellSelectionBackgroundAndCollapseSelection",
- label: selectedCellsColorSet.values().next().value,
- icon: (
-
),
- active: () => true,
- attrs: { color: selectedCellsColorSet.values().next().value },
+ preventCloseCondition: () =>
+ !!document.activeElement?.matches(
+ ".ProseMirror.ProseMirror-focused"
+ ),
},
- ]
- : []),
- {
- icon: ,
- label: "Custom",
- children: [
- {
- content: (
-
- ),
- preventCloseCondition: () =>
- !!document.activeElement?.matches(
- ".ProseMirror.ProseMirror-focused"
- ),
- },
- ],
- },
- ],
+ ],
+ },
+ ];
+ },
},
{
tooltip: dictionary.mark,
@@ -205,65 +226,86 @@ export default function formattingMenuItems(
),
active: () => !!highlight,
visible: !isCode && (!isMobile || !isEmpty) && !isTableCell,
- children: [
- ...(highlight
- ? [
+ children: (): MenuItem[] => {
+ // Get all unique highlight colors used in the document (lazily computed when menu opens)
+ const documentHighlightColors = getDocumentHighlightColors(state);
+
+ // Filter out preset colors and the currently selected color
+ const currentHighlightColor = highlight?.mark.attrs.color;
+ const nonPresetDocumentColors = documentHighlightColors.filter(
+ (color: string) =>
+ !Highlight.isPresetColor(color) && color !== currentHighlightColor
+ );
+
+ return [
+ ...(highlight
+ ? [
+ {
+ name: "highlight",
+ label: dictionary.none,
+ icon: ,
+ active: () => false,
+ attrs: { color: highlight.mark.attrs.color },
+ },
+ ]
+ : []),
+ ...Highlight.presetColors.map((preset) => ({
+ name: "highlight",
+ label: preset.name,
+ icon: ,
+ active: isMarkActive(schema.marks.highlight, { color: preset.hex }),
+ attrs: { color: preset.hex },
+ })),
+ ...(highlight &&
+ highlight.mark.attrs.color &&
+ !Highlight.isPresetColor(highlight.mark.attrs.color)
+ ? [
+ {
+ name: "highlight",
+ label: highlight.mark.attrs.color,
+ icon: (
+
+ ),
+ active: isMarkActive(schema.marks.highlight, {
+ color: highlight.mark.attrs.color,
+ }),
+ attrs: { color: highlight.mark.attrs.color },
+ },
+ ]
+ : []),
+ // Add all other document highlight colors
+ ...nonPresetDocumentColors.map((color: string) => ({
+ name: "highlight",
+ label: color,
+ icon: ,
+ active: () => currentHighlightColor === color,
+ attrs: { color },
+ })),
+ {
+ icon: ,
+ label: "Custom",
+ children: [
{
- name: "highlight",
- label: dictionary.none,
- icon: ,
- active: () => false,
- attrs: { color: highlight.mark.attrs.color },
- },
- ]
- : []),
- ...Highlight.presetColors.map((preset) => ({
- name: "highlight",
- label: preset.name,
- icon: ,
- active: isMarkActive(schema.marks.highlight, { color: preset.hex }),
- attrs: { color: preset.hex },
- })),
- ...(highlight &&
- highlight.mark.attrs.color &&
- !Highlight.isPresetColor(highlight.mark.attrs.color)
- ? [
- {
- name: "highlight",
- label: highlight.mark.attrs.color,
- icon: (
-
),
- active: isMarkActive(schema.marks.highlight, {
- color: highlight.mark.attrs.color,
- }),
- attrs: { color: highlight.mark.attrs.color },
+ preventCloseCondition: () =>
+ !!document.activeElement?.matches(
+ ".ProseMirror.ProseMirror-focused"
+ ),
},
- ]
- : []),
- {
- icon: ,
- label: "Custom",
- children: [
- {
- content: (
-
- ),
- preventCloseCondition: () =>
- !!document.activeElement?.matches(
- ".ProseMirror.ProseMirror-focused"
- ),
- },
- ],
- },
- ],
+ ],
+ },
+ ];
+ },
},
{
name: "code_inline",
diff --git a/shared/editor/queries/getDocumentHighlightColors.test.ts b/shared/editor/queries/getDocumentHighlightColors.test.ts
new file mode 100644
index 0000000000..00febc2950
--- /dev/null
+++ b/shared/editor/queries/getDocumentHighlightColors.test.ts
@@ -0,0 +1,94 @@
+import { schema, createEditorState, doc, p } from "@shared/test/editor";
+import { getDocumentHighlightColors } from "./getDocumentHighlightColors";
+
+describe("getDocumentHighlightColors", () => {
+ it("returns empty array when no highlights exist", () => {
+ const testDoc = doc([p("Plain text without highlights")]);
+ const state = createEditorState(testDoc);
+
+ const colors = getDocumentHighlightColors(state);
+
+ expect(colors).toEqual([]);
+ });
+
+ it("returns unique highlight colors from document", () => {
+ // Create text with highlight marks
+ const highlightMark1 = schema.marks.highlight.create({ color: "#FDEA9B" });
+ const highlightMark2 = schema.marks.highlight.create({ color: "#FED46A" });
+
+ const text1 = schema.text("Highlighted text 1", [highlightMark1]);
+ const text2 = schema.text(" and more ", [highlightMark2]);
+ const text3 = schema.text("and again", [highlightMark1]);
+
+ const testDoc = doc([
+ schema.nodes.paragraph.create(null, [text1, text2, text3])
+ ]);
+ const state = createEditorState(testDoc);
+
+ const colors = getDocumentHighlightColors(state);
+
+ expect(colors).toHaveLength(2);
+ expect(colors).toContain("#FDEA9B");
+ expect(colors).toContain("#FED46A");
+ });
+
+ it("deduplicates colors used multiple times", () => {
+ const highlightMark = schema.marks.highlight.create({ color: "#FDEA9B" });
+
+ const text1 = schema.text("First highlight", [highlightMark]);
+ const text2 = schema.text("Second highlight", [highlightMark]);
+
+ const testDoc = doc([
+ schema.nodes.paragraph.create(null, [text1]),
+ schema.nodes.paragraph.create(null, [text2])
+ ]);
+ const state = createEditorState(testDoc);
+
+ const colors = getDocumentHighlightColors(state);
+
+ expect(colors).toHaveLength(1);
+ expect(colors).toContain("#FDEA9B");
+ });
+
+ it("returns multiple different colors from multiple paragraphs", () => {
+ const highlightMark1 = schema.marks.highlight.create({ color: "#FDEA9B" });
+ const highlightMark2 = schema.marks.highlight.create({ color: "#FED46A" });
+ const highlightMark3 = schema.marks.highlight.create({ color: "#FA551E" });
+
+ const text1 = schema.text("First paragraph", [highlightMark1]);
+ const text2 = schema.text("Second paragraph", [highlightMark2]);
+ const text3 = schema.text("Third paragraph", [highlightMark3]);
+
+ const testDoc = doc([
+ schema.nodes.paragraph.create(null, [text1]),
+ schema.nodes.paragraph.create(null, [text2]),
+ schema.nodes.paragraph.create(null, [text3])
+ ]);
+ const state = createEditorState(testDoc);
+
+ const colors = getDocumentHighlightColors(state);
+
+ expect(colors).toHaveLength(3);
+ expect(colors).toContain("#FDEA9B");
+ expect(colors).toContain("#FED46A");
+ expect(colors).toContain("#FA551E");
+ });
+
+ it("ignores text with other marks but no highlight", () => {
+ const boldMark = schema.marks.strong.create();
+ const highlightMark = schema.marks.highlight.create({ color: "#FDEA9B" });
+
+ const boldText = schema.text("Bold text", [boldMark]);
+ const highlightedText = schema.text("Highlighted text", [highlightMark]);
+
+ const testDoc = doc([
+ schema.nodes.paragraph.create(null, [boldText, highlightedText])
+ ]);
+ const state = createEditorState(testDoc);
+
+ const colors = getDocumentHighlightColors(state);
+
+ expect(colors).toHaveLength(1);
+ expect(colors).toContain("#FDEA9B");
+ });
+});
diff --git a/shared/editor/queries/getDocumentHighlightColors.ts b/shared/editor/queries/getDocumentHighlightColors.ts
new file mode 100644
index 0000000000..d4533b4baf
--- /dev/null
+++ b/shared/editor/queries/getDocumentHighlightColors.ts
@@ -0,0 +1,22 @@
+import type { EditorState } from "prosemirror-state";
+
+/**
+ * Get all unique highlight colors used in the document.
+ *
+ * @param state The editor state.
+ * @returns An array of unique hex color strings used for highlights in the document.
+ */
+export function getDocumentHighlightColors(state: EditorState): string[] {
+ const colors = new Set();
+
+ state.doc.descendants((node) => {
+ if (node.isText) {
+ const highlightMark = node.marks.find((mark) => mark.type.name === "highlight");
+ if (highlightMark?.attrs.color) {
+ colors.add(highlightMark.attrs.color);
+ }
+ }
+ });
+
+ return Array.from(colors);
+}
diff --git a/shared/editor/queries/table.ts b/shared/editor/queries/table.ts
index 66e7e5a929..83ed163684 100644
--- a/shared/editor/queries/table.ts
+++ b/shared/editor/queries/table.ts
@@ -492,6 +492,29 @@ export function getColorSetForSelectedCells(selection: Selection): Set {
return colors;
}
+/**
+ * Get all unique background colors used in table cells across the entire document.
+ *
+ * @param state The editor state.
+ * @returns An array of unique hex color strings used for table cell backgrounds in the document.
+ */
+export function getDocumentTableBackgroundColors(state: EditorState): string[] {
+ const colors = new Set();
+
+ state.doc.descendants((node) => {
+ if (node.type.name === "td" || node.type.name === "th") {
+ const backgroundMark = (node.attrs.marks ?? []).find(
+ (mark: NodeAttrMark) => mark.type === "background"
+ );
+ if (backgroundMark && backgroundMark.attrs.color) {
+ colors.add(backgroundMark.attrs.color);
+ }
+ }
+ });
+
+ return Array.from(colors);
+}
+
/**
* Returns true if any cell in the selection has a mark of the given type
* with matching attributes.
diff --git a/shared/editor/types/index.ts b/shared/editor/types/index.ts
index 551c851df7..fed42810ab 100644
--- a/shared/editor/types/index.ts
+++ b/shared/editor/types/index.ts
@@ -32,7 +32,7 @@ export type MenuItem = {
dangerous?: boolean;
/** Higher number is higher in results, default is 0. */
priority?: number;
- children?: MenuItem[];
+ children?: MenuItem[] | (() => MenuItem[]);
defaultHidden?: boolean;
attrs?:
| Record