@pixerate/editor 0.47.0 → 0.48.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.
Files changed (76) hide show
  1. package/dist/agent/index.cjs +246 -29
  2. package/dist/agent/index.cjs.map +1 -1
  3. package/dist/agent/index.d.cts +18 -6
  4. package/dist/agent/index.d.ts +18 -6
  5. package/dist/agent/index.js +226 -9
  6. package/dist/agent/index.js.map +1 -1
  7. package/dist/{builtins-xs7rK6ys.d.cts → builtins-1kqwLUnb.d.cts} +1 -1
  8. package/dist/{builtins-D2nMtw2W.d.ts → builtins-BKYlHH1s.d.ts} +1 -1
  9. package/dist/chart/index.cjs +214 -3541
  10. package/dist/chart/index.cjs.map +1 -1
  11. package/dist/chart/index.d.cts +4 -494
  12. package/dist/chart/index.d.ts +4 -494
  13. package/dist/chart/index.js +135 -3462
  14. package/dist/chart/index.js.map +1 -1
  15. package/dist/{chunk-5VXI5GED.js → chunk-2CJ3LPYQ.js} +3 -3
  16. package/dist/{chunk-ZFBBVGCI.js → chunk-EKYHKQPG.js} +243 -243
  17. package/dist/{chunk-ZFBBVGCI.js.map → chunk-EKYHKQPG.js.map} +1 -1
  18. package/dist/chunk-HXUL3LS7.js +113 -0
  19. package/dist/chunk-HXUL3LS7.js.map +1 -0
  20. package/dist/{chunk-REAGGLXS.js → chunk-JQ3BB3E6.js} +2 -2
  21. package/dist/chunk-LTUM52X6.js +4074 -0
  22. package/dist/chunk-LTUM52X6.js.map +1 -0
  23. package/dist/{chunk-J3R2OWOE.cjs → chunk-MUCYWI6W.cjs} +608 -15
  24. package/dist/chunk-MUCYWI6W.cjs.map +1 -0
  25. package/dist/chunk-PIMCHMZE.cjs +521 -0
  26. package/dist/chunk-PIMCHMZE.cjs.map +1 -0
  27. package/dist/{chunk-TK2XZK6Z.js → chunk-PKBWLEL7.js} +594 -1
  28. package/dist/chunk-PKBWLEL7.js.map +1 -0
  29. package/dist/chunk-PRSJJZBS.cjs +4074 -0
  30. package/dist/chunk-PRSJJZBS.cjs.map +1 -0
  31. package/dist/chunk-U6IZKC7B.cjs +113 -0
  32. package/dist/chunk-U6IZKC7B.cjs.map +1 -0
  33. package/dist/{chunk-SQC26VQV.cjs → chunk-UN33YOXW.cjs} +191 -191
  34. package/dist/{chunk-SQC26VQV.cjs.map → chunk-UN33YOXW.cjs.map} +1 -1
  35. package/dist/{chunk-PQFAHT5O.js → chunk-VKPEPWKZ.js} +27 -94
  36. package/dist/chunk-VKPEPWKZ.js.map +1 -0
  37. package/dist/{chunk-H4PICYNL.cjs → chunk-WXEHCVJQ.cjs} +113 -180
  38. package/dist/chunk-WXEHCVJQ.cjs.map +1 -0
  39. package/dist/{chunk-LYGEEDNR.cjs → chunk-XP5IUZUO.cjs} +242 -242
  40. package/dist/chunk-XP5IUZUO.cjs.map +1 -0
  41. package/dist/{chunk-C3NWJE3K.js → chunk-XUUD43OV.js} +11 -672
  42. package/dist/chunk-XUUD43OV.js.map +1 -0
  43. package/dist/{chunk-BBRK6MOI.cjs → chunk-YU4WQE5M.cjs} +100 -100
  44. package/dist/{chunk-BBRK6MOI.cjs.map → chunk-YU4WQE5M.cjs.map} +1 -1
  45. package/dist/commands/index.d.cts +1 -1
  46. package/dist/commands/index.d.ts +1 -1
  47. package/dist/controller-ChjCCMLl.d.cts +822 -0
  48. package/dist/controller-DtbEo3om.d.ts +822 -0
  49. package/dist/extensions/index.cjs +32 -4
  50. package/dist/extensions/index.cjs.map +1 -1
  51. package/dist/extensions/index.d.cts +4 -2
  52. package/dist/extensions/index.d.ts +4 -2
  53. package/dist/extensions/index.js +31 -3
  54. package/dist/{index-QrqMmZ75.d.cts → index-DoHqT1mq.d.cts} +203 -7
  55. package/dist/{index-CSUCQYAH.d.ts → index-YLZ0fKqE.d.ts} +203 -7
  56. package/dist/index.cjs +37 -12
  57. package/dist/index.cjs.map +1 -1
  58. package/dist/index.d.cts +9 -7
  59. package/dist/index.d.ts +9 -7
  60. package/dist/index.js +31 -6
  61. package/dist/index.js.map +1 -1
  62. package/dist/spreadsheet/index.cjs +4 -4
  63. package/dist/spreadsheet/index.js +3 -3
  64. package/package.json +1 -1
  65. package/dist/chunk-C3NWJE3K.js.map +0 -1
  66. package/dist/chunk-H4PICYNL.cjs.map +0 -1
  67. package/dist/chunk-J3R2OWOE.cjs.map +0 -1
  68. package/dist/chunk-LYGEEDNR.cjs.map +0 -1
  69. package/dist/chunk-NEOO6QJD.cjs +0 -1182
  70. package/dist/chunk-NEOO6QJD.cjs.map +0 -1
  71. package/dist/chunk-PQFAHT5O.js.map +0 -1
  72. package/dist/chunk-TK2XZK6Z.js.map +0 -1
  73. package/dist/types-BVqWm3d1.d.cts +0 -320
  74. package/dist/types-BVqWm3d1.d.ts +0 -320
  75. /package/dist/{chunk-5VXI5GED.js.map → chunk-2CJ3LPYQ.js.map} +0 -0
  76. /package/dist/{chunk-REAGGLXS.js.map → chunk-JQ3BB3E6.js.map} +0 -0
@@ -1,3 +1,14 @@
1
+ import {
2
+ ChartController,
3
+ DEFAULT_CHART_THEME,
4
+ chartFromMermaid,
5
+ chartSceneToDomSpec,
6
+ chartToMermaid,
7
+ describeChart,
8
+ layoutChart,
9
+ layoutSparkline,
10
+ normalizeChartSpec
11
+ } from "./chunk-LTUM52X6.js";
1
12
  import {
2
13
  isTrackChangesEnabled
3
14
  } from "./chunk-SWE3NPOU.js";
@@ -1174,6 +1185,570 @@ function createTableExtensions(options = {}) {
1174
1185
  ];
1175
1186
  }
1176
1187
 
1188
+ // src/extensions/chart/node-view.ts
1189
+ import { DOMSerializer } from "@tiptap/pm/model";
1190
+ import { NodeSelection } from "@tiptap/pm/state";
1191
+ var specKey = (spec) => JSON.stringify(spec);
1192
+ var ChartNodeView = class {
1193
+ constructor(props, mount, renderStatic) {
1194
+ this.renderStatic = renderStatic;
1195
+ this.editor = props.editor;
1196
+ this.node = props.node;
1197
+ this.getPos = typeof props.getPos === "function" ? props.getPos : () => void 0;
1198
+ this.editable = this.editor.isEditable;
1199
+ this.dom = document.createElement("div");
1200
+ this.dom.className = "pixerate-chart-node";
1201
+ this.dom.setAttribute("data-type", "chart");
1202
+ this.dom.contentEditable = "false";
1203
+ this.syncAttributes();
1204
+ this.key = specKey(this.node.attrs.spec);
1205
+ this.controller = new ChartController({ spec: this.node.attrs.spec });
1206
+ this.unsubscribe = this.controller.subscribe((state) => {
1207
+ if (!state.gesture) this.writeBack();
1208
+ });
1209
+ this.editor.on("update", this.onEditorUpdate);
1210
+ this.renderer = mount ? mount(this.dom, this.viewState()) : null;
1211
+ if (!mount) this.drawStatic();
1212
+ }
1213
+ renderStatic;
1214
+ dom;
1215
+ node;
1216
+ editor;
1217
+ getPos;
1218
+ controller;
1219
+ renderer;
1220
+ unsubscribe;
1221
+ editing = false;
1222
+ selected = false;
1223
+ editable;
1224
+ key;
1225
+ viewState() {
1226
+ return {
1227
+ id: this.node.attrs.id ?? null,
1228
+ controller: this.controller,
1229
+ width: this.node.attrs.width ?? null,
1230
+ height: this.node.attrs.height,
1231
+ editable: this.editable,
1232
+ selected: this.selected,
1233
+ editing: this.editing,
1234
+ setEditing: this.setEditing,
1235
+ setSize: this.setSize
1236
+ };
1237
+ }
1238
+ render() {
1239
+ if (this.renderer) this.renderer.update(this.viewState());
1240
+ else this.drawStatic();
1241
+ this.dom.toggleAttribute("data-editing", this.editing);
1242
+ }
1243
+ drawStatic() {
1244
+ const { dom } = DOMSerializer.renderSpec(document, this.renderStatic(this.node));
1245
+ this.dom.replaceChildren(dom);
1246
+ }
1247
+ syncAttributes() {
1248
+ if (this.node.attrs.id) this.dom.setAttribute("data-chart-id", this.node.attrs.id);
1249
+ else this.dom.removeAttribute("data-chart-id");
1250
+ }
1251
+ onEditorUpdate = () => {
1252
+ if (this.editor.isEditable === this.editable) return;
1253
+ this.editable = this.editor.isEditable;
1254
+ if (!this.editable) this.editing = false;
1255
+ this.render();
1256
+ };
1257
+ /** Writes the controller's spec into the node, if it changed. */
1258
+ writeBack() {
1259
+ const spec = this.controller.toJSON();
1260
+ const key = specKey(spec);
1261
+ if (key === this.key) return;
1262
+ const pos = this.getPos();
1263
+ if (typeof pos !== "number" || this.editor.isDestroyed) return;
1264
+ this.key = key;
1265
+ this.editor.view.dispatch(this.editor.state.tr.setNodeMarkup(pos, void 0, { ...this.node.attrs, spec }));
1266
+ }
1267
+ setEditing = (editing) => {
1268
+ if (editing && !this.editor.isEditable) return;
1269
+ if (editing && !this.selected) {
1270
+ const pos = this.getPos();
1271
+ if (typeof pos === "number") {
1272
+ this.editor.view.dispatch(this.editor.state.tr.setSelection(NodeSelection.create(this.editor.state.doc, pos)));
1273
+ }
1274
+ }
1275
+ if (this.editing === editing) return;
1276
+ this.editing = editing;
1277
+ this.render();
1278
+ };
1279
+ setSize = (size) => {
1280
+ const pos = this.getPos();
1281
+ if (typeof pos !== "number" || !this.editor.isEditable) return;
1282
+ this.editor.view.dispatch(this.editor.state.tr.setNodeMarkup(pos, void 0, { ...this.node.attrs, ...size }));
1283
+ };
1284
+ update(node) {
1285
+ if (node.type !== this.node.type) return false;
1286
+ this.node = node;
1287
+ this.syncAttributes();
1288
+ const key = specKey(node.attrs.spec);
1289
+ if (key !== this.key) {
1290
+ this.key = key;
1291
+ if (specKey(this.controller.toJSON()) !== key) this.controller.load(node.attrs.spec, { clearHistory: true });
1292
+ }
1293
+ this.render();
1294
+ return true;
1295
+ }
1296
+ selectNode() {
1297
+ this.selected = true;
1298
+ this.dom.classList.add("ProseMirror-selectednode");
1299
+ this.render();
1300
+ }
1301
+ deselectNode() {
1302
+ this.selected = false;
1303
+ this.editing = false;
1304
+ this.dom.classList.remove("ProseMirror-selectednode");
1305
+ this.render();
1306
+ }
1307
+ /** The inspector's inputs, buttons and chart gestures keep their events while it is open. */
1308
+ stopEvent(event) {
1309
+ if (!this.editing) return false;
1310
+ return !(event.type === "dragstart" && event.target === this.dom);
1311
+ }
1312
+ ignoreMutation() {
1313
+ return true;
1314
+ }
1315
+ destroy() {
1316
+ this.editor.off("update", this.onEditorUpdate);
1317
+ this.unsubscribe();
1318
+ this.renderer?.destroy();
1319
+ this.controller.destroy();
1320
+ }
1321
+ };
1322
+ function createChartNodeView(mount, renderStatic) {
1323
+ return (props) => new ChartNodeView(props, mount, renderStatic);
1324
+ }
1325
+
1326
+ // src/extensions/chart/chart-node.ts
1327
+ import { Node, mergeAttributes } from "@tiptap/core";
1328
+ import { Plugin as Plugin2, PluginKey as PluginKey2 } from "@tiptap/pm/state";
1329
+
1330
+ // src/extensions/document-structure/utils.ts
1331
+ import { createDocument } from "@tiptap/core";
1332
+ import { EditorState } from "@tiptap/pm/state";
1333
+ import { ReplaceAroundStep, ReplaceStep } from "@tiptap/pm/transform";
1334
+ var idCounter = 0;
1335
+ function generateStructureId(prefix) {
1336
+ idCounter = (idCounter + 1) % 1296;
1337
+ const random = Math.random().toString(36).slice(2, 8).padEnd(6, "0");
1338
+ return `${prefix}-${random}${idCounter.toString(36).padStart(2, "0")}`;
1339
+ }
1340
+ function transactionsInsert(transactions, typeNames) {
1341
+ for (const tr of transactions) {
1342
+ for (const step of tr.steps) {
1343
+ if (!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)) {
1344
+ const mark = step.mark;
1345
+ if (mark && typeNames.includes(mark.type.name)) return true;
1346
+ continue;
1347
+ }
1348
+ let found = false;
1349
+ step.slice.content.descendants((node) => {
1350
+ if (found) return false;
1351
+ if (typeNames.includes(node.type.name) || node.marks.some((m) => typeNames.includes(m.type.name))) {
1352
+ found = true;
1353
+ }
1354
+ return !found;
1355
+ });
1356
+ if (found) return true;
1357
+ }
1358
+ }
1359
+ return false;
1360
+ }
1361
+ function docContains(doc, typeNames) {
1362
+ let found = false;
1363
+ doc.descendants((node) => {
1364
+ if (found) return false;
1365
+ if (typeNames.includes(node.type.name) || node.marks.some((m) => typeNames.includes(m.type.name))) {
1366
+ found = true;
1367
+ }
1368
+ return !found;
1369
+ });
1370
+ return found;
1371
+ }
1372
+ function presenceStateSpec(typeNames) {
1373
+ return {
1374
+ init: (_config, state) => docContains(state.doc, typeNames),
1375
+ apply: (tr, present, _oldState, newState) => {
1376
+ if (!tr.docChanged) return present;
1377
+ if (!present && !transactionsInsert([tr], typeNames)) return false;
1378
+ return docContains(newState.doc, typeNames);
1379
+ }
1380
+ };
1381
+ }
1382
+ function arraysEqual(a, b, eq) {
1383
+ if (a.length !== b.length) return false;
1384
+ for (let i = 0; i < a.length; i++) {
1385
+ if (!eq(a[i], b[i])) return false;
1386
+ }
1387
+ return true;
1388
+ }
1389
+ function scrollDomAtPos(view, pos, behavior = "smooth") {
1390
+ try {
1391
+ const dom = view?.nodeDOM?.(pos);
1392
+ if (dom && typeof dom.scrollIntoView === "function") {
1393
+ dom.scrollIntoView({ block: "start", behavior });
1394
+ }
1395
+ } catch {
1396
+ }
1397
+ }
1398
+ function syncInitialContent(editor, sync) {
1399
+ const { content } = editor.options;
1400
+ if (content == null || content === "") return;
1401
+ try {
1402
+ const doc = createDocument(content, editor.schema, editor.options.parseOptions, {
1403
+ errorOnInvalidContent: false
1404
+ });
1405
+ const tr = sync(EditorState.create({ doc }));
1406
+ if (tr?.docChanged) editor.options.content = tr.doc;
1407
+ } catch {
1408
+ }
1409
+ }
1410
+ var STRUCTURE_PRIORITY = 45;
1411
+
1412
+ // src/extensions/chart/chart-node.ts
1413
+ var chartPluginKey = new PluginKey2("chartNode");
1414
+ function getDocumentCharts(doc, typeName = "chart") {
1415
+ const charts = [];
1416
+ doc.descendants((node, pos) => {
1417
+ if (node.type.name === typeName) {
1418
+ charts.push({ id: node.attrs.id ?? null, pos, node, spec: node.attrs.spec });
1419
+ return false;
1420
+ }
1421
+ return !node.isTextblock;
1422
+ });
1423
+ return charts;
1424
+ }
1425
+ function isChartEnabled(editor) {
1426
+ return !!editor?.schema?.nodes?.chart;
1427
+ }
1428
+ function readSpec(value) {
1429
+ let input = value;
1430
+ if (typeof value === "string") {
1431
+ try {
1432
+ input = JSON.parse(value);
1433
+ } catch {
1434
+ return null;
1435
+ }
1436
+ }
1437
+ if (!input || typeof input !== "object" || Array.isArray(input)) return null;
1438
+ return normalizeChartSpec(input).spec;
1439
+ }
1440
+ var readSize = (value) => {
1441
+ const n = value === null ? NaN : Number(value);
1442
+ return Number.isFinite(n) && n >= 50 && n <= 4096 ? n : null;
1443
+ };
1444
+ function syncChartIds(state, typeName, generateId) {
1445
+ const seen = /* @__PURE__ */ new Set();
1446
+ let tr = null;
1447
+ for (const chart of getDocumentCharts(state.doc, typeName)) {
1448
+ if (chart.id && !seen.has(chart.id)) {
1449
+ seen.add(chart.id);
1450
+ continue;
1451
+ }
1452
+ let id = generateId();
1453
+ while (seen.has(id)) id = generateId();
1454
+ seen.add(id);
1455
+ tr = tr ?? state.tr;
1456
+ tr.setNodeMarkup(chart.pos, void 0, { ...chart.node.attrs, id });
1457
+ }
1458
+ return tr?.setMeta("addToHistory", false) ?? null;
1459
+ }
1460
+ function staticChartSvg(node, options) {
1461
+ const spec = node.attrs.spec;
1462
+ const width = node.attrs.width ?? options.defaultWidth;
1463
+ const height = node.attrs.height ?? options.defaultHeight;
1464
+ const scene = layoutChart(spec, { width, height, theme: options.theme, selection: null });
1465
+ return chartSceneToDomSpec(scene, {
1466
+ ariaLabel: spec.title || "Chart",
1467
+ description: describeChart(spec),
1468
+ attrs: { style: "max-width: 100%; height: auto; display: block" }
1469
+ });
1470
+ }
1471
+ var longestBacktickRun = (text) => Math.max(0, ...Array.from(text.matchAll(/`+/g), (m) => m[0].length));
1472
+ var Chart = Node.create({
1473
+ name: "chart",
1474
+ priority: STRUCTURE_PRIORITY,
1475
+ group: "block",
1476
+ atom: true,
1477
+ selectable: true,
1478
+ draggable: true,
1479
+ addOptions() {
1480
+ return {
1481
+ HTMLAttributes: {},
1482
+ nodeView: null,
1483
+ theme: DEFAULT_CHART_THEME,
1484
+ defaultWidth: 640,
1485
+ defaultHeight: 320,
1486
+ generateId: () => generateStructureId("chart")
1487
+ };
1488
+ },
1489
+ addAttributes() {
1490
+ return {
1491
+ id: {
1492
+ default: null,
1493
+ parseHTML: (element) => element.getAttribute("data-chart-id") || null,
1494
+ renderHTML: (attributes) => attributes.id ? { "data-chart-id": attributes.id } : {}
1495
+ },
1496
+ spec: {
1497
+ default: normalizeChartSpec({}).spec,
1498
+ // null (no `data-chart`) keeps what the parse rule found, e.g. a mermaid block's spec.
1499
+ parseHTML: (element) => readSpec(element.getAttribute("data-chart")),
1500
+ renderHTML: (attributes) => ({
1501
+ "data-chart": JSON.stringify(attributes.spec)
1502
+ })
1503
+ },
1504
+ width: {
1505
+ default: null,
1506
+ parseHTML: (element) => readSize(element.getAttribute("data-width")),
1507
+ renderHTML: (attributes) => attributes.width ? { "data-width": String(attributes.width) } : {}
1508
+ },
1509
+ height: {
1510
+ default: this.options.defaultHeight,
1511
+ parseHTML: (element) => readSize(element.getAttribute("data-height")),
1512
+ renderHTML: (attributes) => ({
1513
+ "data-height": String(attributes.height)
1514
+ })
1515
+ }
1516
+ };
1517
+ },
1518
+ parseHTML() {
1519
+ return [
1520
+ {
1521
+ tag: 'div[data-type="chart"]',
1522
+ priority: 60,
1523
+ // A missing or broken spec falls through, so the fallback content inside (e.g. a
1524
+ // DOCX import's caption and data table) is kept instead.
1525
+ getAttrs: (element) => readSpec(element.getAttribute("data-chart")) ? null : false
1526
+ },
1527
+ {
1528
+ // ```mermaid blocks (from markdown) that hold a chart; other diagrams stay code.
1529
+ tag: "pre",
1530
+ priority: 60,
1531
+ getAttrs: (element) => {
1532
+ const code = element.querySelector("code");
1533
+ const language = code?.getAttribute("class") ?? element.getAttribute("data-language") ?? "";
1534
+ if (!/(?:^|\s)language-mermaid(?:\s|$)/.test(language) && language !== "mermaid") return false;
1535
+ const result = chartFromMermaid((code ?? element).textContent ?? "");
1536
+ return result.family ? { spec: result.spec } : false;
1537
+ }
1538
+ }
1539
+ ];
1540
+ },
1541
+ renderHTML({ node, HTMLAttributes }) {
1542
+ const svg = staticChartSvg(node, this.options);
1543
+ return [
1544
+ "div",
1545
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
1546
+ "data-type": "chart",
1547
+ class: "pixerate-chart"
1548
+ }),
1549
+ svg
1550
+ ];
1551
+ },
1552
+ renderText({ node }) {
1553
+ return `[Chart: ${node.attrs.spec?.title || "untitled"}]`;
1554
+ },
1555
+ addStorage() {
1556
+ return {
1557
+ markdown: {
1558
+ serialize(state, node) {
1559
+ const text = chartToMermaid(node.attrs.spec);
1560
+ const fence = "`".repeat(Math.max(3, longestBacktickRun(text) + 1));
1561
+ state.write(`${fence}mermaid
1562
+ `);
1563
+ state.text(text, false);
1564
+ state.ensureNewLine();
1565
+ state.write(fence);
1566
+ state.closeBlock(node);
1567
+ },
1568
+ parse: {
1569
+ // markdown-it renders ```mermaid as <pre><code class="language-mermaid">, which
1570
+ // `parseHTML` reads.
1571
+ }
1572
+ }
1573
+ };
1574
+ },
1575
+ addCommands() {
1576
+ return {
1577
+ insertChart: (options) => ({ commands }) => commands.insertContent({
1578
+ type: this.name,
1579
+ attrs: {
1580
+ id: options.id ?? this.options.generateId(),
1581
+ spec: normalizeChartSpec(options.spec ?? {}).spec,
1582
+ width: options.width ?? null,
1583
+ height: options.height ?? this.options.defaultHeight
1584
+ }
1585
+ }),
1586
+ updateChart: (id, changes) => ({ state, tr, dispatch }) => {
1587
+ const chart = getDocumentCharts(state.doc, this.name).find((c) => c.id === id);
1588
+ if (!chart) return false;
1589
+ if (dispatch) {
1590
+ tr.setNodeMarkup(chart.pos, void 0, {
1591
+ ...chart.node.attrs,
1592
+ ...changes.spec !== void 0 ? { spec: normalizeChartSpec(changes.spec).spec } : {},
1593
+ ...changes.width !== void 0 ? { width: changes.width } : {},
1594
+ ...changes.height !== void 0 ? { height: changes.height } : {}
1595
+ });
1596
+ }
1597
+ return true;
1598
+ }
1599
+ };
1600
+ },
1601
+ addNodeView() {
1602
+ return createChartNodeView(this.options.nodeView, (node) => staticChartSvg(node, this.options));
1603
+ },
1604
+ addProseMirrorPlugins() {
1605
+ const { name } = this;
1606
+ const { generateId } = this.options;
1607
+ return [
1608
+ new Plugin2({
1609
+ key: chartPluginKey,
1610
+ appendTransaction: (transactions, _oldState, newState) => {
1611
+ if (!transactions.some((tr) => tr.docChanged)) return null;
1612
+ if (!transactionsInsert(transactions, [name])) return null;
1613
+ return syncChartIds(newState, name, generateId);
1614
+ }
1615
+ })
1616
+ ];
1617
+ },
1618
+ onBeforeCreate() {
1619
+ const { generateId } = this.options;
1620
+ syncInitialContent(this.editor, (state) => syncChartIds(state, this.name, generateId));
1621
+ }
1622
+ });
1623
+
1624
+ // src/extensions/chart/sparkline-node.ts
1625
+ import { Node as Node2, mergeAttributes as mergeAttributes2 } from "@tiptap/core";
1626
+ var SPARKLINE_KINDS = ["line", "column", "win-loss"];
1627
+ var MAX_SPARKLINE_VALUES = 1e3;
1628
+ function normalizeSparklineValues(values) {
1629
+ if (!Array.isArray(values)) return [];
1630
+ return values.slice(0, MAX_SPARKLINE_VALUES).map((v) => typeof v === "number" && Number.isFinite(v) ? v : null);
1631
+ }
1632
+ function readValues(text) {
1633
+ if (!text) return [];
1634
+ try {
1635
+ return normalizeSparklineValues(JSON.parse(text));
1636
+ } catch {
1637
+ return normalizeSparklineValues(text.split(/[\s,]+/).filter(Boolean).map(Number));
1638
+ }
1639
+ }
1640
+ var readNumber = (text, fallback, max) => {
1641
+ const n = text === null ? NaN : Number(text);
1642
+ return Number.isFinite(n) && n >= 4 && n <= max ? n : fallback;
1643
+ };
1644
+ function sparklineLabel(node) {
1645
+ if (node.attrs.label) return node.attrs.label;
1646
+ const present = node.attrs.values.filter((v) => v !== null);
1647
+ return present.length ? `Trend from ${present[0]} to ${present[present.length - 1]}` : "No data";
1648
+ }
1649
+ var Sparkline = Node2.create({
1650
+ name: "sparkline",
1651
+ group: "inline",
1652
+ inline: true,
1653
+ atom: true,
1654
+ selectable: true,
1655
+ addOptions() {
1656
+ return { HTMLAttributes: {}, color: "#4269d0", negativeColor: "#e5484d" };
1657
+ },
1658
+ addAttributes() {
1659
+ return {
1660
+ values: {
1661
+ default: [],
1662
+ parseHTML: (element) => readValues(element.getAttribute("data-values")),
1663
+ renderHTML: (attributes) => ({ "data-values": JSON.stringify(attributes.values ?? []) })
1664
+ },
1665
+ kind: {
1666
+ default: "line",
1667
+ parseHTML: (element) => {
1668
+ const kind = element.getAttribute("data-kind");
1669
+ return SPARKLINE_KINDS.includes(kind) ? kind : "line";
1670
+ },
1671
+ renderHTML: (attributes) => ({ "data-kind": attributes.kind })
1672
+ },
1673
+ width: {
1674
+ default: 80,
1675
+ parseHTML: (element) => readNumber(element.getAttribute("data-width"), 80, 1e3),
1676
+ renderHTML: (attributes) => ({ "data-width": String(attributes.width) })
1677
+ },
1678
+ height: {
1679
+ default: 18,
1680
+ parseHTML: (element) => readNumber(element.getAttribute("data-height"), 18, 200),
1681
+ renderHTML: (attributes) => ({ "data-height": String(attributes.height) })
1682
+ },
1683
+ color: {
1684
+ default: null,
1685
+ parseHTML: (element) => element.getAttribute("data-color") || null,
1686
+ renderHTML: (attributes) => attributes.color ? { "data-color": attributes.color } : {}
1687
+ },
1688
+ label: {
1689
+ default: null,
1690
+ parseHTML: (element) => element.getAttribute("data-label") || null,
1691
+ renderHTML: (attributes) => attributes.label ? { "data-label": attributes.label } : {}
1692
+ }
1693
+ };
1694
+ },
1695
+ parseHTML() {
1696
+ return [{ tag: 'span[data-type="sparkline"]', priority: 60 }];
1697
+ },
1698
+ renderHTML({ node, HTMLAttributes }) {
1699
+ const scene = layoutSparkline(node.attrs.values, {
1700
+ kind: node.attrs.kind,
1701
+ width: node.attrs.width,
1702
+ height: node.attrs.height,
1703
+ color: node.attrs.color ?? this.options.color,
1704
+ negativeColor: this.options.negativeColor
1705
+ });
1706
+ const svg = chartSceneToDomSpec(scene, {
1707
+ ariaLabel: sparklineLabel(node),
1708
+ attrs: { style: "display: inline-block; vertical-align: middle" }
1709
+ });
1710
+ return [
1711
+ "span",
1712
+ mergeAttributes2(this.options.HTMLAttributes, HTMLAttributes, {
1713
+ "data-type": "sparkline",
1714
+ class: "pixerate-sparkline"
1715
+ }),
1716
+ svg
1717
+ ];
1718
+ },
1719
+ renderText({ node }) {
1720
+ return `[${sparklineLabel(node)}]`;
1721
+ },
1722
+ addStorage() {
1723
+ return {
1724
+ markdown: {
1725
+ serialize(state, node) {
1726
+ const attr = (name, value) => value === null || value === void 0 ? "" : ` ${name}="${String(value).replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;")}"`;
1727
+ state.write(
1728
+ `<span data-type="sparkline"${attr("data-values", JSON.stringify(node.attrs.values))}${attr("data-kind", node.attrs.kind)}${attr("data-width", node.attrs.width)}${attr("data-height", node.attrs.height)}${attr("data-color", node.attrs.color)}${attr("data-label", node.attrs.label)}></span>`
1729
+ );
1730
+ },
1731
+ parse: {}
1732
+ }
1733
+ };
1734
+ },
1735
+ addCommands() {
1736
+ return {
1737
+ insertSparkline: (options) => ({ commands }) => commands.insertContent({
1738
+ type: this.name,
1739
+ attrs: {
1740
+ values: normalizeSparklineValues(options.values),
1741
+ kind: options.kind && SPARKLINE_KINDS.includes(options.kind) ? options.kind : "line",
1742
+ width: options.width ?? 80,
1743
+ height: options.height ?? 18,
1744
+ color: options.color ?? null,
1745
+ label: options.label ?? null
1746
+ }
1747
+ })
1748
+ };
1749
+ }
1750
+ });
1751
+
1177
1752
  // src/extensions/table/actions.ts
1178
1753
  import { CellSelection as CellSelection2, isInTable, selectedRect } from "@tiptap/pm/tables";
1179
1754
  var TABLE_ACTIONS = [
@@ -1430,6 +2005,12 @@ export {
1430
2005
  handleTablePasteWithoutNesting,
1431
2006
  unnestTables,
1432
2007
  createTableNestingPlugin,
2008
+ generateStructureId,
2009
+ presenceStateSpec,
2010
+ arraysEqual,
2011
+ scrollDomAtPos,
2012
+ syncInitialContent,
2013
+ STRUCTURE_PRIORITY,
1433
2014
  FIND_PARAGRAPH_MARK,
1434
2015
  MAX_FIND_PATTERN_LENGTH,
1435
2016
  createSearchBudget,
@@ -1449,6 +2030,18 @@ export {
1449
2030
  TableCell,
1450
2031
  TableHeader,
1451
2032
  createTableExtensions,
2033
+ ChartNodeView,
2034
+ createChartNodeView,
2035
+ chartPluginKey,
2036
+ getDocumentCharts,
2037
+ isChartEnabled,
2038
+ staticChartSvg,
2039
+ Chart,
2040
+ SPARKLINE_KINDS,
2041
+ MAX_SPARKLINE_VALUES,
2042
+ normalizeSparklineValues,
2043
+ sparklineLabel,
2044
+ Sparkline,
1452
2045
  TABLE_ACTIONS,
1453
2046
  DEFAULT_TABLE_TOOLBAR,
1454
2047
  MAX_TABLE_ROWS,
@@ -1462,4 +2055,4 @@ export {
1462
2055
  getTableState,
1463
2056
  createTableStore
1464
2057
  };
1465
- //# sourceMappingURL=chunk-TK2XZK6Z.js.map
2058
+ //# sourceMappingURL=chunk-PKBWLEL7.js.map