@formulaxjs/tiptap 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -0
- package/README.zh-CN.md +22 -0
- package/dist/index.cjs +134 -122
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +134 -122
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +134 -122
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.global.js
CHANGED
|
@@ -31222,6 +31222,7 @@ var FormulaX = (() => {
|
|
|
31222
31222
|
var FORMULAX_NODE_NAME = "formulaX";
|
|
31223
31223
|
function resolveOptions(options = {}) {
|
|
31224
31224
|
return {
|
|
31225
|
+
name: options.name ?? FORMULAX_NODE_NAME,
|
|
31225
31226
|
formulaClassName: options.formulaClassName ?? DEFAULT_FORMULA_CLASS,
|
|
31226
31227
|
formulaAttributeName: options.formulaAttributeName ?? DEFAULT_FORMULA_ATTRIBUTE,
|
|
31227
31228
|
cursorStyle: options.cursorStyle ?? "pointer",
|
|
@@ -31243,140 +31244,151 @@ var FormulaX = (() => {
|
|
|
31243
31244
|
}
|
|
31244
31245
|
};
|
|
31245
31246
|
}
|
|
31246
|
-
|
|
31247
|
-
|
|
31248
|
-
|
|
31249
|
-
|
|
31250
|
-
|
|
31251
|
-
|
|
31252
|
-
|
|
31253
|
-
|
|
31254
|
-
|
|
31255
|
-
|
|
31256
|
-
|
|
31257
|
-
|
|
31258
|
-
|
|
31259
|
-
|
|
31260
|
-
|
|
31261
|
-
|
|
31262
|
-
|
|
31263
|
-
|
|
31247
|
+
function warnDuplicateNodeName(extension) {
|
|
31248
|
+
const extensions = extension.editor?.extensionManager?.extensions ?? [];
|
|
31249
|
+
const duplicates = extensions.filter((item) => item?.name === extension.name);
|
|
31250
|
+
if (duplicates.length > 1) {
|
|
31251
|
+
console.warn(
|
|
31252
|
+
`[FormulaX] TipTap node name "${extension.name}" is already registered. Pass a unique "name" option to avoid schema collisions.`
|
|
31253
|
+
);
|
|
31254
|
+
}
|
|
31255
|
+
}
|
|
31256
|
+
function createFormulaXNodeConfig(options) {
|
|
31257
|
+
return {
|
|
31258
|
+
name: options.name,
|
|
31259
|
+
group: "inline",
|
|
31260
|
+
inline: true,
|
|
31261
|
+
atom: true,
|
|
31262
|
+
selectable: true,
|
|
31263
|
+
addOptions() {
|
|
31264
|
+
return options;
|
|
31265
|
+
},
|
|
31266
|
+
onCreate() {
|
|
31267
|
+
warnDuplicateNodeName(this);
|
|
31268
|
+
if (typeof document !== "undefined") {
|
|
31269
|
+
ensureFormulaXModalStyles(document);
|
|
31264
31270
|
}
|
|
31265
|
-
}
|
|
31266
|
-
|
|
31267
|
-
|
|
31268
|
-
|
|
31269
|
-
|
|
31270
|
-
getAttrs: (element) => {
|
|
31271
|
-
if (!(element instanceof HTMLElement)) {
|
|
31272
|
-
return false;
|
|
31271
|
+
},
|
|
31272
|
+
addAttributes() {
|
|
31273
|
+
return {
|
|
31274
|
+
latex: {
|
|
31275
|
+
default: ""
|
|
31273
31276
|
}
|
|
31274
|
-
|
|
31275
|
-
|
|
31276
|
-
|
|
31277
|
-
|
|
31278
|
-
|
|
31279
|
-
|
|
31280
|
-
|
|
31281
|
-
|
|
31282
|
-
return [
|
|
31283
|
-
"span",
|
|
31284
|
-
{
|
|
31285
|
-
"data-formulax": "true",
|
|
31286
|
-
[this.options.formulaAttributeName]: node.attrs.latex,
|
|
31287
|
-
"data-latex": node.attrs.latex
|
|
31288
|
-
},
|
|
31289
|
-
node.attrs.latex || "\\square"
|
|
31290
|
-
];
|
|
31291
|
-
}
|
|
31292
|
-
return createFormulaDomElement(document, node.attrs, this.options);
|
|
31293
|
-
},
|
|
31294
|
-
addCommands() {
|
|
31295
|
-
return {
|
|
31296
|
-
openFormulaX: () => () => {
|
|
31297
|
-
const selectedFormula = getSelectedFormula(this.editor);
|
|
31298
|
-
const initialLatex = selectedFormula?.attrs.latex ?? this.options.initialLatex;
|
|
31299
|
-
void openFormulaXTiptapModal({
|
|
31300
|
-
initialLatex,
|
|
31301
|
-
isUpdate: Boolean(selectedFormula),
|
|
31302
|
-
options: this.options
|
|
31303
|
-
}).then((payload) => {
|
|
31304
|
-
if (!payload) {
|
|
31305
|
-
return;
|
|
31277
|
+
};
|
|
31278
|
+
},
|
|
31279
|
+
parseHTML() {
|
|
31280
|
+
return [{
|
|
31281
|
+
tag: "span[data-formulax]",
|
|
31282
|
+
getAttrs: (element) => {
|
|
31283
|
+
if (!(element instanceof HTMLElement)) {
|
|
31284
|
+
return false;
|
|
31306
31285
|
}
|
|
31307
|
-
|
|
31308
|
-
|
|
31309
|
-
|
|
31310
|
-
return true;
|
|
31311
|
-
}
|
|
31312
|
-
};
|
|
31313
|
-
},
|
|
31314
|
-
addKeyboardShortcuts() {
|
|
31315
|
-
return {
|
|
31316
|
-
Enter: () => {
|
|
31317
|
-
if (!getSelectedFormula(this.editor)) {
|
|
31318
|
-
return false;
|
|
31319
|
-
}
|
|
31320
|
-
return this.editor.commands.openFormulaX();
|
|
31321
|
-
},
|
|
31322
|
-
Space: () => {
|
|
31323
|
-
if (!getSelectedFormula(this.editor)) {
|
|
31324
|
-
return false;
|
|
31286
|
+
return {
|
|
31287
|
+
latex: getFormulaLatexFromElement(element, this.options.formulaAttributeName)
|
|
31288
|
+
};
|
|
31325
31289
|
}
|
|
31326
|
-
|
|
31290
|
+
}];
|
|
31291
|
+
},
|
|
31292
|
+
renderHTML({ node }) {
|
|
31293
|
+
if (typeof document === "undefined") {
|
|
31294
|
+
return [
|
|
31295
|
+
"span",
|
|
31296
|
+
{
|
|
31297
|
+
"data-formulax": "true",
|
|
31298
|
+
[this.options.formulaAttributeName]: node.attrs.latex,
|
|
31299
|
+
"data-latex": node.attrs.latex
|
|
31300
|
+
},
|
|
31301
|
+
node.attrs.latex || "\\square"
|
|
31302
|
+
];
|
|
31327
31303
|
}
|
|
31328
|
-
|
|
31329
|
-
|
|
31330
|
-
|
|
31331
|
-
|
|
31332
|
-
|
|
31333
|
-
|
|
31334
|
-
|
|
31335
|
-
|
|
31336
|
-
|
|
31337
|
-
|
|
31338
|
-
|
|
31304
|
+
return createFormulaDomElement(document, node.attrs, this.options);
|
|
31305
|
+
},
|
|
31306
|
+
addCommands() {
|
|
31307
|
+
return {
|
|
31308
|
+
openFormulaX: () => () => {
|
|
31309
|
+
const selectedFormula = getSelectedFormula(this.editor, this.name);
|
|
31310
|
+
const initialLatex = selectedFormula?.attrs.latex ?? this.options.initialLatex;
|
|
31311
|
+
void openFormulaXTiptapModal({
|
|
31312
|
+
initialLatex,
|
|
31313
|
+
isUpdate: Boolean(selectedFormula),
|
|
31314
|
+
options: this.options
|
|
31315
|
+
}).then((payload) => {
|
|
31316
|
+
if (!payload) {
|
|
31317
|
+
return;
|
|
31318
|
+
}
|
|
31319
|
+
applyFormulaPayload(this.editor, payload, selectedFormula, this.name);
|
|
31320
|
+
this.editor.commands.focus();
|
|
31321
|
+
});
|
|
31322
|
+
return true;
|
|
31339
31323
|
}
|
|
31340
|
-
editor.commands.setNodeSelection?.(position);
|
|
31341
31324
|
};
|
|
31342
|
-
|
|
31343
|
-
|
|
31344
|
-
selectNode();
|
|
31345
|
-
});
|
|
31346
|
-
dom.addEventListener("dblclick", (event2) => {
|
|
31347
|
-
event2.preventDefault();
|
|
31348
|
-
event2.stopPropagation();
|
|
31349
|
-
selectNode();
|
|
31350
|
-
editor.commands.openFormulaX();
|
|
31351
|
-
});
|
|
31325
|
+
},
|
|
31326
|
+
addKeyboardShortcuts() {
|
|
31352
31327
|
return {
|
|
31353
|
-
|
|
31354
|
-
|
|
31355
|
-
if (updatedNode.type.name !== FORMULAX_NODE_NAME) {
|
|
31328
|
+
Enter: () => {
|
|
31329
|
+
if (!getSelectedFormula(this.editor, this.name)) {
|
|
31356
31330
|
return false;
|
|
31357
31331
|
}
|
|
31358
|
-
|
|
31359
|
-
void renderFormulaIntoElement(dom, updatedNode.attrs, this.options);
|
|
31360
|
-
return true;
|
|
31361
|
-
},
|
|
31362
|
-
selectNode: () => {
|
|
31363
|
-
dom.classList.add("ProseMirror-selectednode");
|
|
31332
|
+
return this.editor.commands.openFormulaX();
|
|
31364
31333
|
},
|
|
31365
|
-
|
|
31366
|
-
|
|
31334
|
+
Space: () => {
|
|
31335
|
+
if (!getSelectedFormula(this.editor, this.name)) {
|
|
31336
|
+
return false;
|
|
31337
|
+
}
|
|
31338
|
+
return this.editor.commands.openFormulaX();
|
|
31367
31339
|
}
|
|
31368
31340
|
};
|
|
31369
|
-
}
|
|
31370
|
-
|
|
31371
|
-
|
|
31341
|
+
},
|
|
31342
|
+
addNodeView() {
|
|
31343
|
+
return ({ editor, getPos, node }) => {
|
|
31344
|
+
const dom = createFormulaDomElement(document, node.attrs, this.options) ?? document.createElement("span");
|
|
31345
|
+
dom.classList.add("formulax-math--interactive");
|
|
31346
|
+
void renderFormulaIntoElement(dom, node.attrs, this.options);
|
|
31347
|
+
const selectNode = () => {
|
|
31348
|
+
const position = getPos();
|
|
31349
|
+
if (typeof position !== "number") {
|
|
31350
|
+
return;
|
|
31351
|
+
}
|
|
31352
|
+
editor.commands.setNodeSelection?.(position);
|
|
31353
|
+
};
|
|
31354
|
+
dom.addEventListener("click", (event2) => {
|
|
31355
|
+
event2.preventDefault();
|
|
31356
|
+
selectNode();
|
|
31357
|
+
});
|
|
31358
|
+
dom.addEventListener("dblclick", (event2) => {
|
|
31359
|
+
event2.preventDefault();
|
|
31360
|
+
event2.stopPropagation();
|
|
31361
|
+
selectNode();
|
|
31362
|
+
editor.commands.openFormulaX();
|
|
31363
|
+
});
|
|
31364
|
+
return {
|
|
31365
|
+
dom,
|
|
31366
|
+
update: (updatedNode) => {
|
|
31367
|
+
if (updatedNode.type.name !== this.name) {
|
|
31368
|
+
return false;
|
|
31369
|
+
}
|
|
31370
|
+
syncFormulaDomElement(dom, updatedNode.attrs, this.options);
|
|
31371
|
+
void renderFormulaIntoElement(dom, updatedNode.attrs, this.options);
|
|
31372
|
+
return true;
|
|
31373
|
+
},
|
|
31374
|
+
selectNode: () => {
|
|
31375
|
+
dom.classList.add("ProseMirror-selectednode");
|
|
31376
|
+
},
|
|
31377
|
+
deselectNode: () => {
|
|
31378
|
+
dom.classList.remove("ProseMirror-selectednode");
|
|
31379
|
+
}
|
|
31380
|
+
};
|
|
31381
|
+
};
|
|
31382
|
+
}
|
|
31383
|
+
};
|
|
31384
|
+
}
|
|
31372
31385
|
function createFormulaXNode(nodeFactory = Node2, options) {
|
|
31373
|
-
|
|
31374
|
-
return options ? extension.configure(options) : extension;
|
|
31386
|
+
return nodeFactory.create(createFormulaXNodeConfig(resolveOptions(options)));
|
|
31375
31387
|
}
|
|
31376
31388
|
var FormulaXNode = createFormulaXNode();
|
|
31377
31389
|
var createFormulaXPayload = (latex) => parseLatex(latex);
|
|
31378
31390
|
var serializeFormulaXPayload = (doc2) => serializeLatex(doc2);
|
|
31379
|
-
function applyFormulaPayload(editor, payload, selectedFormula) {
|
|
31391
|
+
function applyFormulaPayload(editor, payload, selectedFormula, nodeName) {
|
|
31380
31392
|
const latex = payload.latex.trim();
|
|
31381
31393
|
if (selectedFormula) {
|
|
31382
31394
|
if (!latex) {
|
|
@@ -31391,19 +31403,19 @@ var FormulaX = (() => {
|
|
|
31391
31403
|
from: selectedFormula.from,
|
|
31392
31404
|
to: selectedFormula.to
|
|
31393
31405
|
},
|
|
31394
|
-
createFormulaNodeContent(payload)
|
|
31406
|
+
createFormulaNodeContent(payload, nodeName)
|
|
31395
31407
|
).run();
|
|
31396
31408
|
return;
|
|
31397
31409
|
}
|
|
31398
31410
|
if (!latex) {
|
|
31399
31411
|
return;
|
|
31400
31412
|
}
|
|
31401
|
-
editor.chain().focus().insertContent(createFormulaNodeContent(payload)).run();
|
|
31413
|
+
editor.chain().focus().insertContent(createFormulaNodeContent(payload, nodeName)).run();
|
|
31402
31414
|
}
|
|
31403
|
-
function getSelectedFormula(editor) {
|
|
31415
|
+
function getSelectedFormula(editor, nodeName) {
|
|
31404
31416
|
const { selection } = editor.state;
|
|
31405
31417
|
const node = selection.node;
|
|
31406
|
-
if (node?.type?.name !==
|
|
31418
|
+
if (node?.type?.name !== nodeName) {
|
|
31407
31419
|
return null;
|
|
31408
31420
|
}
|
|
31409
31421
|
return {
|
|
@@ -31414,9 +31426,9 @@ var FormulaX = (() => {
|
|
|
31414
31426
|
}
|
|
31415
31427
|
};
|
|
31416
31428
|
}
|
|
31417
|
-
function createFormulaNodeContent(payload) {
|
|
31429
|
+
function createFormulaNodeContent(payload, nodeName = FORMULAX_NODE_NAME) {
|
|
31418
31430
|
return {
|
|
31419
|
-
type:
|
|
31431
|
+
type: nodeName,
|
|
31420
31432
|
attrs: {
|
|
31421
31433
|
latex: payload.latex
|
|
31422
31434
|
}
|