diff --git a/apps/web/src/common/toolbar-config.ts b/apps/web/src/common/toolbar-config.ts index 7c3f02aa2..bd7291388 100644 --- a/apps/web/src/common/toolbar-config.ts +++ b/apps/web/src/common/toolbar-config.ts @@ -19,6 +19,7 @@ along with this program. If not, see . import { getDefaultPresets, ToolbarGroupDefinition } from "@notesnook/editor"; import { db } from "./db"; +import { migrateToolbar } from "@notesnook/common"; const defaultPresets = getDefaultPresets(); export type PresetId = "default" | "minimal" | "custom"; @@ -38,9 +39,11 @@ const presets: Record = { custom: { id: "custom", title: "Custom", tools: [], editable: true } }; -export function getCurrentPreset() { - const preset = db.settings.getToolbarConfig("desktop"); +export async function getCurrentPreset() { + let preset = db.settings.getToolbarConfig("desktop"); if (!preset) return presets.default; + preset = await migrateToolbar("desktop", preset); + switch (preset.preset as PresetId) { case "custom": presets.custom.tools = preset.config || []; diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsx index 57266e585..d650ab220 100644 --- a/apps/web/src/components/editor/tiptap.tsx +++ b/apps/web/src/components/editor/tiptap.tsx @@ -194,7 +194,7 @@ function TipTap(props: TipTapProps) { content: content?.(), autofocus: "start", onFocus, - onCreate: ({ editor }) => { + onCreate: async ({ editor }) => { if (onLoad) onLoad(); if (oldNonce.current !== nonce) editor.commands.focus("start", { scrollIntoView: true }); @@ -204,7 +204,7 @@ function TipTap(props: TipTapProps) { editor: toIEditor(editor as Editor), canRedo: editor.can().redo(), canUndo: editor.can().undo(), - toolbarConfig: getCurrentPreset().tools, + toolbarConfig: (await getCurrentPreset()).tools, statistics: { words: { total: getTotalWords(editor as Editor), diff --git a/apps/web/src/dialogs/settings/components/customize-toolbar.tsx b/apps/web/src/dialogs/settings/components/customize-toolbar.tsx index fae9c83d0..570770295 100644 --- a/apps/web/src/dialogs/settings/components/customize-toolbar.tsx +++ b/apps/web/src/dialogs/settings/components/customize-toolbar.tsx @@ -63,6 +63,7 @@ import { isUserPremium } from "../../../hooks/use-is-user-premium"; import { Pro } from "../../../components/icons"; import { Icon } from "@notesnook/ui"; +import { CURRENT_TOOLBAR_VERSION } from "@notesnook/common"; export function CustomizeToolbar() { const sensors = useSensors( @@ -73,12 +74,11 @@ export function CustomizeToolbar() { ); const [items, setItems] = useState([]); const [activeItem, setActiveItem] = useState(); - const [currentPreset, setCurrentPreset] = useState( - getCurrentPreset() - ); + const [currentPreset, setCurrentPreset] = useState(); const { setToolbarConfig } = useToolbarConfig(); useEffect(() => { + if (!currentPreset) return; const items = flatten(getPresetTools(currentPreset)); items.push(createTrash()); items.push(...flatten([getDisabledTools(items)]).slice(1)); @@ -87,9 +87,18 @@ export function CustomizeToolbar() { useEffect(() => { (async () => { + const preset = await getCurrentPreset(); + setCurrentPreset(preset); + })(); + }, []); + + useEffect(() => { + (async () => { + if (!currentPreset) return; const tools = unflatten(items).slice(0, -1); await db.settings.setToolbarConfig("desktop", { + version: CURRENT_TOOLBAR_VERSION, preset: currentPreset.id, config: currentPreset.id === "custom" ? tools : undefined }); @@ -98,6 +107,7 @@ export function CustomizeToolbar() { })(); }, [items]); + if (!currentPreset) return null; return ( @@ -171,10 +181,10 @@ export function CustomizeToolbar() { }} onDragStart={(event) => { if (currentPreset.id !== "custom") { - setCurrentPreset((c) => ({ + setCurrentPreset({ ...getPreset("custom"), - tools: getPresetTools(c) - })); + tools: getPresetTools(currentPreset) + }); } const { active } = event;