@nerd-bible/wordgard 0.5.3-beta7 → 0.6.2-beta2

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/dist/table.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Decoration, Wordgard } from 'wordgard/editor';
2
2
  import { GardState, Transaction, GardSelection, Correction } from 'wordgard/state';
3
3
  import { Table, ColSpan, RowSpan, TableRow, Cell, BlockCell, HeaderCell, BlockHeaderCell } from 'wordgard/types';
4
- import { Node, ValidationError, Token, ChangeSet } from 'wordgard/doc';
4
+ import { Node, Slice, ValidationError, Token, ChangeSet } from 'wordgard/doc';
5
5
  import { Command, moveByUnit, moveByWord, moveByLine, moveToLineSide, Menu } from 'wordgard/command';
6
6
  import { tablePhrases } from 'wordgard/phrases';
7
7
 
@@ -330,6 +330,24 @@ class CellSelection extends GardSelection {
330
330
  let { from, to } = this.replacementRange;
331
331
  return { anchor: from, anchorSide: 1, head: to, headSide: -1 };
332
332
  }
333
+ toSlice(doc) {
334
+ let base = doc.resolve(this.anchorCell), table = base.parent.parent;
335
+ let map = TableMap.get(table.node, table.start);
336
+ let rect = map.rectBetween(this.anchorCell, this.headCell);
337
+ let rows = [], added = new Set();
338
+ for (let row = rect.startRow; row < rect.endRow; row++) {
339
+ let rowContent = [];
340
+ for (let col = rect.startCol; col < rect.endCol; col++) {
341
+ let cell = map.cellAt(col, row);
342
+ if (added.has(cell))
343
+ continue;
344
+ added.add(cell);
345
+ rowContent.push(map.getCell(cell));
346
+ }
347
+ rows.push(table.node.content[row].tag.create(rowContent));
348
+ }
349
+ return Slice.of(rows, [table.node.tag]);
350
+ }
333
351
  eq(other) {
334
352
  return other instanceof CellSelection && other.anchor == this.anchor && other.head == this.head;
335
353
  }
@@ -768,7 +786,7 @@ const splitCell = ({ state }) => {
768
786
  };
769
787
  };
770
788
 
771
- function fitSlice(schema, parent, slice, context) {
789
+ function fitSlice(schema, parent, slice) {
772
790
  let wrap = schema.findWrapping(schema.docTag.type, parent.type);
773
791
  if (!wrap)
774
792
  return null;
@@ -776,7 +794,7 @@ function fitSlice(schema, parent, slice, context) {
776
794
  for (let i = wrap.length - 1; i >= 0; i--)
777
795
  content = [wrap[i].create(content)];
778
796
  let doc = schema.doc(content);
779
- let changes = ChangeSet.create(doc, { from: wrap.length + 1, to: doc.length - wrap.length - 1, insert: slice, fit: context });
797
+ let changes = ChangeSet.create(doc, { from: wrap.length + 1, to: doc.length - wrap.length - 1, insert: slice, fit: true });
780
798
  doc = changes.apply(doc);
781
799
  let node = doc.length > wrap.length && doc.plotAt(wrap.length);
782
800
  return node && node.type == parent.type ? node : null;
@@ -784,15 +802,15 @@ function fitSlice(schema, parent, slice, context) {
784
802
  function isTableContent(schema, type) {
785
803
  return type == TableRow.type || schema.matchNode(type, Node.Group.TableCell);
786
804
  }
787
- function pastedCells(schema, slice, context) {
805
+ function pastedCells(schema, slice) {
788
806
  let table = null, tok;
789
807
  if (slice.content.length == 1 && (tok = slice.content[0]).tokenType == Token.Type.Node &&
790
808
  tok.type == Table.type) {
791
809
  table = tok;
792
810
  }
793
- else if (context.some(tag => isTableContent(schema, tag.type)) ||
811
+ else if (slice.context.some(tag => isTableContent(schema, tag.type)) ||
794
812
  slice.content.some(tok => tok.tokenType != Token.Type.Close && isTableContent(schema, tok.type))) {
795
- table = fitSlice(schema, Table, slice, context);
813
+ table = fitSlice(schema, Table, slice);
796
814
  }
797
815
  return table && ensureRectangular(schema, table.content.map(row => row.content));
798
816
  }
@@ -960,14 +978,14 @@ function insertCells(state, map, startCol, startRow, cells, event) {
960
978
  userEvent: `${event}.paste`
961
979
  };
962
980
  }
963
- function handleTablePaste(state, slice, context, drop) {
981
+ function handleTablePaste(state, slice, drop) {
964
982
  if (state.readOnly)
965
983
  return false;
966
984
  let { schema } = state.doc;
967
985
  if (drop == null && state.selection instanceof CellSelection) {
968
- let cells = pastedCells(schema, slice, context);
986
+ let cells = pastedCells(schema, slice);
969
987
  if (!cells) {
970
- let single = fitSlice(schema, cellTag(schema), slice, context);
988
+ let single = fitSlice(schema, cellTag(schema), slice);
971
989
  if (!single)
972
990
  return false;
973
991
  cells = { width: 1, height: 1, rows: [[single]] };
@@ -978,20 +996,20 @@ function handleTablePaste(state, slice, context, drop) {
978
996
  }
979
997
  else {
980
998
  let cx = tableContext(state, drop), cells;
981
- if (!cx || !(cells = pastedCells(schema, slice, context)))
999
+ if (!cx || !(cells = pastedCells(schema, slice)))
982
1000
  return false;
983
1001
  let rect = cx.map.cellRect(cx.cells[0]);
984
1002
  return insertCells(state, cx.map, rect.startCol, rect.startRow, cells, drop == null ? "drop" : "paste");
985
1003
  }
986
1004
  }
987
- const tablePasteHandler = /*@__PURE__*/Wordgard.pasteHandler.of((wg, _event, slice, context) => {
988
- let tr = handleTablePaste(wg.state, slice, context);
1005
+ const tablePasteHandler = /*@__PURE__*/Wordgard.pasteHandler.of((wg, _event, slice) => {
1006
+ let tr = handleTablePaste(wg.state, slice);
989
1007
  return tr && (wg.dispatch(tr), true);
990
1008
  });
991
- const tableDropHandler = /*@__PURE__*/Wordgard.dropHandler.of((wg, _event, pos, move, slice, context) => {
1009
+ const tableDropHandler = /*@__PURE__*/Wordgard.dropHandler.of((wg, _event, pos, move, slice) => {
992
1010
  if (wg.state.readOnly)
993
1011
  return false;
994
- let tr = handleTablePaste(wg.state, slice, context, pos);
1012
+ let tr = handleTablePaste(wg.state, slice, pos);
995
1013
  if (!tr)
996
1014
  return false;
997
1015
  if (move) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nerd-bible/wordgard",
3
- "version": "0.5.3-beta7",
3
+ "version": "0.6.2-beta2",
4
4
  "description": "Semantic rich text editor system",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -22,12 +22,13 @@
22
22
  "author": "Marijn Haverbeke <marijn@haverbeke.berlin>",
23
23
  "license": "MIT",
24
24
  "dependencies": {
25
+ "@codemirror/legacy-modes": "^6.5.5",
26
+ "@codemirror/streamparser": "^6.0.0",
25
27
  "@marijn/find-cluster-break": "^1.0.2",
26
28
  "crelt": "^1.0.6",
27
29
  "style-mod": "^4.1.3"
28
30
  },
29
31
  "devDependencies": {
30
- "@nerd-bible/config": "^0.2.5",
31
32
  "@swc/wasm-typescript": "^1.15.3",
32
33
  "@types/mocha": "^10.0.10",
33
34
  "@types/node": "^24.10.2",