mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-16 19:57:52 +01:00
editor: add missing changes
This commit is contained in:
@@ -23,11 +23,15 @@ import { showLinkPopup } from "../../toolbar/popups/link-popup.js";
|
|||||||
import { isListActive } from "../../utils/list.js";
|
import { isListActive } from "../../utils/list.js";
|
||||||
import { tiptapKeys } from "@notesnook/common";
|
import { tiptapKeys } from "@notesnook/common";
|
||||||
import { isInTable } from "../table/prosemirror-tables/util.js";
|
import { isInTable } from "../table/prosemirror-tables/util.js";
|
||||||
import { findParentNodeOfType } from "../../utils/prosemirror.js";
|
import {
|
||||||
|
findParentNodeClosestToPos,
|
||||||
|
findParentNodeOfType
|
||||||
|
} from "../../utils/prosemirror.js";
|
||||||
import { Fragment, Node, Slice } from "@tiptap/pm/model";
|
import { Fragment, Node, Slice } from "@tiptap/pm/model";
|
||||||
import { ReplaceStep } from "@tiptap/pm/transform";
|
import { ReplaceStep } from "@tiptap/pm/transform";
|
||||||
import { Selection } from "@tiptap/pm/state";
|
import { Selection } from "@tiptap/pm/state";
|
||||||
import { Callout } from "../callout/callout.js";
|
import { Callout } from "../callout/callout.js";
|
||||||
|
import { Blockquote } from "../blockquote/blockquote.js";
|
||||||
|
|
||||||
export const KeyMap = Extension.create({
|
export const KeyMap = Extension.create({
|
||||||
name: "key-map",
|
name: "key-map",
|
||||||
@@ -78,10 +82,20 @@ export const KeyMap = Extension.create({
|
|||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
[tiptapKeys.moveLineUp.keys]: ({ editor }) => {
|
[tiptapKeys.moveLineUp.keys]: ({ editor }) => {
|
||||||
return moveNode(editor, "up");
|
try {
|
||||||
|
return moveNode(editor, "up");
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error moving node up:", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[tiptapKeys.moveLineDown.keys]: ({ editor }) => {
|
[tiptapKeys.moveLineDown.keys]: ({ editor }) => {
|
||||||
return moveNode(editor, "down");
|
try {
|
||||||
|
return moveNode(editor, "down");
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Error moving node down:", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -112,31 +126,45 @@ function moveNode(editor: Editor, dir: "up" | "down") {
|
|||||||
|
|
||||||
const { $from } = state.selection;
|
const { $from } = state.selection;
|
||||||
|
|
||||||
let currentResolved;
|
let targetType = $from.node().type;
|
||||||
let targetType;
|
let currentResolved = findParentNodeOfType(targetType)(state.selection);
|
||||||
|
|
||||||
const outlineListItem = findParentNodeOfType(
|
if (isListActive(editor)) {
|
||||||
editor.schema.nodes.outlineListItem
|
const currentNode = $from.node();
|
||||||
)(state.selection);
|
const parentNode = $from.node($from.depth - 1);
|
||||||
const taskItem = findParentNodeOfType(editor.schema.nodes.taskItem)(
|
const isFirstParagraph =
|
||||||
state.selection
|
currentNode.type.name === "paragraph" &&
|
||||||
);
|
parentNode.firstChild === currentNode;
|
||||||
const listItem = findParentNodeOfType(editor.schema.nodes.listItem)(
|
|
||||||
state.selection
|
// move the entire list item
|
||||||
);
|
if (isFirstParagraph) {
|
||||||
if (outlineListItem) {
|
targetType = $from.node($from.depth - 1).type;
|
||||||
currentResolved = outlineListItem;
|
if (
|
||||||
targetType = editor.schema.nodes.outlineListItem;
|
targetType.name === Callout.name ||
|
||||||
} else if (taskItem) {
|
targetType.name === Blockquote.name
|
||||||
currentResolved = taskItem;
|
) {
|
||||||
targetType = editor.schema.nodes.taskItem;
|
targetType = $from.node($from.depth - 2).type;
|
||||||
} else if (listItem) {
|
}
|
||||||
currentResolved = listItem;
|
}
|
||||||
targetType = editor.schema.nodes.listItem;
|
|
||||||
} else {
|
currentResolved = findParentNodeOfType(targetType)(state.selection);
|
||||||
const type = $from.node().type;
|
}
|
||||||
currentResolved = findParentNodeOfType(type)(state.selection);
|
|
||||||
targetType = type;
|
if (
|
||||||
|
findParentNodeClosestToPos($from, (node) => node.type.name === Callout.name)
|
||||||
|
) {
|
||||||
|
const currentNode = $from.node();
|
||||||
|
const parentNode = $from.node($from.depth - 1);
|
||||||
|
const isFirstHeading =
|
||||||
|
currentNode.type.name === "heading" &&
|
||||||
|
parentNode.firstChild === currentNode;
|
||||||
|
|
||||||
|
// move the entire callout
|
||||||
|
if (isFirstHeading) {
|
||||||
|
targetType = $from.node($from.depth - 1).type;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentResolved = findParentNodeOfType(targetType)(state.selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!currentResolved) {
|
if (!currentResolved) {
|
||||||
@@ -152,17 +180,6 @@ function moveNode(editor: Editor, dir: "up" | "down") {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
|
||||||
(targetType === editor.schema.nodes.outlineListItem &&
|
|
||||||
parent.type.name !== "outlineList") ||
|
|
||||||
(targetType === editor.schema.nodes.taskItem &&
|
|
||||||
parent.type.name !== "taskList") ||
|
|
||||||
(targetType === editor.schema.nodes.listItem &&
|
|
||||||
!["bulletList", "orderedList"].includes(parent.type.name))
|
|
||||||
) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let arr = mapChildren(parent, (node) => node);
|
let arr = mapChildren(parent, (node) => node);
|
||||||
let index = arr.indexOf(currentNode);
|
let index = arr.indexOf(currentNode);
|
||||||
let swapWith = isDown ? index + 1 : index - 1;
|
let swapWith = isDown ? index + 1 : index - 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user