Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: improve task list ux & fix issue in dropcursor

thecodrr41dc5623b82a
diff --git a/packages/editor/dist/extensions/dropcursor/dropcursor.d.ts b/packages/editor/dist/extensions/dropcursor/dropcursor.d.ts
new file mode 100644
index 000000000..5901f57f7
--- /dev/null
+++ b/packages/editor/dist/extensions/dropcursor/dropcursor.d.ts
@@ -0,0 +1,7 @@
+import { Extension } from "@tiptap/core";
+export interface DropcursorOptions {
+ color?: string | null;
+ width?: number | null;
+ class?: string | null;
+}
+export declare const Dropcursor: Extension<DropcursorOptions, any>;
diff --git a/packages/editor/dist/extensions/dropcursor/dropcursor.js b/packages/editor/dist/extensions/dropcursor/dropcursor.js
new file mode 100644
index 000000000..4ab92e676
--- /dev/null
+++ b/packages/editor/dist/extensions/dropcursor/dropcursor.js
@@ -0,0 +1,15 @@
+import { Extension } from "@tiptap/core";
+import { dropCursor } from "./drop-cursor.pm";
+export var Dropcursor = Extension.create({
+ name: "dropCursor",
+ addOptions: function () {
+ return {
+ color: "currentColor",
+ width: 1,
+ class: null,
+ };
+ },
+ addProseMirrorPlugins: function () {
+ return [dropCursor(this.options)];
+ },
+});
diff --git a/packages/editor/dist/extensions/dropcursor/dropcursor.pm.d.ts b/packages/editor/dist/extensions/dropcursor/dropcursor.pm.d.ts
new file mode 100644
index 000000000..cda1cb5f3
--- /dev/null
+++ b/packages/editor/dist/extensions/dropcursor/dropcursor.pm.d.ts
@@ -0,0 +1,3 @@
+import { Plugin } from "prosemirror-state";
+import { DropcursorOptions } from "./drop-cursor";
+export declare function dropCursor(options?: DropcursorOptions): Plugin<any, any>;
diff --git a/packages/editor/dist/extensions/dropcursor/dropcursor.pm.js b/packages/editor/dist/extensions/dropcursor/dropcursor.pm.js
new file mode 100644
index 000000000..d4c42d088
--- /dev/null
+++ b/packages/editor/dist/extensions/dropcursor/dropcursor.pm.js
@@ -0,0 +1,177 @@
+import { Plugin } from "prosemirror-state";
+import { dropPoint } from "prosemirror-transform";
+// :: (options: ?Object) → Plugin
+// Create a plugin that, when added to a ProseMirror instance,
+// causes a decoration to show up at the drop position when something
+// is dragged over the editor.
+//
+// Nodes may add a `disableDropCursor` property to their spec to
+// control the showing of a drop cursor inside them. This may be a
+// boolean or a function, which will be called with a view and a
+// position, and should return a boolean.
+//
+// options::- These options are supported:
+//
+// color:: ?string
+// The color of the cursor. Defaults to `black`.
+//
+// width:: ?number
+// The precise width of the cursor in pixels. Defaults to 1.
+//
+// class:: ?string
+// A CSS class name to add to the cursor element.
+export function dropCursor(options) {
+ if (options === void 0) { options = {}; }
+ return new Plugin({
+ view: function (editorView) {
+ return new DropCursorView(editorView, options);
+ },
+ });
+}
+var DropCursorView = /** @class */ (function () {
+ function DropCursorView(editorView, options) {
+ var _this = this;
+ this.editorView = editorView;
+ this.width = options.width || 1;
+ this.color = options.color || "black";
+ this.class = options.class || "";
+ this.cursorPos = null;
+ this.element = null;
+ this.timeout = null;
+ this.handlers = ["dragover", "dragend", "drop", "dragleave"].map(function (name) {
+ var handler = function (e) { return _this[name](e); };
+ editorView.dom.addEventListener(name, handler);
+ return { name: name, handler: handler };
+ });
+ }
+ DropCursorView.prototype.destroy = function () {
+ var _this = this;
+ this.handlers.forEach(function (_a) {
+ var name = _a.name, handler = _a.handler;
+ return _this.editorView.dom.removeEventListener(name, handler);
+ });
+ };
+ DropCursorView.prototype.update = function (editorView, prevState) {
+ if (this.cursorPos != null && prevState.doc != editorView.state.doc) {
+ if (this.cursorPos > editorView.state.doc.content.size)
+ this.setCursor(null);
+ else
+ this.updateOverlay();
+ }
+ };
+ DropCursorView.prototype.setCursor = function (pos) {
+ if (pos == this.cursorPos)
+ return;
+ this.cursorPos = pos;
+ if (pos == null && this.element && this.element.parentNode) {
+ this.element.parentNode.removeChild(this.element);
+ this.element = null;
+ }
+ else {
+ this.updateOverlay();
+ }
+ };
+ DropCursorView.prototype.updateOverlay = function () {
+ if (!this.cursorPos)
+ return;
+ var $pos = this.editorView.state.doc.resolve(this.cursorPos), rect;
+ if (!$pos.parent.inlineContent) {
+ var before = $pos.nodeBefore, after = $pos.nodeAfter;
+ if (before || after) {
+ var node = this.editorView.nodeDOM(this.cursorPos - (before ? before.nodeSize : 0));
+ if (!node)
+ return;
+ var nodeRect = node.getBoundingClientRect();
+ var top_1 = before ? nodeRect.bottom : nodeRect.top;
+ if (before && after) {
+ var cursor = this.editorView.nodeDOM(this.cursorPos);
+ if (!cursor)
+ return;
+ top_1 = (top_1 + cursor.getBoundingClientRect().top) / 2;
+ }
+ rect = {
+ left: nodeRect.left,
+ right: nodeRect.right,
+ top: top_1 - this.width / 2,
+ bottom: top_1 + this.width / 2,
+ };
+ }
+ }
+ if (!rect) {
+ var coords = this.editorView.coordsAtPos(this.cursorPos);
+ rect = {
+ left: coords.left - this.width / 2,
+ right: coords.left + this.width / 2,
+ top: coords.top,
+ bottom: coords.bottom,
+ };
+ }
+ var parent = this.editorView.dom.offsetParent;
+ if (!this.element && parent) {
+ this.element = parent.appendChild(document.createElement("div"));
+ if (this.class)
+ this.element.className = this.class;
+ this.element.style.cssText =
+ "position: absolute; z-index: 50; pointer-events: none; background-color: " +
+ this.color;
+ }
+ var parentLeft, parentTop;
+ if (!parent ||
+ (parent == document.body && getComputedStyle(parent).position == "static")) {
+ parentLeft = -pageXOffset;
+ parentTop = -pageYOffset;
+ }
+ else {
+ var rect_1 = parent.getBoundingClientRect();
+ parentLeft = rect_1.left - parent.scrollLeft;
+ parentTop = rect_1.top - parent.scrollTop;
+ }
+ if (!this.element)
+ return;
+ this.element.style.left = rect.left - parentLeft + "px";
+ this.element.style.top = rect.top - parentTop + "px";
+ this.element.style.width = rect.right - rect.left + "px";
+ this.element.style.height = rect.bottom - rect.top + "px";
+ };
+ DropCursorView.prototype.scheduleRemoval = function (timeout) {
+ var _this = this;
+ clearTimeout(this.timeout || undefined);
+ this.timeout = (setTimeout(function () { return _this.setCursor(null); }, timeout));
+ };
+ DropCursorView.prototype.dragover = function (event) {
+ if (!this.editorView.editable)
+ return;
+ var pos = this.editorView.posAtCoords({
+ left: event.clientX,
+ top: event.clientY,
+ });
+ var node = pos && pos.inside >= 0 && this.editorView.state.doc.nodeAt(pos.inside);
+ var disableDropCursor = node && node.type.spec.disableDropCursor;
+ var disabled = typeof disableDropCursor == "function"
+ ? disableDropCursor(this.editorView, pos)
+ : disableDropCursor;
+ if (pos && !disabled) {
+ var target = pos.pos;
+ if (this.editorView.dragging && this.editorView.dragging.slice) {
+ var point = dropPoint(this.editorView.state.doc, target, this.editorView.dragging.slice);
+ if (!point)
+ return this.setCursor(null);
+ target = point;
+ }
+ this.setCursor(target);
+ this.scheduleRemoval(5000);
+ }
+ };
+ DropCursorView.prototype.dragend = function () {
+ this.scheduleRemoval(20);
+ };
+ DropCursorView.prototype.drop = function () {
+ this.scheduleRemoval(20);
+ };
+ DropCursorView.prototype.dragleave = function (event) {
+ if (event.target == this.editorView.dom ||
+ !this.editorView.dom.contains(event.relatedTarget))
+ this.setCursor(null);
+ };
+ return DropCursorView;
+}());
diff --git a/packages/editor/dist/extensions/dropcursor/index.d.ts b/packages/editor/dist/extensions/dropcursor/index.d.ts
new file mode 100644
index 000000000..8b3d7693d
--- /dev/null
+++ b/packages/editor/dist/extensions/dropcursor/index.d.ts
@@ -0,0 +1,3 @@
+import { Dropcursor } from "./drop-cursor";
+export * from "./drop-cursor";
+export default Dropcursor;
diff --git a/packages/editor/dist/extensions/dropcursor/index.js b/packages/editor/dist/extensions/dropcursor/index.js
new file mode 100644
index 000000000..8b3d7693d
--- /dev/null
+++ b/packages/editor/dist/extensions/dropcursor/index.js
@@ -0,0 +1,3 @@
+import { Dropcursor } from "./drop-cursor";
+export * from "./drop-cursor";
+export default Dropcursor;
diff --git a/packages/editor/dist/extensions/taskitem/component.js b/packages/editor/dist/extensions/taskitem/component.js
index d2a4b04b5..3c6251edd 100644
--- a/packages/editor/dist/extensions/taskitem/component.js
+++ b/packages/editor/dist/extensions/taskitem/component.js
@@ -9,25 +9,19 @@ var __assign = (this && this.__assign) || function () {
};
return __assign.apply(this, arguments);
};
-import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
-import { Flex } from "rebass";
+import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
+import { Flex, Text } from "rebass";
import { NodeViewWrapper, NodeViewContent, } from "@tiptap/react";
import { ThemeProvider } from "emotion-theming";
import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
import { findChildren, } from "@tiptap/core";
-import { useCallback } from "react";
+import { useCallback, useEffect, useState } from "react";
export function TaskItemComponent(props) {
var checked = props.node.attrs.checked;
+ var _a = useState({ checked: 0, total: 0 }), stats = _a[0], setStats = _a[1];
var editor = props.editor, updateAttributes = props.updateAttributes, node = props.node, getPos = props.getPos;
- // const [isOpen, setIsOpen] = useState(true);
- // const elementRef = useRef<HTMLSpanElement>();
- // const isActive = editor.isActive("attachment", { hash });
- // const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
var theme = editor.storage.theme;
- // useEffect(() => {
- // setIsToolbarVisible(isActive);
- // }, [isActive]);
var toggle = useCallback(function () {
if (!editor.isEditable)
return false;
@@ -43,71 +37,86 @@ export function TaskItemComponent(props) {
return node.type.name === "taskList";
});
var isNested = !!nestedTaskList;
+ var isCollapsed = nestedTaskList
+ ? nestedTaskList.node.attrs.collapsed
+ : false;
+ useEffect(function () {
+ if (!nestedTaskList)
+ return;
+ var pos = nestedTaskList.pos, node = nestedTaskList.node;
+ var children = findChildren(node, function (node) { return node.type.name === "taskItem"; });
+ var checked = children.filter(function (_a) {
+ var node = _a.node;
+ return node.attrs.checked;
+ }).length;
+ var total = children.length;
+ setStats({ checked: checked, total: total });
+ }, [isNested, nestedTaskList, node]);
return (_jsx(NodeViewWrapper, { children: _jsx(ThemeProvider, __assign({ theme: theme }, { children: _jsxs(Flex, __assign({ sx: {
mb: 2,
":hover > .dragHandle, :hover > .toggleSublist": {
opacity: 1,
},
- } }, { children: [_jsxs(Flex, __assign({ sx: { flex: 1 } }, { children: [_jsx(Icon, { className: "dragHandle", draggable: "true", contentEditable: false, "data-drag-handle": true, path: Icons.dragHandle, sx: {
- opacity: 0,
- alignSelf: "start",
- mr: 2,
- cursor: "grab",
- ".icon:hover path": {
- fill: "var(--disabled) !important",
- },
- }, size: 20 }), _jsx(Icon, { path: checked ? Icons.check : "", sx: {
- border: "2px solid",
- borderColor: checked ? "disabled" : "icon",
- borderRadius: "default",
- alignSelf: "start",
- mr: 2,
- p: "1px",
- cursor: "pointer",
- ":hover": {
- borderColor: "disabled",
- },
- ":hover .icon path": {
- fill: "var(--disabled) !important",
- },
- }, onMouseEnter: function (e) {
- if (e.buttons > 0) {
- toggle();
- }
- }, onMouseDown: function (e) {
- if (toggle())
- e.preventDefault();
- }, color: checked ? "disabled" : "icon", size: 13 }), _jsx(NodeViewContent, { as: "li", style: {
- listStyleType: "none",
- textDecorationLine: checked ? "line-through" : "none",
- color: checked ? "var(--disabled)" : "var(--text)",
- flex: 1,
- } })] })), isNested && (_jsx(Icon, { className: "toggleSublist", path: nestedTaskList.node.attrs.collapsed
- ? Icons.chevronDown
- : Icons.chevronUp, sx: {
+ } }, { children: [_jsx(Icon, { className: "dragHandle", draggable: "true", contentEditable: false, "data-drag-handle": true, path: Icons.dragHandle, sx: {
opacity: 0,
- position: "absolute",
- right: 0,
alignSelf: "start",
mr: 2,
- cursor: "pointer",
+ cursor: "grab",
".icon:hover path": {
- fill: "var(--disabled) !important",
+ fill: "var(--checked) !important",
},
- }, size: 20, onClick: function () {
- editor
- .chain()
- .setNodeSelection(getPos())
- .command(function (_a) {
- var tr = _a.tr;
- var pos = nestedTaskList.pos, node = nestedTaskList.node;
- tr.setNodeMarkup(pos, undefined, {
- collapsed: !node.attrs.collapsed,
- });
- return true;
- })
- .run();
- } }))] })) })) }));
+ }, size: 20 }), _jsx(Icon, { path: checked ? Icons.check : "", stroke: "1px", sx: {
+ border: "2px solid",
+ borderColor: checked ? "checked" : "icon",
+ borderRadius: "default",
+ alignSelf: "start",
+ mr: 2,
+ p: "1px",
+ cursor: "pointer",
+ ":hover": {
+ borderColor: "checked",
+ },
+ ":hover .icon path": {
+ fill: "var(--checked) !important",
+ },
+ }, onMouseEnter: function (e) {
+ if (e.buttons > 0) {
+ toggle();
+ }
+ }, onMouseDown: function (e) {
+ if (toggle())
+ e.preventDefault();
+ }, color: checked ? "checked" : "icon", size: 13 }), _jsx(NodeViewContent, { as: "li", style: {
+ listStyleType: "none",
+ textDecorationLine: checked ? "line-through" : "none",
+ color: checked ? "var(--checked)" : "var(--text)",
+ flex: 1,
+ } }), isNested && (_jsxs(_Fragment, { children: [isCollapsed && (_jsxs(Text, __assign({ variant: "body", sx: { color: "fontTertiary", mr: 35 } }, { children: [stats.checked, "/", stats.total] }))), _jsx(Icon, { className: "toggleSublist", path: nestedTaskList.node.attrs.collapsed
+ ? Icons.chevronDown
+ : Icons.chevronUp, sx: {
+ opacity: isCollapsed ? 1 : 0,
+ position: "absolute",
+ right: 0,
+ alignSelf: "start",
+ mr: 2,
+ cursor: "pointer",
+ ".icon:hover path": {
+ fill: "var(--checked) !important",
+ },
+ }, size: 20, onClick: function () {
+ editor
+ .chain()
+ .setNodeSelection(getPos())
+ .command(function (_a) {
+ var tr = _a.tr;
+ var pos = nestedTaskList.pos, node = nestedTaskList.node;
+ tr.setNodeMarkup(pos, undefined, {
+ collapsed: !node.attrs.collapsed,
+ });
+ return true;
+ })
+ .run();
+ } })] }))] })) })) }));
}
function toggleChildren(node, tr, toggleState, parentPos) {
var children = findChildren(node, function (node) { return node.type.name === "taskItem"; });
diff --git a/packages/editor/dist/extensions/tasklist/component.js b/packages/editor/dist/extensions/tasklist/component.js
index 49761bc3e..678dcb764 100644
--- a/packages/editor/dist/extensions/tasklist/component.js
+++ b/packages/editor/dist/extensions/tasklist/component.js
@@ -17,10 +17,11 @@ import { ThemeProvider } from "emotion-theming";
import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
import { useEffect, useState } from "react";
+import { Input } from "@rebass/forms";
export function TaskListComponent(props) {
- var editor = props.editor, getPos = props.getPos, node = props.node;
- var collapsed = node.attrs.collapsed;
- var _a = useState({ checked: 0, total: 0, percentage: 0 }), stats = _a[0], setStats = _a[1];
+ var editor = props.editor, getPos = props.getPos, node = props.node, updateAttributes = props.updateAttributes;
+ var _a = node.attrs, collapsed = _a.collapsed, title = _a.title;
+ var _b = useState({ checked: 0, total: 0, percentage: 0 }), stats = _b[0], setStats = _b[1];
var theme = editor.storage.theme;
var resolvedPos = editor.state.doc.resolve(getPos());
var parentTaskItem = findParentNodeClosestToPos(resolvedPos, function (node) { return node.type.name === "taskItem"; });
@@ -43,8 +44,7 @@ export function TaskListComponent(props) {
var percentage = Math.round((checked / total) * 100);
setStats({ checked: checked, total: total, percentage: percentage });
}, [nested, node]);
- console.log(collapsed);
- return (_jsx(NodeViewWrapper, __assign({ style: { display: collapsed ? "none" : "block" } }, { children: _jsx(ThemeProvider, __assign({ theme: theme }, { children: _jsxs(Flex, __assign({ sx: { flexDirection: "column" } }, { children: [nested ? null : (_jsxs(Flex, __assign({ sx: {
+ return (_jsxs(NodeViewWrapper, __assign({ style: { display: collapsed ? "none" : "block" } }, { children: [_jsx(ThemeProvider, __assign({ theme: theme }, { children: _jsx(Flex, __assign({ sx: { flexDirection: "column" } }, { children: nested ? null : (_jsxs(Flex, __assign({ sx: {
position: "relative",
bg: "bgSecondary",
py: 1,
@@ -53,7 +53,6 @@ export function TaskListComponent(props) {
alignItems: "center",
justifyContent: "end",
overflow: "hidden",
- px: 2,
} }, { children: [_jsx(Box, { sx: {
height: "100%",
width: "".concat(stats.percentage, "%"),
@@ -62,11 +61,13 @@ export function TaskListComponent(props) {
zIndex: 0,
left: 0,
transition: "width 250ms ease-out",
- } }), _jsxs(Flex, __assign({ sx: { zIndex: 1 } }, { children: [_jsx(Icon, { path: Icons.checkbox, size: 15 }), _jsxs(Text, __assign({ variant: "body", sx: { ml: 1 } }, { children: [stats.checked, "/", stats.total] }))] }))] }))), _jsx(NodeViewContent, { as: "ul", style: {
- paddingInlineStart: 0,
- marginBlockStart: nested ? 15 : 0,
- marginBlockEnd: 0,
- } })] })) })) })));
+ } }), _jsx(Input, { value: title, variant: "clean", sx: { p: 0, px: 2, zIndex: 1, color: "fontTertiary" }, placeholder: "Untitled", onChange: function (e) {
+ updateAttributes({ title: e.target.value });
+ } }), _jsxs(Flex, __assign({ sx: { flexShrink: 0, pr: 2 } }, { children: [_jsx(Icon, { path: Icons.checkbox, size: 15, color: "fontTertiary" }), _jsxs(Text, __assign({ variant: "body", sx: { ml: 1, color: "fontTertiary" } }, { children: [stats.checked, "/", stats.total] }))] }))] }))) })) })), _jsx(NodeViewContent, { as: "ul", style: {
+ paddingInlineStart: 0,
+ marginBlockStart: nested ? 15 : 0,
+ marginBlockEnd: 0,
+ } })] })));
}
function areAllChecked(node) {
var children = findChildren(node, function (node) { return node.type.name === "taskItem"; });
diff --git a/packages/editor/dist/extensions/tasklist/tasklist.js b/packages/editor/dist/extensions/tasklist/tasklist.js
index 34d533bc8..f57db22ce 100644
--- a/packages/editor/dist/extensions/tasklist/tasklist.js
+++ b/packages/editor/dist/extensions/tasklist/tasklist.js
@@ -12,6 +12,19 @@ export var TaskListNode = TaskList.extend({
"data-collapsed": attributes.collapsed === true,
}); },
},
+ title: {
+ default: null,
+ keepOnSplit: false,
+ parseHTML: function (element) { return element.dataset.title; },
+ renderHTML: function (attributes) {
+ if (!attributes.title || attributes.nested) {
+ return {};
+ }
+ return {
+ "data-title": attributes.title,
+ };
+ },
+ },
};
},
// renderHTML({ node, HTMLAttributes }) {
diff --git a/packages/editor/dist/index.d.ts b/packages/editor/dist/index.d.ts
index e85a96c93..42fca2c06 100644
--- a/packages/editor/dist/index.d.ts
+++ b/packages/editor/dist/index.d.ts
@@ -1,4 +1,3 @@
-/// <reference types="react" />
import { EditorOptions } from "@tiptap/react";
import Toolbar from "./toolbar";
import { ThemeConfig } from "@notesnook/theme/dist/theme/types";
diff --git a/packages/editor/dist/index.js b/packages/editor/dist/index.js
index 3631263a3..c9e814591 100644
--- a/packages/editor/dist/index.js
+++ b/packages/editor/dist/index.js
@@ -49,6 +49,7 @@ import { useTheme } from "@notesnook/theme";
import { AttachmentNode } from "./extensions/attachment";
import { TaskListNode } from "./extensions/task-list";
import { TaskItemNode } from "./extensions/task-item";
+import { Dropcursor } from "./extensions/drop-cursor";
EditorView.prototype.updateState = function updateState(state) {
if (!this.docView)
return; // This prevents the matchesNode error on hot reloads
@@ -61,9 +62,10 @@ var useTiptap = function (options, deps) {
extensions: [
TextStyle,
StarterKit.configure({
- dropcursor: {
- class: "drop-cursor",
- },
+ dropcursor: false,
+ }),
+ Dropcursor.configure({
+ class: "drop-cursor",
}),
CharacterCount,
Underline,
diff --git a/packages/editor/src/extensions/drop-cursor/drop-cursor.pm.ts b/packages/editor/src/extensions/drop-cursor/drop-cursor.pm.ts
new file mode 100644
index 000000000..10c014caf
--- /dev/null
+++ b/packages/editor/src/extensions/drop-cursor/drop-cursor.pm.ts
@@ -0,0 +1,207 @@
+import { Plugin, EditorState } from "prosemirror-state";
+import { dropPoint } from "prosemirror-transform";
+import { EditorView } from "prosemirror-view";
+import { DropcursorOptions } from "./drop-cursor";
+
+type Handler = (e: Event) => void;
+// :: (options: ?Object) → Plugin
+// Create a plugin that, when added to a ProseMirror instance,
+// causes a decoration to show up at the drop position when something
+// is dragged over the editor.
+//
+// Nodes may add a `disableDropCursor` property to their spec to
+// control the showing of a drop cursor inside them. This may be a
+// boolean or a function, which will be called with a view and a
+// position, and should return a boolean.
+//
+// options::- These options are supported:
+//
+// color:: ?string
+// The color of the cursor. Defaults to `black`.
+//
+// width:: ?number
+// The precise width of the cursor in pixels. Defaults to 1.
+//
+// class:: ?string
+// A CSS class name to add to the cursor element.
+export function dropCursor(options: DropcursorOptions = {}) {
+ return new Plugin({
+ view(editorView) {
+ return new DropCursorView(editorView, options);
+ },
+ });
+}
+
+class DropCursorView {
+ editorView: EditorView;
+ width: number;
+ color: string;
+ class: string;
+ handlers: { name: string; handler: Handler }[];
+ cursorPos: number | null;
+ timeout: number | null;
+ element: HTMLElement | null;
+
+ constructor(editorView: EditorView, options: DropcursorOptions) {
+ this.editorView = editorView;
+ this.width = options.width || 1;
+ this.color = options.color || "black";
+ this.class = options.class || "";
+ this.cursorPos = null;
+ this.element = null;
+ this.timeout = null;
+
+ this.handlers = ["dragover", "dragend", "drop", "dragleave"].map((name) => {
+ let handler: Handler = (e) => (<any>this)[name](e);
+ editorView.dom.addEventListener(name, handler);
+ return { name, handler };
+ });
+ }
+
+ destroy() {
+ this.handlers.forEach(({ name, handler }) =>
+ this.editorView.dom.removeEventListener(name, handler)
+ );
+ }
+
+ update(editorView: EditorView, prevState: EditorState) {
+ if (this.cursorPos != null && prevState.doc != editorView.state.doc) {
+ if (this.cursorPos > editorView.state.doc.content.size)
+ this.setCursor(null);
+ else this.updateOverlay();
+ }
+ }
+
+ setCursor(pos: number | null) {
+ if (pos == this.cursorPos) return;
+
+ this.cursorPos = pos;
+ if (pos == null && this.element && this.element.parentNode) {
+ this.element.parentNode.removeChild(this.element);
+ this.element = null;
+ } else {
+ this.updateOverlay();
+ }
+ }
+
+ updateOverlay() {
+ if (!this.cursorPos) return;
+
+ let $pos = this.editorView.state.doc.resolve(this.cursorPos),
+ rect;
+ if (!$pos.parent.inlineContent) {
+ let before = $pos.nodeBefore,
+ after = $pos.nodeAfter;
+ if (before || after) {
+ const node = this.editorView.nodeDOM(
+ this.cursorPos - (before ? before.nodeSize : 0)
+ ) as HTMLElement;
+ if (!node) return;
+ let nodeRect = node.getBoundingClientRect();
+
+ let top = before ? nodeRect.bottom : nodeRect.top;
+ if (before && after) {
+ const cursor = this.editorView.nodeDOM(this.cursorPos) as HTMLElement;
+ if (!cursor) return;
+ top = (top + cursor.getBoundingClientRect().top) / 2;
+ }
+ rect = {
+ left: nodeRect.left,
+ right: nodeRect.right,
+ top: top - this.width / 2,
+ bottom: top + this.width / 2,
+ };
+ }
+ }
+ if (!rect) {
+ let coords = this.editorView.coordsAtPos(this.cursorPos);
+ rect = {
+ left: coords.left - this.width / 2,
+ right: coords.left + this.width / 2,
+ top: coords.top,
+ bottom: coords.bottom,
+ };
+ }
+
+ let parent = (this.editorView.dom as HTMLElement).offsetParent;
+ if (!this.element && parent) {
+ this.element = parent.appendChild(document.createElement("div"));
+ if (this.class) this.element.className = this.class;
+ this.element.style.cssText =
+ "position: absolute; z-index: 50; pointer-events: none; background-color: " +
+ this.color;
+ }
+ let parentLeft, parentTop;
+ if (
+ !parent ||
+ (parent == document.body && getComputedStyle(parent).position == "static")
+ ) {
+ parentLeft = -pageXOffset;
+ parentTop = -pageYOffset;
+ } else {
+ let rect = parent.getBoundingClientRect();
+ parentLeft = rect.left - parent.scrollLeft;
+ parentTop = rect.top - parent.scrollTop;
+ }
+
+ if (!this.element) return;
+ this.element.style.left = rect.left - parentLeft + "px";
+ this.element.style.top = rect.top - parentTop + "px";
+ this.element.style.width = rect.right - rect.left + "px";
+ this.element.style.height = rect.bottom - rect.top + "px";
+ }
+
+ scheduleRemoval(timeout: number) {
+ clearTimeout(this.timeout || undefined);
+ this.timeout = (<unknown>(
+ setTimeout(() => this.setCursor(null), timeout)
+ )) as number;
+ }
+
+ dragover(event: DragEvent) {
+ if (!this.editorView.editable) return;
+ let pos = this.editorView.posAtCoords({
+ left: event.clientX,
+ top: event.clientY,
+ });
+
+ let node =
+ pos && pos.inside >= 0 && this.editorView.state.doc.nodeAt(pos.inside);
+ let disableDropCursor = node && node.type.spec.disableDropCursor;
+ let disabled =
+ typeof disableDropCursor == "function"
+ ? disableDropCursor(this.editorView, pos)
+ : disableDropCursor;
+
+ if (pos && !disabled) {
+ let target = pos.pos;
+ if (this.editorView.dragging && this.editorView.dragging.slice) {
+ const point = dropPoint(
+ this.editorView.state.doc,
+ target,
+ this.editorView.dragging.slice
+ );
+ if (!point) return this.setCursor(null);
+ target = point;
+ }
+ this.setCursor(target);
+ this.scheduleRemoval(5000);
+ }
+ }
+
+ dragend() {
+ this.scheduleRemoval(20);
+ }
+
+ drop() {
+ this.scheduleRemoval(20);
+ }
+
+ dragleave(event: DragEvent) {
+ if (
+ event.target == this.editorView.dom ||
+ !this.editorView.dom.contains(event.relatedTarget as Node)
+ )
+ this.setCursor(null);
+ }
+}
diff --git a/packages/editor/src/extensions/drop-cursor/drop-cursor.ts b/packages/editor/src/extensions/drop-cursor/drop-cursor.ts
new file mode 100644
index 000000000..797700be9
--- /dev/null
+++ b/packages/editor/src/extensions/drop-cursor/drop-cursor.ts
@@ -0,0 +1,24 @@
+import { Extension } from "@tiptap/core";
+import { dropCursor } from "./drop-cursor.pm";
+
+export interface DropcursorOptions {
+ color?: string | null;
+ width?: number | null;
+ class?: string | null;
+}
+
+export const Dropcursor = Extension.create<DropcursorOptions>({
+ name: "dropCursor",
+
+ addOptions() {
+ return {
+ color: "currentColor",
+ width: 1,
+ class: null,
+ };
+ },
+
+ addProseMirrorPlugins() {
+ return [dropCursor(this.options)];
+ },
+});
diff --git a/packages/editor/src/extensions/drop-cursor/index.ts b/packages/editor/src/extensions/drop-cursor/index.ts
new file mode 100644
index 000000000..e87a13a3e
--- /dev/null
+++ b/packages/editor/src/extensions/drop-cursor/index.ts
@@ -0,0 +1,5 @@
+import { Dropcursor } from "./drop-cursor";
+
+export * from "./drop-cursor";
+
+export default Dropcursor;
diff --git a/packages/editor/src/extensions/task-item/component.tsx b/packages/editor/src/extensions/task-item/component.tsx
index a07e6e9cf..6de00d352 100644
--- a/packages/editor/src/extensions/task-item/component.tsx
+++ b/packages/editor/src/extensions/task-item/component.tsx
@@ -16,22 +16,14 @@ import {
findChildren,
NodeWithPos,
} from "@tiptap/core";
-import { useCallback, useState } from "react";
+import { useCallback, useEffect, useState } from "react";
export function TaskItemComponent(props: ImageProps & NodeViewProps) {
const { checked } = props.node.attrs;
-
+ const [stats, setStats] = useState({ checked: 0, total: 0 });
const { editor, updateAttributes, node, getPos } = props;
- // const [isOpen, setIsOpen] = useState(true);
- // const elementRef = useRef<HTMLSpanElement>();
- // const isActive = editor.isActive("attachment", { hash });
- // const [isToolbarVisible, setIsToolbarVisible] = useState<boolean>();
const theme = editor.storage.theme as Theme;
- // useEffect(() => {
- // setIsToolbarVisible(isActive);
- // }, [isActive]);
-
const toggle = useCallback(() => {
if (!editor.isEditable) return false;
updateAttributes({ checked: !checked });
@@ -44,10 +36,26 @@ export function TaskItemComponent(props: ImageProps & NodeViewProps) {
editor.view.dispatch(tr);
return true;
}, [editor, getPos, node]);
+
const nestedTaskList = getChildren(node, getPos()).find(
({ node }) => node.type.name === "taskList"
);
const isNested = !!nestedTaskList;
+ const isCollapsed = nestedTaskList
+ ? nestedTaskList.node.attrs.collapsed
+ : false;
+
+ useEffect(() => {
+ if (!nestedTaskList) return;
+ const { pos, node } = nestedTaskList;
+ const children = findChildren(
+ node,
+ (node) => node.type.name === "taskItem"
+ );
+ const checked = children.filter(({ node }) => node.attrs.checked).length;
+ const total = children.length;
+ setStats({ checked, total });
+ }, [isNested, nestedTaskList, node]);
return (
<NodeViewWrapper>
@@ -60,107 +68,107 @@ export function TaskItemComponent(props: ImageProps & NodeViewProps) {
},
}}
>
- <Flex sx={{ flex: 1 }}>
- <Icon
- className="dragHandle"
- draggable="true"
- contentEditable={false}
- data-drag-handle
- path={Icons.dragHandle}
- sx={{
- opacity: 0,
- alignSelf: "start",
- mr: 2,
- cursor: "grab",
- ".icon:hover path": {
- fill: "var(--disabled) !important",
- },
- }}
- size={20}
- />
- <Icon
- path={checked ? Icons.check : ""}
- sx={{
- border: "2px solid",
- borderColor: checked ? "disabled" : "icon",
- borderRadius: "default",
- alignSelf: "start",
- mr: 2,
- p: "1px",
- cursor: "pointer",
- ":hover": {
- borderColor: "disabled",
- },
- ":hover .icon path": {
- fill: "var(--disabled) !important",
- },
- }}
- onMouseEnter={(e) => {
- if (e.buttons > 0) {
- toggle();
- }
- }}
- onMouseDown={(e) => {
- if (toggle()) e.preventDefault();
- }}
- color={checked ? "disabled" : "icon"}
- size={13}
- />
+ <Icon
+ className="dragHandle"
+ draggable="true"
+ contentEditable={false}
+ data-drag-handle
+ path={Icons.dragHandle}
+ sx={{
+ opacity: 0,
+ alignSelf: "start",
+ mr: 2,
+ cursor: "grab",
+ ".icon:hover path": {
+ fill: "var(--checked) !important",
+ },
+ }}
+ size={20}
+ />
+ <Icon
+ path={checked ? Icons.check : ""}
+ stroke="1px"
+ sx={{
+ border: "2px solid",
+ borderColor: checked ? "checked" : "icon",
+ borderRadius: "default",
+ alignSelf: "start",
+ mr: 2,
+ p: "1px",
+ cursor: "pointer",
+ ":hover": {
+ borderColor: "checked",
+ },
+ ":hover .icon path": {
+ fill: "var(--checked) !important",
+ },
+ }}
+ onMouseEnter={(e) => {
+ if (e.buttons > 0) {
+ toggle();
+ }
+ }}
+ onMouseDown={(e) => {
+ if (toggle()) e.preventDefault();
+ }}
+ color={checked ? "checked" : "icon"}
+ size={13}
+ />
+
+ <NodeViewContent
+ as={"li"}
+ style={{
+ listStyleType: "none",
+ textDecorationLine: checked ? "line-through" : "none",
+ color: checked ? "var(--checked)" : "var(--text)",
+ flex: 1,
+ }}
+ />
- <NodeViewContent
- as={"li"}
- style={{
- listStyleType: "none",
- textDecorationLine: checked ? "line-through" : "none",
- color: checked ? "var(--disabled)" : "var(--text)",
- flex: 1,
- }}
- />
- </Flex>
{isNested && (
- <Icon
- className="toggleSublist"
- path={
- nestedTaskList.node.attrs.collapsed
- ? Icons.chevronDown
- : Icons.chevronUp
- }
- sx={{
- opacity: 0,
- position: "absolute",
- right: 0,
- alignSelf: "start",
- mr: 2,
- cursor: "pointer",
- ".icon:hover path": {
- fill: "var(--disabled) !important",
- },
- }}
- size={20}
- onClick={() => {
- editor
- .chain()
- .setNodeSelection(getPos())
- .command(({ tr }) => {
- const { pos, node } = nestedTaskList;
- tr.setNodeMarkup(pos, undefined, {
- collapsed: !node.attrs.collapsed,
- });
- return true;
- })
- .run();
- }}
- />
+ <>
+ {isCollapsed && (
+ <Text variant={"body"} sx={{ color: "fontTertiary", mr: 35 }}>
+ {stats.checked}/{stats.total}
+ </Text>
+ )}
+ <Icon
+ className="toggleSublist"
+ path={
+ nestedTaskList.node.attrs.collapsed
+ ? Icons.chevronDown
+ : Icons.chevronUp
+ }
+ sx={{
+ opacity: isCollapsed ? 1 : 0,
+ position: "absolute",
+ right: 0,
+ alignSelf: "start",
+ mr: 2,
+ cursor: "pointer",
+ ".icon:hover path": {
+ fill: "var(--checked) !important",
+ },
+ }}
+ size={20}
+ onClick={() => {
+ editor
+ .chain()
+ .setNodeSelection(getPos())
+ .command(({ tr }) => {
+ const { pos, node } = nestedTaskList;
+ tr.setNodeMarkup(pos, undefined, {
+ collapsed: !node.attrs.collapsed,
+ });
+ return true;
+ })
+ .run();
+ }}
+ />
+ </>
)}
</Flex>
</ThemeProvider>
- {/*
- <Flex>
- <Box></Box>
-
- <Box contentEditable="true" />
- </Flex>
- </ThemeProvider> */}
</NodeViewWrapper>
);
}
diff --git a/packages/editor/src/extensions/task-list/component.tsx b/packages/editor/src/extensions/task-list/component.tsx
index 4da1b1ccd..f8e5b88d2 100644
--- a/packages/editor/src/extensions/task-list/component.tsx
+++ b/packages/editor/src/extensions/task-list/component.tsx
@@ -13,10 +13,11 @@ import { Theme } from "@notesnook/theme";
import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
import { useEffect, useState } from "react";
+import { Input } from "@rebass/forms";
export function TaskListComponent(props: NodeViewProps) {
- const { editor, getPos, node } = props;
- const { collapsed } = node.attrs;
+ const { editor, getPos, node, updateAttributes } = props;
+ const { collapsed, title } = node.attrs;
const [stats, setStats] = useState({ checked: 0, total: 0, percentage: 0 });
const theme = editor.storage.theme as Theme;
@@ -47,7 +48,7 @@ export function TaskListComponent(props: NodeViewProps) {
const percentage = Math.round((checked / total) * 100);
setStats({ checked, total, percentage });
}, [nested, node]);
- console.log(collapsed);
+
return (
<NodeViewWrapper style={{ display: collapsed ? "none" : "block" }}>
<ThemeProvider theme={theme}>
@@ -63,7 +64,6 @@ export function TaskListComponent(props: NodeViewProps) {
alignItems: "center",
justifyContent: "end",
overflow: "hidden",
- px: 2,
}}
>
<Box
@@ -78,27 +78,33 @@ export function TaskListComponent(props: NodeViewProps) {
transition: "width 250ms ease-out",
}}
/>
- <Flex sx={{ zIndex: 1 }}>
- <Icon path={Icons.checkbox} size={15} />
- <Text variant={"body"} sx={{ ml: 1 }}>
+ <Input
+ value={title}
+ variant={"clean"}
+ sx={{ p: 0, px: 2, zIndex: 1, color: "fontTertiary" }}
+ placeholder="Untitled"
+ onChange={(e) => {
+ updateAttributes({ title: e.target.value });
+ }}
+ />
+ <Flex sx={{ flexShrink: 0, pr: 2 }}>
+ <Icon path={Icons.checkbox} size={15} color="fontTertiary" />
+ <Text variant={"body"} sx={{ ml: 1, color: "fontTertiary" }}>
{stats.checked}/{stats.total}
</Text>
</Flex>
- {/* <Text variant={"body"} sx={{ zIndex: 1 }}>
- {stats.percentage}% done
- </Text> */}
</Flex>
)}
- <NodeViewContent
- as={"ul"}
- style={{
- paddingInlineStart: 0,
- marginBlockStart: nested ? 15 : 0,
- marginBlockEnd: 0,
- }}
- />
</Flex>
</ThemeProvider>
+ <NodeViewContent
+ as={"ul"}
+ style={{
+ paddingInlineStart: 0,
+ marginBlockStart: nested ? 15 : 0,
+ marginBlockEnd: 0,
+ }}
+ />
</NodeViewWrapper>
);
}
diff --git a/packages/editor/src/extensions/task-list/task-list.ts b/packages/editor/src/extensions/task-list/task-list.ts
index 796b7ef7d..b5658d841 100644
--- a/packages/editor/src/extensions/task-list/task-list.ts
+++ b/packages/editor/src/extensions/task-list/task-list.ts
@@ -15,6 +15,20 @@ export const TaskListNode = TaskList.extend({
"data-collapsed": attributes.collapsed === true,
}),
},
+ title: {
+ default: null,
+ keepOnSplit: false,
+ parseHTML: (element) => element.dataset.title,
+ renderHTML: (attributes) => {
+ if (!attributes.title || attributes.nested) {
+ return {};
+ }
+
+ return {
+ "data-title": attributes.title,
+ };
+ },
+ },
};
},
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 7ee0903f4..188d991f0 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -28,6 +28,7 @@ import { useTheme } from "@notesnook/theme";
import { AttachmentNode, AttachmentOptions } from "./extensions/attachment";
import { TaskListNode } from "./extensions/task-list";
import { TaskItemNode } from "./extensions/task-item";
+import { Dropcursor } from "./extensions/drop-cursor";
EditorView.prototype.updateState = function updateState(state) {
if (!(this as any).docView) return; // This prevents the matchesNode error on hot reloads
@@ -52,9 +53,10 @@ const useTiptap = (
extensions: [
TextStyle,
StarterKit.configure({
- dropcursor: {
- class: "drop-cursor",
- },
+ dropcursor: false,
+ }),
+ Dropcursor.configure({
+ class: "drop-cursor",
}),
CharacterCount,
Underline,
diff --git a/packages/editor/toolbar.md b/packages/editor/toolbar.md
index 84543e384..d57a22864 100644
--- a/packages/editor/toolbar.md
+++ b/packages/editor/toolbar.md
@@ -59,7 +59,7 @@ Total Tools:
- [x] subscript superscript
- [x] numlist
- [x] bullist
-- [] checklist
+- [x] checklist
- [x] forecolor backcolor
- [x] removeformat
- [x] hr