Skip to content

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

fix: math notes cannot be inserted

thecodrr8032d3cd2aac
diff --git a/packages/editor/dist/es/extensions/math/mathinline.js b/packages/editor/dist/es/extensions/math/mathinline.js
index 731eb3d88..e1e3a3181 100644
--- a/packages/editor/dist/es/extensions/math/mathinline.js
+++ b/packages/editor/dist/es/extensions/math/mathinline.js
@@ -1,6 +1,6 @@
import { Node, mergeAttributes } from "@tiptap/core";
import { inputRules } from "prosemirror-inputrules";
-import { makeInlineMathInputRule, REGEX_INLINE_MATH_DOLLARS, mathPlugin, insertMathNode, } from "./plugin";
+import { makeInlineMathInputRule, REGEX_INLINE_MATH_DOLLARS, mathPlugin, } from "./plugin";
export const MathInline = Node.create({
name: "mathInline",
group: "inline math",
@@ -24,8 +24,12 @@ export const MathInline = Node.create({
},
addCommands() {
return {
- insertMathInline: () => ({ state, dispatch, view }) => {
- return insertMathNode(this.type)(state, dispatch, view);
+ insertMathInline: () => ({ chain, tr }) => {
+ const { $from } = tr.selection;
+ return chain()
+ .insertContent({ type: this.name, attrs: {} })
+ .setNodeSelection($from.pos)
+ .run();
},
};
},
diff --git a/packages/editor/dist/es/extensions/math/plugin/commands/insertmathnode.js b/packages/editor/dist/es/extensions/math/plugin/commands/insertmathnode.js
index c0722ccb5..215ccdb1d 100644
--- a/packages/editor/dist/es/extensions/math/plugin/commands/insertmathnode.js
+++ b/packages/editor/dist/es/extensions/math/plugin/commands/insertmathnode.js
@@ -1,4 +1,4 @@
-import { NodeSelection } from "prosemirror-state";
+import { NodeSelection, TextSelection, } from "prosemirror-state";
////////////////////////////////////////////////////////////////////////////////
/**
* Returns a new command that can be used to inserts a new math node at the
@@ -11,14 +11,19 @@ import { NodeSelection } from "prosemirror-state";
*/
export function insertMathNode(mathNodeType, initialText = "") {
return function (state, dispatch) {
- let { $from } = state.selection, index = $from.index();
- if (!$from.parent.canReplaceWith(index, index, mathNodeType)) {
+ let { $from, empty } = state.selection, index = $from.index();
+ if (!empty && !$from.parent.canReplaceWith(index, index, mathNodeType)) {
return false;
}
if (dispatch) {
let mathNode = mathNodeType.create({}, initialText ? state.schema.text(initialText) : null);
let tr = state.tr.replaceSelectionWith(mathNode);
- tr = tr.setSelection(NodeSelection.create(tr.doc, $from.pos));
+ if (empty) {
+ tr = tr.setSelection(TextSelection.create(tr.doc, $from.pos + 1));
+ }
+ else {
+ tr = tr.setSelection(NodeSelection.create(tr.doc, $from.pos));
+ }
dispatch(tr);
}
return true;
diff --git a/packages/editor/dist/es/extensions/math/plugin/math-nodeview.js b/packages/editor/dist/es/extensions/math/plugin/math-nodeview.js
index 5777aec1f..a07bc52f2 100644
--- a/packages/editor/dist/es/extensions/math/plugin/math-nodeview.js
+++ b/packages/editor/dist/es/extensions/math/plugin/math-nodeview.js
@@ -253,7 +253,12 @@ export class MathView {
let prevCursorPos = maybePos !== null && maybePos !== void 0 ? maybePos : 0;
// compute position that cursor should appear within the expanded math node
let innerPos = prevCursorPos <= this._getPos() ? 0 : this._node.nodeSize - 2;
- this._innerView.dispatch(innerState.tr.setSelection(TextSelection.create(innerState.doc, innerPos)));
+ setTimeout(() => {
+ if (!this._innerView)
+ return;
+ this._innerView.focus();
+ this._innerView.dispatch(innerState.tr.setSelection(TextSelection.create(innerState.doc, innerPos)));
+ });
this._isEditing = true;
}
/**
diff --git a/packages/editor/src/extensions/math/math-inline.ts b/packages/editor/src/extensions/math/math-inline.ts
index 1349fc728..03695ed2d 100644
--- a/packages/editor/src/extensions/math/math-inline.ts
+++ b/packages/editor/src/extensions/math/math-inline.ts
@@ -43,8 +43,12 @@ export const MathInline = Node.create({
return {
insertMathInline:
() =>
- ({ state, dispatch, view }) => {
- return insertMathNode(this.type)(state, dispatch, view);
+ ({ chain, tr }) => {
+ const { $from } = tr.selection;
+ return chain()
+ .insertContent({ type: this.name, attrs: {} })
+ .setNodeSelection($from.pos)
+ .run();
},
};
},
diff --git a/packages/editor/src/extensions/math/plugin/commands/insert-math-node.ts b/packages/editor/src/extensions/math/plugin/commands/insert-math-node.ts
index f7fd383e1..139e00797 100644
--- a/packages/editor/src/extensions/math/plugin/commands/insert-math-node.ts
+++ b/packages/editor/src/extensions/math/plugin/commands/insert-math-node.ts
@@ -1,6 +1,11 @@
import { Command } from "prosemirror-state";
import { NodeType } from "prosemirror-model";
-import { EditorState, NodeSelection, Transaction } from "prosemirror-state";
+import {
+ EditorState,
+ NodeSelection,
+ Transaction,
+ TextSelection,
+} from "prosemirror-state";
////////////////////////////////////////////////////////////////////////////////
@@ -21,9 +26,9 @@ export function insertMathNode(
state: EditorState,
dispatch: ((tr: Transaction) => void) | undefined
) {
- let { $from } = state.selection,
+ let { $from, empty } = state.selection,
index = $from.index();
- if (!$from.parent.canReplaceWith(index, index, mathNodeType)) {
+ if (!empty && !$from.parent.canReplaceWith(index, index, mathNodeType)) {
return false;
}
if (dispatch) {
@@ -32,7 +37,11 @@ export function insertMathNode(
initialText ? state.schema.text(initialText) : null
);
let tr = state.tr.replaceSelectionWith(mathNode);
- tr = tr.setSelection(NodeSelection.create(tr.doc, $from.pos));
+ if (empty) {
+ tr = tr.setSelection(TextSelection.create(tr.doc, $from.pos + 1));
+ } else {
+ tr = tr.setSelection(NodeSelection.create(tr.doc, $from.pos));
+ }
dispatch(tr);
}
return true;
diff --git a/packages/editor/src/extensions/math/plugin/math-node-view.ts b/packages/editor/src/extensions/math/plugin/math-node-view.ts
index dd91d4fea..81a3b8c78 100644
--- a/packages/editor/src/extensions/math/plugin/math-node-view.ts
+++ b/packages/editor/src/extensions/math/plugin/math-node-view.ts
@@ -373,9 +373,16 @@ export class MathView implements NodeView, ICursorPosObserver {
let innerPos =
prevCursorPos <= this._getPos() ? 0 : this._node.nodeSize - 2;
- this._innerView.dispatch(
- innerState.tr.setSelection(TextSelection.create(innerState.doc, innerPos))
- );
+ setTimeout(() => {
+ if (!this._innerView) return;
+
+ this._innerView.focus();
+ this._innerView.dispatch(
+ innerState.tr.setSelection(
+ TextSelection.create(innerState.doc, innerPos)
+ )
+ );
+ });
this._isEditing = true;
}