@prosekit/extensions 0.4.3 → 0.4.4

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.
@@ -485,7 +485,9 @@ export declare function defineListSpec(): Extension<{
485
485
  * A mark rule is something that can automatically apply marks to text if it
486
486
  * matches a certain pattern, and remove them if it doesn't match anymore.
487
487
  */
488
- export declare function defineMarkRule(options: MarkRuleOptions): Extension<ExtensionTyping<string, string, CommandArgs>>;
488
+ declare function defineMarkRule(options: MarkRuleOptions): Extension<ExtensionTyping<string, string, CommandArgs>>;
489
+ export { defineMarkRule }
490
+ export { defineMarkRule as defineMarkRule_alias_1 }
489
491
 
490
492
  /**
491
493
  * @public
@@ -839,11 +841,10 @@ export { ListDOMSerializer }
839
841
  /**
840
842
  * @internal
841
843
  */
842
- export declare class MarkRule {
843
- readonly regex: RegExp;
844
- readonly type: string | MarkType;
845
- readonly getAttrs: (match: RegExpMatchArray) => Attrs | null;
846
- constructor({ regex, type, attrs }: MarkRuleOptions);
844
+ export declare interface MarkRule {
845
+ regex: RegExp;
846
+ type: string | MarkType;
847
+ getAttrs?: ((match: RegExpMatchArray) => Attrs | null) | null;
847
848
  }
848
849
 
849
850
  /**
@@ -1,4 +1,4 @@
1
- // src/mark-rule/index.ts
1
+ // src/mark-rule/extension.ts
2
2
  import { Facet, pluginFacet } from "@prosekit/core";
3
3
  import { ProseMirrorPlugin } from "@prosekit/pm/state";
4
4
 
@@ -62,7 +62,7 @@ function getMapRange(transactions, oldState, newState) {
62
62
  hi = map.map(hi);
63
63
  map.forEach((_oldStart, _oldEnd, newStart, newEnd) => {
64
64
  lo = Math.min(lo, hi, newStart);
65
- hi = Math.max(hi, hi, newEnd);
65
+ hi = Math.max(lo, hi, newEnd);
66
66
  });
67
67
  }
68
68
  }
@@ -77,8 +77,9 @@ function getCheckRanges(transactions, oldState, newState) {
77
77
 
78
78
  // src/mark-rule/apply.ts
79
79
  function getExpectedMarkings(rules, doc, from, to) {
80
+ var _a;
80
81
  const text = doc.textBetween(from, to, OBJECT_REPLACEMENT_CHARACTER);
81
- const result = [];
82
+ const ranges = [];
82
83
  for (const rule of rules) {
83
84
  rule.regex.lastIndex = 0;
84
85
  const matches = text.matchAll(rule.regex);
@@ -87,9 +88,18 @@ function getExpectedMarkings(rules, doc, from, to) {
87
88
  const index = match.index;
88
89
  if (index == null)
89
90
  continue;
90
- const attrs = rule.getAttrs(match);
91
+ const attrs = (_a = rule.getAttrs) == null ? void 0 : _a.call(rule, match);
91
92
  const mark = markType.create(attrs);
92
- result.push([mark, from + index, from + index + match[0].length]);
93
+ ranges.push([from + index, from + index + match[0].length, mark]);
94
+ }
95
+ }
96
+ ranges.sort((a, b) => a[0] - b[0] || b[1] - a[1]);
97
+ const result = [];
98
+ let freeIndex = 0;
99
+ for (const range of ranges) {
100
+ if (range[0] >= freeIndex) {
101
+ result.push(range);
102
+ freeIndex = range[1];
93
103
  }
94
104
  }
95
105
  return result;
@@ -105,17 +115,17 @@ function getReceivedMarkings(rules, doc, from, to) {
105
115
  for (const markType of markTypes) {
106
116
  const mark = node.marks.find((mark2) => mark2.type === markType);
107
117
  if (mark) {
108
- result.push([mark, pos, pos + node.nodeSize]);
118
+ result.push([pos, pos + node.nodeSize, mark]);
109
119
  }
110
120
  }
111
121
  });
112
122
  return result;
113
123
  }
114
- function markingEquals(a, b) {
115
- return a[1] === b[1] && a[2] === b[2] && a[0].eq(b[0]);
124
+ function markRangeEquals(a, b) {
125
+ return a[0] === b[0] && a[1] === b[1] && a[2].eq(b[2]);
116
126
  }
117
- function markingDiffs(a, b) {
118
- return a.filter((x) => !b.some((y) => markingEquals(x, y)));
127
+ function markRangeDiffs(a, b) {
128
+ return a.filter((x) => !b.some((y) => markRangeEquals(x, y)));
119
129
  }
120
130
  function applyMarkRules(rules, transactions, oldState, newState) {
121
131
  if (transactions.length === 0 || transactions.every((tr2) => !tr2.docChanged)) {
@@ -127,35 +137,27 @@ function applyMarkRules(rules, transactions, oldState, newState) {
127
137
  for (const [from, to] of ranges) {
128
138
  const expected = getExpectedMarkings(rules, newState.doc, from, to);
129
139
  const received = getReceivedMarkings(rules, newState.doc, from, to);
130
- toRemove.push(...markingDiffs(received, expected));
131
- toCreate.push(...markingDiffs(expected, received));
140
+ toRemove.push(...markRangeDiffs(received, expected));
141
+ toCreate.push(...markRangeDiffs(expected, received));
132
142
  }
133
143
  if (toCreate.length === 0 && toRemove.length === 0) {
134
144
  return null;
135
145
  }
136
146
  const tr = newState.tr;
137
- for (const [mark, from, to] of toRemove) {
147
+ for (const [from, to, mark] of toRemove) {
138
148
  tr.removeMark(from, to, mark);
139
149
  }
140
- for (const [mark, from, to] of toCreate) {
150
+ for (const [from, to, mark] of toCreate) {
141
151
  tr.addMark(from, to, mark);
142
152
  }
143
153
  return tr;
144
154
  }
145
155
 
146
- // src/mark-rule/rule.ts
147
- import "@prosekit/pm/model";
148
- var MarkRule = class {
149
- constructor({ regex, type, attrs = null }) {
150
- this.regex = regex;
151
- this.type = type;
152
- this.getAttrs = typeof attrs === "function" ? attrs : () => attrs;
153
- }
154
- };
155
-
156
- // src/mark-rule/index.ts
156
+ // src/mark-rule/extension.ts
157
157
  function defineMarkRule(options) {
158
- return markRuleFacet.extension([new MarkRule(options)]);
158
+ const { regex, type, attrs } = options;
159
+ const getAttrs = attrs && typeof attrs === "object" ? () => attrs : attrs;
160
+ return markRuleFacet.extension([{ regex, type, getAttrs }]);
159
161
  }
160
162
  var markRuleFacet = Facet.define({
161
163
  converter: () => {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  defineMarkRule
3
- } from "./chunk-TXF4SPMB.js";
3
+ } from "./chunk-MX6UNVWH.js";
4
4
  import {
5
5
  defineEnterRule
6
6
  } from "./chunk-ASTUC4KT.js";
@@ -1,2 +1,2 @@
1
- export { defineMarkRule } from './_tsup-dts-rollup';
2
1
  export { MarkRuleOptions } from './_tsup-dts-rollup';
2
+ export { defineMarkRule_alias_1 as defineMarkRule } from './_tsup-dts-rollup';
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  defineMarkRule
3
- } from "./chunk-TXF4SPMB.js";
3
+ } from "./chunk-MX6UNVWH.js";
4
4
  export {
5
5
  defineMarkRule
6
6
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@prosekit/extensions",
3
3
  "type": "module",
4
- "version": "0.4.3",
4
+ "version": "0.4.4",
5
5
  "private": false,
6
6
  "author": {
7
7
  "name": "ocavue",
@@ -152,7 +152,7 @@
152
152
  "dist"
153
153
  ],
154
154
  "dependencies": {
155
- "@prosekit/core": "^0.4.0",
155
+ "@prosekit/core": "^0.4.1",
156
156
  "@prosekit/pm": "^0.1.2",
157
157
  "prosemirror-dropcursor": "^1.8.1",
158
158
  "prosemirror-flat-list": "^0.5.0",