Skip to content

Commit

Permalink
web: migrate toolbar on first use
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Feb 24, 2024
1 parent 1db4221 commit e058e3b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
7 changes: 5 additions & 2 deletions apps/web/src/common/toolbar-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

import { getDefaultPresets, ToolbarGroupDefinition } from "@notesnook/editor";
import { db } from "./db";
import { migrateToolbar } from "@notesnook/common";

const defaultPresets = getDefaultPresets();
export type PresetId = "default" | "minimal" | "custom";
Expand All @@ -38,9 +39,11 @@ const presets: Record<PresetId, Preset> = {
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 || [];
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/components/editor/tiptap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,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 });
Expand All @@ -200,7 +200,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),
Expand Down
24 changes: 17 additions & 7 deletions apps/web/src/dialogs/settings/components/customize-toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -73,12 +74,11 @@ export function CustomizeToolbar() {
);
const [items, setItems] = useState<TreeNode[]>([]);
const [activeItem, setActiveItem] = useState<TreeNode>();
const [currentPreset, setCurrentPreset] = useState<Preset>(
getCurrentPreset()
);
const [currentPreset, setCurrentPreset] = useState<Preset>();
const { setToolbarConfig } = useToolbarConfig();

useEffect(() => {
if (!currentPreset) return;
const items = flatten(getPresetTools(currentPreset));
items.push(createTrash());
items.push(...flatten([getDisabledTools(items)]).slice(1));
Expand All @@ -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
});
Expand All @@ -98,6 +107,7 @@ export function CustomizeToolbar() {
})();
}, [items]);

if (!currentPreset) return null;
return (
<Flex sx={{ flexDirection: "column" }}>
<Flex
Expand Down Expand Up @@ -128,7 +138,7 @@ export function CustomizeToolbar() {
);
return;
}

console.log("CHANGE PRESET", value);
setCurrentPreset(getPreset(value as PresetId));
}}
/>
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e058e3b

Please sign in to comment.