Skip to content

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

editor: infer text direction from parent before insertion

Abdullah Atta93c1a44285e5
diff --git a/packages/editor/src/extensions/blockquote/blockquote.ts b/packages/editor/src/extensions/blockquote/blockquote.ts
new file mode 100644
index 000000000..0d29504f2
--- /dev/null
+++ b/packages/editor/src/extensions/blockquote/blockquote.ts
@@ -0,0 +1,51 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { getParentAttributes } from "../../utils/prosemirror";
+import { wrappingInputRule } from "@tiptap/core";
+import TiptapBlockquote, { inputRegex } from "@tiptap/extension-blockquote";
+
+export const Blockquote = TiptapBlockquote.extend({
+ addCommands() {
+ return {
+ setBlockquote:
+ () =>
+ ({ commands }) => {
+ return commands.wrapIn(this.name, getParentAttributes(this.editor));
+ },
+ toggleBlockquote:
+ () =>
+ ({ commands }) => {
+ return commands.toggleWrap(
+ this.name,
+ getParentAttributes(this.editor)
+ );
+ }
+ };
+ },
+
+ addInputRules() {
+ return [
+ wrappingInputRule({
+ find: inputRegex,
+ type: this.type,
+ getAttributes: () => getParentAttributes(this.editor)
+ })
+ ];
+ }
+});
diff --git a/packages/editor/src/extensions/blockquote/index.ts b/packages/editor/src/extensions/blockquote/index.ts
new file mode 100644
index 000000000..1715492ff
--- /dev/null
+++ b/packages/editor/src/extensions/blockquote/index.ts
@@ -0,0 +1,24 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { Blockquote } from "./blockquote";
+
+export * from "./blockquote";
+
+export default Blockquote;
diff --git a/packages/editor/src/extensions/bullet-list/bullet-list.ts b/packages/editor/src/extensions/bullet-list/bullet-list.ts
index 9acf29dd2..dfe9b4952 100644
--- a/packages/editor/src/extensions/bullet-list/bullet-list.ts
+++ b/packages/editor/src/extensions/bullet-list/bullet-list.ts
@@ -18,7 +18,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import TiptapBulletList from "@tiptap/extension-bullet-list";
+import { wrappingInputRule } from "@tiptap/core";
+import { getParentAttributes } from "../../utils/prosemirror";
+export const inputRegex = /^\s*([-+*])\s$/;
export const BulletList = TiptapBulletList.extend({
addAttributes() {
return {
@@ -37,5 +40,43 @@ export const BulletList = TiptapBulletList.extend({
}
}
};
+ },
+ addCommands() {
+ return {
+ toggleBulletList:
+ () =>
+ ({ chain }) => {
+ return chain()
+ .toggleList(
+ this.name,
+ this.options.itemTypeName,
+ this.options.keepMarks,
+ getParentAttributes(
+ this.editor,
+ this.options.keepMarks,
+ this.options.keepAttributes
+ )
+ )
+ .run();
+ }
+ };
+ },
+ addInputRules() {
+ return [
+ wrappingInputRule({
+ find: inputRegex,
+ type: this.type,
+ keepMarks: this.options.keepMarks,
+ keepAttributes: this.options.keepAttributes,
+ editor: this.editor,
+ getAttributes: () => {
+ return getParentAttributes(
+ this.editor,
+ this.options.keepMarks,
+ this.options.keepAttributes
+ );
+ }
+ })
+ ];
}
});
diff --git a/packages/editor/src/extensions/ordered-list/ordered-list.ts b/packages/editor/src/extensions/ordered-list/ordered-list.ts
index 7a45a1f39..261c14cdc 100644
--- a/packages/editor/src/extensions/ordered-list/ordered-list.ts
+++ b/packages/editor/src/extensions/ordered-list/ordered-list.ts
@@ -17,7 +17,9 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import TiptapOrderedList from "@tiptap/extension-ordered-list";
+import { wrappingInputRule } from "@tiptap/core";
+import TiptapOrderedList, { inputRegex } from "@tiptap/extension-ordered-list";
+import { getParentAttributes } from "../../utils/prosemirror";
export const OrderedList = TiptapOrderedList.extend({
addAttributes() {
@@ -37,5 +39,50 @@ export const OrderedList = TiptapOrderedList.extend({
}
}
};
+ },
+
+ addCommands() {
+ return {
+ toggleOrderedList:
+ () =>
+ ({ chain }) => {
+ return chain()
+ .toggleList(
+ this.name,
+ this.options.itemTypeName,
+ this.options.keepMarks,
+ getParentAttributes(
+ this.editor,
+ this.options.keepMarks,
+ this.options.keepAttributes
+ )
+ )
+ .run();
+ }
+ };
+ },
+
+ addInputRules() {
+ return [
+ wrappingInputRule({
+ find: inputRegex,
+ type: this.type,
+ keepMarks: this.options.keepMarks,
+ keepAttributes: this.options.keepAttributes,
+ editor: this.editor,
+ joinPredicate: (match, node) =>
+ node.childCount + node.attrs.start === +match[1],
+ getAttributes: (match) => {
+ return {
+ ...getParentAttributes(
+ this.editor,
+ this.options.keepMarks,
+ this.options.keepAttributes
+ ),
+ start: +match[1]
+ };
+ }
+ })
+ ];
}
});
diff --git a/packages/editor/src/extensions/outline-list/outline-list.ts b/packages/editor/src/extensions/outline-list/outline-list.ts
index 28dbd1a36..f11f7051c 100644
--- a/packages/editor/src/extensions/outline-list/outline-list.ts
+++ b/packages/editor/src/extensions/outline-list/outline-list.ts
@@ -17,8 +17,8 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { getParentAttributes } from "../../utils/prosemirror";
import { Node, mergeAttributes, wrappingInputRule } from "@tiptap/core";
-import TextStyle from "@tiptap/extension-text-style";
export type OutlineListAttributes = {
collapsed: boolean;
@@ -81,26 +81,19 @@ export const OutlineList = Node.create<OutlineListOptions>({
return {
toggleOutlineList:
() =>
- ({ commands, chain }) => {
- if (this.options.keepAttributes) {
- return chain()
- .toggleList(
- this.name,
- outlineListItemName,
- this.options.keepMarks
+ ({ chain }) => {
+ return chain()
+ .toggleList(
+ this.name,
+ outlineListItemName,
+ this.options.keepMarks,
+ getParentAttributes(
+ this.editor,
+ this.options.keepMarks,
+ this.options.keepAttributes
)
- .updateAttributes(
- outlineListItemName,
- this.editor.getAttributes(TextStyle.name)
- )
- .run();
- }
-
- return commands.toggleList(
- this.name,
- outlineListItemName,
- this.options.keepMarks
- );
+ )
+ .run();
}
};
},
@@ -110,29 +103,24 @@ export const OutlineList = Node.create<OutlineListOptions>({
"Mod-Shift-O": () => this.editor.commands.toggleOutlineList()
};
},
-
addInputRules() {
- let inputRule = wrappingInputRule({
- find: inputRegex,
- type: this.type
- });
-
- if (this.options.keepMarks || this.options.keepAttributes) {
- inputRule = wrappingInputRule({
+ return [
+ wrappingInputRule({
find: inputRegex,
type: this.type,
keepMarks: this.options.keepMarks,
keepAttributes: this.options.keepAttributes,
+ editor: this.editor,
getAttributes: () => {
- return this.editor.getAttributes(TextStyle.name);
- },
- editor: this.editor
- });
- }
-
- return [inputRule];
+ return getParentAttributes(
+ this.editor,
+ this.options.keepMarks,
+ this.options.keepAttributes
+ );
+ }
+ })
+ ];
},
-
addNodeView() {
return ({ node }) => {
const ul = document.createElement("ul");
diff --git a/packages/editor/src/extensions/task-item/component.tsx b/packages/editor/src/extensions/task-item/component.tsx
index d469c354c..1a9844576 100644
--- a/packages/editor/src/extensions/task-item/component.tsx
+++ b/packages/editor/src/extensions/task-item/component.tsx
@@ -105,7 +105,7 @@ export function TaskItemComponent(
alignSelf: "start",
p: "1px",
mt: "0.40ch",
- mr: 1,
+ marginInlineEnd: 1,
cursor: editor.isEditable ? "pointer" : "unset",
":hover": {
borderColor: "accent"
diff --git a/packages/editor/src/extensions/task-item/task-item.ts b/packages/editor/src/extensions/task-item/task-item.ts
index b2b4c09fc..4d21cb788 100644
--- a/packages/editor/src/extensions/task-item/task-item.ts
+++ b/packages/editor/src/extensions/task-item/task-item.ts
@@ -92,5 +92,9 @@ export const TaskItemNode = TaskItem.extend({
return prev.checked !== next.checked;
}
});
+ },
+
+ addInputRules() {
+ return [];
}
});
diff --git a/packages/editor/src/extensions/task-list/task-list.ts b/packages/editor/src/extensions/task-list/task-list.ts
index ac5369c25..897aeeb79 100644
--- a/packages/editor/src/extensions/task-list/task-list.ts
+++ b/packages/editor/src/extensions/task-list/task-list.ts
@@ -17,16 +17,19 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-import { mergeAttributes } from "@tiptap/core";
+import { mergeAttributes, wrappingInputRule } from "@tiptap/core";
import { TaskList } from "@tiptap/extension-task-list";
import { createNodeView } from "../react";
import { TaskListComponent } from "./component";
import { Plugin, PluginKey, NodeSelection } from "prosemirror-state";
-import TaskItem from "@tiptap/extension-task-item";
+import TaskItem, { inputRegex } from "@tiptap/extension-task-item";
import { dropPoint } from "prosemirror-transform";
-import { findChildrenByType, hasSameAttributes } from "../../utils/prosemirror";
+import {
+ findChildrenByType,
+ getParentAttributes,
+ hasSameAttributes
+} from "../../utils/prosemirror";
import { countCheckedItems } from "./utils";
-import TextStyle from "@tiptap/extension-text-style";
export type TaskListAttributes = {
title: string;
@@ -57,7 +60,7 @@ export const TaskListNode = TaskList.extend({
return [
{
tag: "ul",
- getAttrs: (node: any) => {
+ getAttrs: (node) => {
if (node instanceof Node && node instanceof HTMLElement) {
return node.classList.contains("checklist") && null;
}
@@ -89,11 +92,8 @@ export const TaskListNode = TaskList.extend({
.toggleList(
this.name,
this.options.itemTypeName,
- true // TODO
- )
- .updateAttributes(
- this.options.itemTypeName,
- this.editor.getAttributes(TextStyle.name)
+ true,
+ getParentAttributes(this.editor, true, true)
)
.run();
@@ -195,6 +195,25 @@ export const TaskListNode = TaskList.extend({
}
})
];
+ },
+
+ addInputRules() {
+ const inputRule = wrappingInputRule({
+ find: inputRegex,
+ type: this.type,
+ getAttributes: () => {
+ return getParentAttributes(this.editor, true, true);
+ }
+ });
+ const oldHandler = inputRule.handler;
+ inputRule.handler = ({ state, range, match, chain, can, commands }) => {
+ oldHandler({ state, range, match, chain, can, commands });
+
+ state.tr.setNodeMarkup(state.tr.selection.to - 2, undefined, {
+ checked: match[match.length - 1] === "x"
+ });
+ };
+ return [inputRule];
}
});
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 5beb82dfa..1672cc183 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -77,6 +77,7 @@ import { DownloadOptions } from "./utils/downloader";
import { Heading } from "./extensions/heading";
import Clipboard, { ClipboardOptions } from "./extensions/clipboard";
import { convertBrToParagraph } from "./utils/html";
+import Blockquote from "./extensions/blockquote";
declare global {
// eslint-disable-next-line no-var
@@ -183,6 +184,7 @@ const useTiptap = (
paragraph: false,
hardBreak: false,
heading: false,
+ blockquote: false,
history: {
depth: 200,
newGroupDelay: 1000
@@ -211,6 +213,7 @@ const useTiptap = (
];
}
}),
+ Blockquote,
CharacterCount,
Underline,
Subscript,
diff --git a/packages/editor/src/utils/prosemirror.ts b/packages/editor/src/utils/prosemirror.ts
index c37bafc3f..b3e7c0ad0 100644
--- a/packages/editor/src/utils/prosemirror.ts
+++ b/packages/editor/src/utils/prosemirror.ts
@@ -42,6 +42,7 @@ import { OutlineListItem } from "../extensions/outline-list-item";
import { TaskItemNode } from "../extensions/task-item";
import { TaskListNode } from "../extensions/task-list";
import { LIST_NODE_TYPES } from "./node-types";
+import TextStyle from "@tiptap/extension-text-style";
export type NodeWithOffset = {
node?: ProsemirrorNode;
@@ -178,6 +179,22 @@ export function findParentNodeOfType(nodeType: NodeType | NodeType[]) {
return findParentNode((node) => equalNodeType(nodeType, node));
}
+export function getParentAttributes(
+ editor: Editor,
+ keepMarks = false,
+ keepAttributes = false
+) {
+ const { textAlign, textDirection } =
+ editor.state.selection.$from.parent?.attrs || {};
+ return {
+ ...(keepMarks || keepAttributes
+ ? editor.getAttributes(TextStyle.name)
+ : {}),
+ textAlign,
+ textDirection
+ };
+}
+
const walkNode = (node: ProsemirrorNode, descend = true) => {
if (!node) {
throw new Error('Invalid "node" parameter');
diff --git a/packages/editor/styles/styles.css b/packages/editor/styles/styles.css
index e4a27598a..b95052ee2 100644
--- a/packages/editor/styles/styles.css
+++ b/packages/editor/styles/styles.css
@@ -598,3 +598,5 @@ blockquote[dir="rtl"] {
.outline-list[dir="rtl"] li.collapsed > p::before {
transform: rotate(90deg);
}
+
+[dir="rtl"] .taskItemTools { right: unset; left: 0 }
\ No newline at end of file