Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

feat(notebook): add placeholder, redo and undo button (#3564)

Meier Lukas3411354677fe
diff --git a/packages/translation/src/lang/en.json b/packages/translation/src/lang/en.json
index 4bac6ae40..a419b7659 100644
--- a/packages/translation/src/lang/en.json
+++ b/packages/translation/src/lang/en.json
@@ -1502,7 +1502,8 @@
"rows": "Rows",
"width": "Width",
"height": "Height"
- }
+ },
+ "placeholder": "Start writing your notes"
},
"iframe": {
"name": "iFrame",
diff --git a/packages/widgets/package.json b/packages/widgets/package.json
index 9ef19f5f8..17f22d6b3 100644
--- a/packages/widgets/package.json
+++ b/packages/widgets/package.json
@@ -56,6 +56,7 @@
"@tiptap/extension-highlight": "2.25.0",
"@tiptap/extension-image": "2.25.0",
"@tiptap/extension-link": "^2.25.0",
+ "@tiptap/extension-placeholder": "^2.25.0",
"@tiptap/extension-table": "2.25.0",
"@tiptap/extension-table-cell": "2.25.0",
"@tiptap/extension-table-header": "2.25.0",
diff --git a/packages/widgets/src/notebook/notebook.css b/packages/widgets/src/notebook/notebook.css
index 83919bf39..6b8054367 100644
--- a/packages/widgets/src/notebook/notebook.css
+++ b/packages/widgets/src/notebook/notebook.css
@@ -66,3 +66,7 @@
.tiptap[contenteditable="false"].resize-cursor {
pointer-events: none;
}
+
+.ProseMirror {
+ height: 100%;
+}
diff --git a/packages/widgets/src/notebook/notebook.tsx b/packages/widgets/src/notebook/notebook.tsx
index acb22d73e..28a8d27eb 100644
--- a/packages/widgets/src/notebook/notebook.tsx
+++ b/packages/widgets/src/notebook/notebook.tsx
@@ -1,6 +1,6 @@
"use client";
-import { useCallback, useEffect, useState } from "react";
+import { useCallback, useEffect, useRef, useState } from "react";
import {
ActionIcon,
Box,
@@ -43,6 +43,7 @@ import {
import { Color } from "@tiptap/extension-color";
import Highlight from "@tiptap/extension-highlight";
import Image from "@tiptap/extension-image";
+import Placeholder from "@tiptap/extension-placeholder";
import Table from "@tiptap/extension-table";
import TableCell from "@tiptap/extension-table-cell";
import TableHeader from "@tiptap/extension-table-header";
@@ -65,6 +66,7 @@ import type { TablerIcon } from "@homarr/ui";
import type { WidgetComponentProps } from "../definition";
+import "@mantine/tiptap/styles.css";
import "./notebook.css";
import { useSession } from "@homarr/auth/client";
@@ -81,15 +83,15 @@ const controlIconProps = {
stroke: 1.5,
};
-export function Notebook({ options, isEditMode, boardId, itemId }: WidgetComponentProps<"notebook">) {
+export function Notebook({ options, setOptions, isEditMode, boardId, itemId }: WidgetComponentProps<"notebook">) {
const [content, setContent] = useState(options.content);
- const [toSaveContent, setToSaveContent] = useState(content);
+ const previousContentRef = useRef(content);
const board = useRequiredBoard();
const { data: session } = useSession();
const { hasChangeAccess } = constructBoardPermissions(board, session);
- const enabled = !isEditMode && hasChangeAccess;
+ const canChange = !isEditMode && hasChangeAccess;
const [isEditing, setIsEditing] = useState(false);
const { primaryColor } = useMantineTheme();
@@ -103,6 +105,9 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
const editor = useEditor(
{
extensions: [
+ Placeholder.configure({
+ placeholder: `${t("widget.notebook.placeholder")}…`,
+ }),
Color,
Highlight.configure({ multicolor: true }),
Image.extend({
@@ -150,14 +155,14 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
TaskItem.configure({
nested: true,
onReadOnlyChecked: (node, checked) => {
- if (options.allowReadOnlyCheck && enabled) {
- const event = new CustomEvent("onReadOnlyCheck", {
- detail: { node, checked },
- });
- dispatchEvent(event);
- return true;
- }
- return false;
+ if (!options.allowReadOnlyCheck) return false;
+ if (!canChange) return false;
+
+ const event = new CustomEvent("onReadOnlyCheck", {
+ detail: { node, checked },
+ });
+ dispatchEvent(event);
+ return true;
},
}),
TaskList.configure({ itemTypeName: "taskItem" }),
@@ -173,7 +178,7 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
editor.setEditable(false);
},
},
- [toSaveContent],
+ [],
);
const handleOnReadOnlyCheck = (event: CustomEventInit<{ node: Node; checked: boolean }>) => {
@@ -184,16 +189,14 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
if (!event.detail) return;
if (!subnode.eq(event.detail.node)) return;
- if (subnode.eq(event.detail.node)) {
- const { tr } = editor.state;
- tr.setNodeMarkup(pos, undefined, {
- ...event.detail.node.attrs,
- checked: event.detail.checked,
- });
- editor.view.dispatch(tr);
- setContent(editor.getHTML());
- handleContentUpdate(editor.getHTML());
- }
+ const { tr } = editor.state;
+ tr.setNodeMarkup(pos, undefined, {
+ ...event.detail.node.attrs,
+ checked: event.detail.checked,
+ });
+ editor.view.dispatch(tr);
+ setContent(editor.getHTML());
+ handleContentUpdate(editor.getHTML());
});
};
@@ -201,13 +204,15 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
const handleContentUpdate = useCallback(
(contentUpdate: string) => {
- setToSaveContent(contentUpdate);
+ previousContentRef.current = contentUpdate;
+ setOptions({ newOptions: { content: contentUpdate } });
+
// This is not available in preview mode
if (boardId && itemId) {
void mutateAsync({ boardId, itemId, content: contentUpdate });
}
},
- [boardId, itemId, mutateAsync],
+ [boardId, itemId, mutateAsync, setOptions],
);
const handleEditToggleCallback = useCallback(
@@ -216,7 +221,9 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
if (!editor) return current;
editor.setEditable(current);
- handleContentUpdate(content);
+ if (previous) {
+ handleContentUpdate(content);
+ }
return current;
},
@@ -227,11 +234,11 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
if (!editor) return false;
editor.setEditable(false);
- setContent(toSaveContent);
- editor.commands.setContent(toSaveContent);
+ setContent(previousContentRef.current);
+ editor.commands.setContent(previousContentRef.current);
return false;
- }, [editor, toSaveContent]);
+ }, [editor]);
const handleEditCancel = useCallback(() => {
setIsEditing(handleEditCancelCallback);
@@ -242,7 +249,7 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
}, [setIsEditing, handleEditToggleCallback]);
return (
- <Box>
+ <Box h="100%">
<RichTextEditor
p={0}
mt={0}
@@ -251,14 +258,12 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
editor={editor}
styles={(theme) => ({
root: {
- "& .ProseMirror": {
- padding: "0 !important",
- },
backgroundColor: colorScheme === "dark" ? theme.colors.dark[6] : "white",
border: "none",
borderRadius: "0.5rem",
display: "flex",
flexDirection: "column",
+ height: "100%",
},
toolbar: {
backgroundColor: "transparent",
@@ -267,6 +272,10 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
content: {
backgroundColor: "transparent",
padding: "0.5rem",
+ height: "100%",
+ },
+ typographyStylesProvider: {
+ height: "100%",
},
})}
>
@@ -351,6 +360,11 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
</>
)}
</RichTextEditor.ControlsGroup>
+
+ <RichTextEditor.ControlsGroup>
+ <RichTextEditor.Undo />
+ <RichTextEditor.Redo />
+ </RichTextEditor.ControlsGroup>
</RichTextEditor.Toolbar>
{editor && (
<BubbleMenu editor={editor}>
@@ -362,11 +376,24 @@ export function Notebook({ options, isEditMode, boardId, itemId }: WidgetCompone
</BubbleMenu>
)}
- <ScrollArea mih="4rem" offsetScrollbars pl={12} pt={12}>
+ <ScrollArea
+ mih="4rem"
+ offsetScrollbars
+ pl={12}
+ pt={12}
+ styles={{
+ root: {
+ height: "100%",
+ },
+ content: {
+ height: "100%",
+ },
+ }}
+ >
<RichTextEditor.Content />
</ScrollArea>
</RichTextEditor>
- {enabled && (
+ {canChange && (
<>
<ActionIcon
title={isEditing ? t("common.action.save") : t("common.action.edit")}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2f813d292..3b8377239 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -261,7 +261,7 @@ importers:
version: 2.12.5(@types/[email protected])([email protected])
mantine-react-table:
specifier: 2.0.0-beta.9
next:
specifier: 15.3.5
@@ -1552,7 +1552,7 @@ importers:
version: link:../ui
'@mantine/notifications':
specifier: ^8.1.3
'@tabler/icons-react':
specifier: ^3.34.0
version: 3.34.0([email protected])
@@ -1807,7 +1807,7 @@ importers:
version: link:../server-settings
'@mantine/dates':
specifier: ^8.1.3
next:
specifier: 15.3.5
@@ -1874,7 +1874,7 @@ importers:
version: 8.1.3([email protected])
'@mantine/spotlight':
specifier: ^8.1.3
'@tabler/icons-react':
specifier: ^3.34.0
version: 3.34.0([email protected])
@@ -1926,7 +1926,7 @@ importers:
version: 4.3.1
mantine-react-table:
specifier: 2.0.0-beta.9
next:
specifier: 15.3.5
@@ -1978,7 +1978,7 @@ importers:
'@mantine/dates':
specifier: ^8.1.3
'@mantine/hooks':
specifier: ^8.1.3
version: 8.1.3([email protected])
@@ -1987,7 +1987,7 @@ importers:
version: 3.34.0([email protected])
mantine-react-table:
specifier: 2.0.0-beta.9
next:
specifier: 15.3.5
@@ -2146,6 +2146,9 @@ importers:
'@tiptap/extension-link':
specifier: ^2.25.0
version: 2.25.0(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-placeholder':
+ specifier: ^2.25.0
+ version: 2.25.0(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/extension-table':
specifier: 2.25.0
version: 2.25.0(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
@@ -2187,7 +2190,7 @@ importers:
version: 1.11.13
mantine-react-table:
specifier: 2.0.0-beta.9
next:
specifier: 15.3.5
@@ -4463,6 +4466,12 @@ packages:
peerDependencies:
'@tiptap/core': ^2.7.0
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-BUJnp/WQt/8iMJ9wn1xGlA+RiXCsP24u5n+ecxw+DBTgFq7RRL9I1nDQ/IJBrfWMJGOrMEq6tg8rmg1NVLGfWw==}
+ peerDependencies:
+ '@tiptap/core': ^2.7.0
+ '@tiptap/pm': ^2.7.0
+
'@tiptap/[email protected]':
resolution: {integrity: sha512-Z5YBKnv4N6MMD1LEo9XbmWnmdXavZKOOJt/OkXYFZ3KgzB52Z3q3DDfH+NyeCtKKSWqWVxbBHKLnsojDerSf2g==}
peerDependencies:
@@ -12005,7 +12014,7 @@ snapshots:
transitivePeerDependencies:
- '@types/react'
dependencies:
'@mantine/hooks': 8.1.3([email protected])
@@ -12039,7 +12048,7 @@ snapshots:
react: 19.1.0
react-dom: 19.1.0([email protected])
dependencies:
'@mantine/hooks': 8.1.3([email protected])
@@ -12048,7 +12057,7 @@ snapshots:
react-dom: 19.1.0([email protected])
react-transition-group: 4.4.5([email protected]([email protected]))([email protected])
dependencies:
'@mantine/hooks': 8.1.3([email protected])
@@ -13156,6 +13165,11 @@ snapshots:
dependencies:
'@tiptap/core': 2.25.0(@tiptap/[email protected])
+ dependencies:
+ '@tiptap/core': 2.25.0(@tiptap/[email protected])
+ '@tiptap/pm': 2.25.0
+
dependencies:
'@tiptap/core': 2.25.0(@tiptap/[email protected])
@@ -17136,10 +17150,10 @@ snapshots:
dependencies:
'@mantine/hooks': 8.1.3([email protected])
'@tabler/icons-react': 3.34.0([email protected])
'@tanstack/match-sorter-utils': 8.19.4