@tiptap/core 2.0.0-beta.195 → 2.0.0-beta.197

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.
@@ -1936,6 +1936,135 @@ const setContent = (content, emitUpdate = false, parseOptions = {}) => ({ tr, ed
1936
1936
  return true;
1937
1937
  };
1938
1938
 
1939
+ /**
1940
+ * Returns a new `Transform` based on all steps of the passed transactions.
1941
+ */
1942
+ function combineTransactionSteps(oldDoc, transactions) {
1943
+ const transform = new prosemirrorTransform.Transform(oldDoc);
1944
+ transactions.forEach(transaction => {
1945
+ transaction.steps.forEach(step => {
1946
+ transform.step(step);
1947
+ });
1948
+ });
1949
+ return transform;
1950
+ }
1951
+
1952
+ function defaultBlockAt(match) {
1953
+ for (let i = 0; i < match.edgeCount; i += 1) {
1954
+ const { type } = match.edge(i);
1955
+ if (type.isTextblock && !type.hasRequiredAttrs()) {
1956
+ return type;
1957
+ }
1958
+ }
1959
+ return null;
1960
+ }
1961
+
1962
+ function findChildren(node, predicate) {
1963
+ const nodesWithPos = [];
1964
+ node.descendants((child, pos) => {
1965
+ if (predicate(child)) {
1966
+ nodesWithPos.push({
1967
+ node: child,
1968
+ pos,
1969
+ });
1970
+ }
1971
+ });
1972
+ return nodesWithPos;
1973
+ }
1974
+
1975
+ /**
1976
+ * Same as `findChildren` but searches only within a `range`.
1977
+ */
1978
+ function findChildrenInRange(node, range, predicate) {
1979
+ const nodesWithPos = [];
1980
+ // if (range.from === range.to) {
1981
+ // const nodeAt = node.nodeAt(range.from)
1982
+ // if (nodeAt) {
1983
+ // nodesWithPos.push({
1984
+ // node: nodeAt,
1985
+ // pos: range.from,
1986
+ // })
1987
+ // }
1988
+ // }
1989
+ node.nodesBetween(range.from, range.to, (child, pos) => {
1990
+ if (predicate(child)) {
1991
+ nodesWithPos.push({
1992
+ node: child,
1993
+ pos,
1994
+ });
1995
+ }
1996
+ });
1997
+ return nodesWithPos;
1998
+ }
1999
+
2000
+ function findParentNodeClosestToPos($pos, predicate) {
2001
+ for (let i = $pos.depth; i > 0; i -= 1) {
2002
+ const node = $pos.node(i);
2003
+ if (predicate(node)) {
2004
+ return {
2005
+ pos: i > 0 ? $pos.before(i) : 0,
2006
+ start: $pos.start(i),
2007
+ depth: i,
2008
+ node,
2009
+ };
2010
+ }
2011
+ }
2012
+ }
2013
+
2014
+ function findParentNode(predicate) {
2015
+ return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
2016
+ }
2017
+
2018
+ function getHTMLFromFragment(fragment, schema) {
2019
+ const documentFragment = prosemirrorModel.DOMSerializer
2020
+ .fromSchema(schema)
2021
+ .serializeFragment(fragment);
2022
+ const temporaryDocument = document.implementation.createHTMLDocument();
2023
+ const container = temporaryDocument.createElement('div');
2024
+ container.appendChild(documentFragment);
2025
+ return container.innerHTML;
2026
+ }
2027
+
2028
+ function getSchema(extensions) {
2029
+ const resolvedExtensions = ExtensionManager.resolve(extensions);
2030
+ return getSchemaByResolvedExtensions(resolvedExtensions);
2031
+ }
2032
+
2033
+ function generateHTML(doc, extensions) {
2034
+ const schema = getSchema(extensions);
2035
+ const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
2036
+ return getHTMLFromFragment(contentNode.content, schema);
2037
+ }
2038
+
2039
+ function generateJSON(html, extensions) {
2040
+ const schema = getSchema(extensions);
2041
+ const dom = elementFromString(html);
2042
+ return prosemirrorModel.DOMParser.fromSchema(schema)
2043
+ .parse(dom)
2044
+ .toJSON();
2045
+ }
2046
+
2047
+ function getText(node, options) {
2048
+ const range = {
2049
+ from: 0,
2050
+ to: node.content.size,
2051
+ };
2052
+ return getTextBetween(node, range, options);
2053
+ }
2054
+
2055
+ function generateText(doc, extensions, options) {
2056
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
2057
+ const schema = getSchema(extensions);
2058
+ const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
2059
+ return getText(contentNode, {
2060
+ blockSeparator,
2061
+ textSerializers: {
2062
+ ...textSerializers,
2063
+ ...getTextSerializersFromSchema(schema),
2064
+ },
2065
+ });
2066
+ }
2067
+
1939
2068
  function getMarkAttributes(state, typeOrName) {
1940
2069
  const type = getMarkType(typeOrName, state.schema);
1941
2070
  const { from, to, empty } = state.selection;
@@ -1958,118 +2087,454 @@ function getMarkAttributes(state, typeOrName) {
1958
2087
  return { ...mark.attrs };
1959
2088
  }
1960
2089
 
1961
- const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
1962
- const { selection } = tr;
1963
- const { empty, ranges } = selection;
1964
- const type = getMarkType(typeOrName, state.schema);
1965
- if (dispatch) {
1966
- if (empty) {
1967
- const oldAttributes = getMarkAttributes(state, type);
1968
- tr.addStoredMark(type.create({
1969
- ...oldAttributes,
1970
- ...attributes,
1971
- }));
1972
- }
1973
- else {
1974
- ranges.forEach(range => {
1975
- const from = range.$from.pos;
1976
- const to = range.$to.pos;
1977
- state.doc.nodesBetween(from, to, (node, pos) => {
1978
- const trimmedFrom = Math.max(pos, from);
1979
- const trimmedTo = Math.min(pos + node.nodeSize, to);
1980
- const someHasMark = node.marks.find(mark => mark.type === type);
1981
- // if there is already a mark of this type
1982
- // we know that we have to merge its attributes
1983
- // otherwise we add a fresh new mark
1984
- if (someHasMark) {
1985
- node.marks.forEach(mark => {
1986
- if (type === mark.type) {
1987
- tr.addMark(trimmedFrom, trimmedTo, type.create({
1988
- ...mark.attrs,
1989
- ...attributes,
1990
- }));
1991
- }
1992
- });
1993
- }
1994
- else {
1995
- tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
1996
- }
1997
- });
1998
- });
1999
- }
2000
- }
2001
- return true;
2002
- };
2003
-
2004
- const setMeta = (key, value) => ({ tr }) => {
2005
- tr.setMeta(key, value);
2006
- return true;
2007
- };
2008
-
2009
- const setNode = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
2090
+ function getNodeAttributes(state, typeOrName) {
2010
2091
  const type = getNodeType(typeOrName, state.schema);
2011
- // TODO: use a fallback like insertContent?
2012
- if (!type.isTextblock) {
2013
- console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.');
2014
- return false;
2015
- }
2016
- return chain()
2017
- // try to convert node to default node if needed
2018
- .command(({ commands }) => {
2019
- const canSetBlock = prosemirrorCommands.setBlockType(type, attributes)(state);
2020
- if (canSetBlock) {
2021
- return true;
2022
- }
2023
- return commands.clearNodes();
2024
- })
2025
- .command(({ state: updatedState }) => {
2026
- return prosemirrorCommands.setBlockType(type, attributes)(updatedState, dispatch);
2027
- })
2028
- .run();
2029
- };
2030
-
2031
- const setNodeSelection = position => ({ tr, dispatch }) => {
2032
- if (dispatch) {
2033
- const { doc } = tr;
2034
- const from = minMax(position, 0, doc.content.size);
2035
- const selection = prosemirrorState.NodeSelection.create(doc, from);
2036
- tr.setSelection(selection);
2092
+ const { from, to } = state.selection;
2093
+ const nodes = [];
2094
+ state.doc.nodesBetween(from, to, node => {
2095
+ nodes.push(node);
2096
+ });
2097
+ const node = nodes
2098
+ .reverse()
2099
+ .find(nodeItem => nodeItem.type.name === type.name);
2100
+ if (!node) {
2101
+ return {};
2037
2102
  }
2038
- return true;
2039
- };
2103
+ return { ...node.attrs };
2104
+ }
2040
2105
 
2041
- const setTextSelection = position => ({ tr, dispatch }) => {
2042
- if (dispatch) {
2043
- const { doc } = tr;
2044
- const { from, to } = typeof position === 'number'
2045
- ? { from: position, to: position }
2046
- : position;
2047
- const minPos = prosemirrorState.TextSelection.atStart(doc).from;
2048
- const maxPos = prosemirrorState.TextSelection.atEnd(doc).to;
2049
- const resolvedFrom = minMax(from, minPos, maxPos);
2050
- const resolvedEnd = minMax(to, minPos, maxPos);
2051
- const selection = prosemirrorState.TextSelection.create(doc, resolvedFrom, resolvedEnd);
2052
- tr.setSelection(selection);
2106
+ function getAttributes(state, typeOrName) {
2107
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2108
+ ? typeOrName
2109
+ : typeOrName.name, state.schema);
2110
+ if (schemaType === 'node') {
2111
+ return getNodeAttributes(state, typeOrName);
2053
2112
  }
2054
- return true;
2055
- };
2056
-
2057
- const sinkListItem = typeOrName => ({ state, dispatch }) => {
2058
- const type = getNodeType(typeOrName, state.schema);
2059
- return prosemirrorSchemaList.sinkListItem(type)(state, dispatch);
2060
- };
2061
-
2062
- function defaultBlockAt(match) {
2063
- for (let i = 0; i < match.edgeCount; i += 1) {
2064
- const { type } = match.edge(i);
2065
- if (type.isTextblock && !type.hasRequiredAttrs()) {
2066
- return type;
2067
- }
2113
+ if (schemaType === 'mark') {
2114
+ return getMarkAttributes(state, typeOrName);
2068
2115
  }
2069
- return null;
2116
+ return {};
2070
2117
  }
2071
2118
 
2072
- function getSplittedAttributes(extensionAttributes, typeName, attributes) {
2119
+ /**
2120
+ * Removes duplicated values within an array.
2121
+ * Supports numbers, strings and objects.
2122
+ */
2123
+ function removeDuplicates(array, by = JSON.stringify) {
2124
+ const seen = {};
2125
+ return array.filter(item => {
2126
+ const key = by(item);
2127
+ return Object.prototype.hasOwnProperty.call(seen, key)
2128
+ ? false
2129
+ : (seen[key] = true);
2130
+ });
2131
+ }
2132
+
2133
+ /**
2134
+ * Removes duplicated ranges and ranges that are
2135
+ * fully captured by other ranges.
2136
+ */
2137
+ function simplifyChangedRanges(changes) {
2138
+ const uniqueChanges = removeDuplicates(changes);
2139
+ return uniqueChanges.length === 1
2140
+ ? uniqueChanges
2141
+ : uniqueChanges.filter((change, index) => {
2142
+ const rest = uniqueChanges.filter((_, i) => i !== index);
2143
+ return !rest.some(otherChange => {
2144
+ return change.oldRange.from >= otherChange.oldRange.from
2145
+ && change.oldRange.to <= otherChange.oldRange.to
2146
+ && change.newRange.from >= otherChange.newRange.from
2147
+ && change.newRange.to <= otherChange.newRange.to;
2148
+ });
2149
+ });
2150
+ }
2151
+ /**
2152
+ * Returns a list of changed ranges
2153
+ * based on the first and last state of all steps.
2154
+ */
2155
+ function getChangedRanges(transform) {
2156
+ const { mapping, steps } = transform;
2157
+ const changes = [];
2158
+ mapping.maps.forEach((stepMap, index) => {
2159
+ const ranges = [];
2160
+ // This accounts for step changes where no range was actually altered
2161
+ // e.g. when setting a mark, node attribute, etc.
2162
+ // @ts-ignore
2163
+ if (!stepMap.ranges.length) {
2164
+ const { from, to } = steps[index];
2165
+ if (from === undefined || to === undefined) {
2166
+ return;
2167
+ }
2168
+ ranges.push({ from, to });
2169
+ }
2170
+ else {
2171
+ stepMap.forEach((from, to) => {
2172
+ ranges.push({ from, to });
2173
+ });
2174
+ }
2175
+ ranges.forEach(({ from, to }) => {
2176
+ const newStart = mapping.slice(index).map(from, -1);
2177
+ const newEnd = mapping.slice(index).map(to);
2178
+ const oldStart = mapping.invert().map(newStart, -1);
2179
+ const oldEnd = mapping.invert().map(newEnd);
2180
+ changes.push({
2181
+ oldRange: {
2182
+ from: oldStart,
2183
+ to: oldEnd,
2184
+ },
2185
+ newRange: {
2186
+ from: newStart,
2187
+ to: newEnd,
2188
+ },
2189
+ });
2190
+ });
2191
+ });
2192
+ return simplifyChangedRanges(changes);
2193
+ }
2194
+
2195
+ function getDebugJSON(node, startOffset = 0) {
2196
+ const isTopNode = node.type === node.type.schema.topNodeType;
2197
+ const increment = isTopNode ? 0 : 1;
2198
+ const from = startOffset;
2199
+ const to = from + node.nodeSize;
2200
+ const marks = node.marks.map(mark => {
2201
+ const output = {
2202
+ type: mark.type.name,
2203
+ };
2204
+ if (Object.keys(mark.attrs).length) {
2205
+ output.attrs = { ...mark.attrs };
2206
+ }
2207
+ return output;
2208
+ });
2209
+ const attrs = { ...node.attrs };
2210
+ const output = {
2211
+ type: node.type.name,
2212
+ from,
2213
+ to,
2214
+ };
2215
+ if (Object.keys(attrs).length) {
2216
+ output.attrs = attrs;
2217
+ }
2218
+ if (marks.length) {
2219
+ output.marks = marks;
2220
+ }
2221
+ if (node.content.childCount) {
2222
+ output.content = [];
2223
+ node.forEach((child, offset) => {
2224
+ var _a;
2225
+ (_a = output.content) === null || _a === void 0 ? void 0 : _a.push(getDebugJSON(child, startOffset + offset + increment));
2226
+ });
2227
+ }
2228
+ if (node.text) {
2229
+ output.text = node.text;
2230
+ }
2231
+ return output;
2232
+ }
2233
+
2234
+ function getMarksBetween(from, to, doc) {
2235
+ const marks = [];
2236
+ // get all inclusive marks on empty selection
2237
+ if (from === to) {
2238
+ doc
2239
+ .resolve(from)
2240
+ .marks()
2241
+ .forEach(mark => {
2242
+ const $pos = doc.resolve(from - 1);
2243
+ const range = getMarkRange($pos, mark.type);
2244
+ if (!range) {
2245
+ return;
2246
+ }
2247
+ marks.push({
2248
+ mark,
2249
+ ...range,
2250
+ });
2251
+ });
2252
+ }
2253
+ else {
2254
+ doc.nodesBetween(from, to, (node, pos) => {
2255
+ marks.push(...node.marks.map(mark => ({
2256
+ from: pos,
2257
+ to: pos + node.nodeSize,
2258
+ mark,
2259
+ })));
2260
+ });
2261
+ }
2262
+ return marks;
2263
+ }
2264
+
2265
+ function isMarkActive(state, typeOrName, attributes = {}) {
2266
+ const { empty, ranges } = state.selection;
2267
+ const type = typeOrName
2268
+ ? getMarkType(typeOrName, state.schema)
2269
+ : null;
2270
+ if (empty) {
2271
+ return !!(state.storedMarks || state.selection.$from.marks())
2272
+ .filter(mark => {
2273
+ if (!type) {
2274
+ return true;
2275
+ }
2276
+ return type.name === mark.type.name;
2277
+ })
2278
+ .find(mark => objectIncludes(mark.attrs, attributes, { strict: false }));
2279
+ }
2280
+ let selectionRange = 0;
2281
+ const markRanges = [];
2282
+ ranges.forEach(({ $from, $to }) => {
2283
+ const from = $from.pos;
2284
+ const to = $to.pos;
2285
+ state.doc.nodesBetween(from, to, (node, pos) => {
2286
+ if (!node.isText && !node.marks.length) {
2287
+ return;
2288
+ }
2289
+ const relativeFrom = Math.max(from, pos);
2290
+ const relativeTo = Math.min(to, pos + node.nodeSize);
2291
+ const range = relativeTo - relativeFrom;
2292
+ selectionRange += range;
2293
+ markRanges.push(...node.marks.map(mark => ({
2294
+ mark,
2295
+ from: relativeFrom,
2296
+ to: relativeTo,
2297
+ })));
2298
+ });
2299
+ });
2300
+ if (selectionRange === 0) {
2301
+ return false;
2302
+ }
2303
+ // calculate range of matched mark
2304
+ const matchedRange = markRanges
2305
+ .filter(markRange => {
2306
+ if (!type) {
2307
+ return true;
2308
+ }
2309
+ return type.name === markRange.mark.type.name;
2310
+ })
2311
+ .filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
2312
+ .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2313
+ // calculate range of marks that excludes the searched mark
2314
+ // for example `code` doesn’t allow any other marks
2315
+ const excludedRange = markRanges
2316
+ .filter(markRange => {
2317
+ if (!type) {
2318
+ return true;
2319
+ }
2320
+ return markRange.mark.type !== type
2321
+ && markRange.mark.type.excludes(type);
2322
+ })
2323
+ .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2324
+ // we only include the result of `excludedRange`
2325
+ // if there is a match at all
2326
+ const range = matchedRange > 0
2327
+ ? matchedRange + excludedRange
2328
+ : matchedRange;
2329
+ return range >= selectionRange;
2330
+ }
2331
+
2332
+ function isActive(state, name, attributes = {}) {
2333
+ if (!name) {
2334
+ return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
2335
+ }
2336
+ const schemaType = getSchemaTypeNameByName(name, state.schema);
2337
+ if (schemaType === 'node') {
2338
+ return isNodeActive(state, name, attributes);
2339
+ }
2340
+ if (schemaType === 'mark') {
2341
+ return isMarkActive(state, name, attributes);
2342
+ }
2343
+ return false;
2344
+ }
2345
+
2346
+ function isList(name, extensions) {
2347
+ const { nodeExtensions } = splitExtensions(extensions);
2348
+ const extension = nodeExtensions.find(item => item.name === name);
2349
+ if (!extension) {
2350
+ return false;
2351
+ }
2352
+ const context = {
2353
+ name: extension.name,
2354
+ options: extension.options,
2355
+ storage: extension.storage,
2356
+ };
2357
+ const group = callOrReturn(getExtensionField(extension, 'group', context));
2358
+ if (typeof group !== 'string') {
2359
+ return false;
2360
+ }
2361
+ return group.split(' ').includes('list');
2362
+ }
2363
+
2364
+ function isNodeEmpty(node) {
2365
+ var _a;
2366
+ const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
2367
+ const content = node.toJSON();
2368
+ return JSON.stringify(defaultContent) === JSON.stringify(content);
2369
+ }
2370
+
2371
+ function isNodeSelection(value) {
2372
+ return value instanceof prosemirrorState.NodeSelection;
2373
+ }
2374
+
2375
+ function posToDOMRect(view, from, to) {
2376
+ const minPos = 0;
2377
+ const maxPos = view.state.doc.content.size;
2378
+ const resolvedFrom = minMax(from, minPos, maxPos);
2379
+ const resolvedEnd = minMax(to, minPos, maxPos);
2380
+ const start = view.coordsAtPos(resolvedFrom);
2381
+ const end = view.coordsAtPos(resolvedEnd, -1);
2382
+ const top = Math.min(start.top, end.top);
2383
+ const bottom = Math.max(start.bottom, end.bottom);
2384
+ const left = Math.min(start.left, end.left);
2385
+ const right = Math.max(start.right, end.right);
2386
+ const width = right - left;
2387
+ const height = bottom - top;
2388
+ const x = left;
2389
+ const y = top;
2390
+ const data = {
2391
+ top,
2392
+ bottom,
2393
+ left,
2394
+ right,
2395
+ width,
2396
+ height,
2397
+ x,
2398
+ y,
2399
+ };
2400
+ return {
2401
+ ...data,
2402
+ toJSON: () => data,
2403
+ };
2404
+ }
2405
+
2406
+ function canSetMark(state, tr, newMarkType) {
2407
+ var _a;
2408
+ const { selection } = tr;
2409
+ let cursor = null;
2410
+ if (isTextSelection(selection)) {
2411
+ cursor = selection.$cursor;
2412
+ }
2413
+ if (cursor) {
2414
+ const currentMarks = (_a = state.storedMarks) !== null && _a !== void 0 ? _a : cursor.marks();
2415
+ // There can be no current marks that exclude the new mark
2416
+ return !!newMarkType.isInSet(currentMarks) || !currentMarks.some(mark => mark.type.excludes(newMarkType));
2417
+ }
2418
+ const { ranges } = selection;
2419
+ return ranges.some(({ $from, $to }) => {
2420
+ let someNodeSupportsMark = $from.depth === 0 ? state.doc.inlineContent && state.doc.type.allowsMarkType(newMarkType) : false;
2421
+ state.doc.nodesBetween($from.pos, $to.pos, (node, _pos, parent) => {
2422
+ // If we already found a mark that we can enable, return false to bypass the remaining search
2423
+ if (someNodeSupportsMark) {
2424
+ return false;
2425
+ }
2426
+ if (node.isInline) {
2427
+ const parentAllowsMarkType = !parent || parent.type.allowsMarkType(newMarkType);
2428
+ const currentMarksAllowMarkType = !!newMarkType.isInSet(node.marks) || !node.marks.some(otherMark => otherMark.type.excludes(newMarkType));
2429
+ someNodeSupportsMark = parentAllowsMarkType && currentMarksAllowMarkType;
2430
+ }
2431
+ return !someNodeSupportsMark;
2432
+ });
2433
+ return someNodeSupportsMark;
2434
+ });
2435
+ }
2436
+ const setMark = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
2437
+ const { selection } = tr;
2438
+ const { empty, ranges } = selection;
2439
+ const type = getMarkType(typeOrName, state.schema);
2440
+ if (dispatch) {
2441
+ if (empty) {
2442
+ const oldAttributes = getMarkAttributes(state, type);
2443
+ tr.addStoredMark(type.create({
2444
+ ...oldAttributes,
2445
+ ...attributes,
2446
+ }));
2447
+ }
2448
+ else {
2449
+ ranges.forEach(range => {
2450
+ const from = range.$from.pos;
2451
+ const to = range.$to.pos;
2452
+ state.doc.nodesBetween(from, to, (node, pos) => {
2453
+ const trimmedFrom = Math.max(pos, from);
2454
+ const trimmedTo = Math.min(pos + node.nodeSize, to);
2455
+ const someHasMark = node.marks.find(mark => mark.type === type);
2456
+ // if there is already a mark of this type
2457
+ // we know that we have to merge its attributes
2458
+ // otherwise we add a fresh new mark
2459
+ if (someHasMark) {
2460
+ node.marks.forEach(mark => {
2461
+ if (type === mark.type) {
2462
+ tr.addMark(trimmedFrom, trimmedTo, type.create({
2463
+ ...mark.attrs,
2464
+ ...attributes,
2465
+ }));
2466
+ }
2467
+ });
2468
+ }
2469
+ else {
2470
+ tr.addMark(trimmedFrom, trimmedTo, type.create(attributes));
2471
+ }
2472
+ });
2473
+ });
2474
+ }
2475
+ }
2476
+ return canSetMark(state, tr, type);
2477
+ };
2478
+
2479
+ const setMeta = (key, value) => ({ tr }) => {
2480
+ tr.setMeta(key, value);
2481
+ return true;
2482
+ };
2483
+
2484
+ const setNode = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
2485
+ const type = getNodeType(typeOrName, state.schema);
2486
+ // TODO: use a fallback like insertContent?
2487
+ if (!type.isTextblock) {
2488
+ console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.');
2489
+ return false;
2490
+ }
2491
+ return chain()
2492
+ // try to convert node to default node if needed
2493
+ .command(({ commands }) => {
2494
+ const canSetBlock = prosemirrorCommands.setBlockType(type, attributes)(state);
2495
+ if (canSetBlock) {
2496
+ return true;
2497
+ }
2498
+ return commands.clearNodes();
2499
+ })
2500
+ .command(({ state: updatedState }) => {
2501
+ return prosemirrorCommands.setBlockType(type, attributes)(updatedState, dispatch);
2502
+ })
2503
+ .run();
2504
+ };
2505
+
2506
+ const setNodeSelection = position => ({ tr, dispatch }) => {
2507
+ if (dispatch) {
2508
+ const { doc } = tr;
2509
+ const from = minMax(position, 0, doc.content.size);
2510
+ const selection = prosemirrorState.NodeSelection.create(doc, from);
2511
+ tr.setSelection(selection);
2512
+ }
2513
+ return true;
2514
+ };
2515
+
2516
+ const setTextSelection = position => ({ tr, dispatch }) => {
2517
+ if (dispatch) {
2518
+ const { doc } = tr;
2519
+ const { from, to } = typeof position === 'number'
2520
+ ? { from: position, to: position }
2521
+ : position;
2522
+ const minPos = prosemirrorState.TextSelection.atStart(doc).from;
2523
+ const maxPos = prosemirrorState.TextSelection.atEnd(doc).to;
2524
+ const resolvedFrom = minMax(from, minPos, maxPos);
2525
+ const resolvedEnd = minMax(to, minPos, maxPos);
2526
+ const selection = prosemirrorState.TextSelection.create(doc, resolvedFrom, resolvedEnd);
2527
+ tr.setSelection(selection);
2528
+ }
2529
+ return true;
2530
+ };
2531
+
2532
+ const sinkListItem = typeOrName => ({ state, dispatch }) => {
2533
+ const type = getNodeType(typeOrName, state.schema);
2534
+ return prosemirrorSchemaList.sinkListItem(type)(state, dispatch);
2535
+ };
2536
+
2537
+ function getSplittedAttributes(extensionAttributes, typeName, attributes) {
2073
2538
  return Object.fromEntries(Object
2074
2539
  .entries(attributes)
2075
2540
  .filter(([name]) => {
@@ -2241,42 +2706,6 @@ const splitListItem = typeOrName => ({ tr, state, dispatch, editor, }) => {
2241
2706
  return true;
2242
2707
  };
2243
2708
 
2244
- function findParentNodeClosestToPos($pos, predicate) {
2245
- for (let i = $pos.depth; i > 0; i -= 1) {
2246
- const node = $pos.node(i);
2247
- if (predicate(node)) {
2248
- return {
2249
- pos: i > 0 ? $pos.before(i) : 0,
2250
- start: $pos.start(i),
2251
- depth: i,
2252
- node,
2253
- };
2254
- }
2255
- }
2256
- }
2257
-
2258
- function findParentNode(predicate) {
2259
- return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
2260
- }
2261
-
2262
- function isList(name, extensions) {
2263
- const { nodeExtensions } = splitExtensions(extensions);
2264
- const extension = nodeExtensions.find(item => item.name === name);
2265
- if (!extension) {
2266
- return false;
2267
- }
2268
- const context = {
2269
- name: extension.name,
2270
- options: extension.options,
2271
- storage: extension.storage,
2272
- };
2273
- const group = callOrReturn(getExtensionField(extension, 'group', context));
2274
- if (typeof group !== 'string') {
2275
- return false;
2276
- }
2277
- return group.split(' ').includes('list');
2278
- }
2279
-
2280
2709
  const joinListBackwards = (tr, listType) => {
2281
2710
  const list = findParentNode(node => node.type === listType)(tr.selection);
2282
2711
  if (!list) {
@@ -2358,73 +2787,6 @@ const toggleList = (listTypeOrName, itemTypeOrName) => ({ editor, tr, state, dis
2358
2787
  .run();
2359
2788
  };
2360
2789
 
2361
- function isMarkActive(state, typeOrName, attributes = {}) {
2362
- const { empty, ranges } = state.selection;
2363
- const type = typeOrName
2364
- ? getMarkType(typeOrName, state.schema)
2365
- : null;
2366
- if (empty) {
2367
- return !!(state.storedMarks || state.selection.$from.marks())
2368
- .filter(mark => {
2369
- if (!type) {
2370
- return true;
2371
- }
2372
- return type.name === mark.type.name;
2373
- })
2374
- .find(mark => objectIncludes(mark.attrs, attributes, { strict: false }));
2375
- }
2376
- let selectionRange = 0;
2377
- const markRanges = [];
2378
- ranges.forEach(({ $from, $to }) => {
2379
- const from = $from.pos;
2380
- const to = $to.pos;
2381
- state.doc.nodesBetween(from, to, (node, pos) => {
2382
- if (!node.isText && !node.marks.length) {
2383
- return;
2384
- }
2385
- const relativeFrom = Math.max(from, pos);
2386
- const relativeTo = Math.min(to, pos + node.nodeSize);
2387
- const range = relativeTo - relativeFrom;
2388
- selectionRange += range;
2389
- markRanges.push(...node.marks.map(mark => ({
2390
- mark,
2391
- from: relativeFrom,
2392
- to: relativeTo,
2393
- })));
2394
- });
2395
- });
2396
- if (selectionRange === 0) {
2397
- return false;
2398
- }
2399
- // calculate range of matched mark
2400
- const matchedRange = markRanges
2401
- .filter(markRange => {
2402
- if (!type) {
2403
- return true;
2404
- }
2405
- return type.name === markRange.mark.type.name;
2406
- })
2407
- .filter(markRange => objectIncludes(markRange.mark.attrs, attributes, { strict: false }))
2408
- .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2409
- // calculate range of marks that excludes the searched mark
2410
- // for example `code` doesn’t allow any other marks
2411
- const excludedRange = markRanges
2412
- .filter(markRange => {
2413
- if (!type) {
2414
- return true;
2415
- }
2416
- return markRange.mark.type !== type
2417
- && markRange.mark.type.excludes(type);
2418
- })
2419
- .reduce((sum, markRange) => sum + markRange.to - markRange.from, 0);
2420
- // we only include the result of `excludedRange`
2421
- // if there is a match at all
2422
- const range = matchedRange > 0
2423
- ? matchedRange + excludedRange
2424
- : matchedRange;
2425
- return range >= selectionRange;
2426
- }
2427
-
2428
2790
  const toggleMark = (typeOrName, attributes = {}, options = {}) => ({ state, commands }) => {
2429
2791
  const { extendEmptyMarkRange = false } = options;
2430
2792
  const type = getMarkType(typeOrName, state.schema);
@@ -2812,78 +3174,10 @@ var extensions = /*#__PURE__*/Object.freeze({
2812
3174
  ClipboardTextSerializer: ClipboardTextSerializer,
2813
3175
  Commands: Commands,
2814
3176
  Editable: Editable,
2815
- FocusEvents: FocusEvents,
2816
- Keymap: Keymap,
2817
- Tabindex: Tabindex
2818
- });
2819
-
2820
- function getNodeAttributes(state, typeOrName) {
2821
- const type = getNodeType(typeOrName, state.schema);
2822
- const { from, to } = state.selection;
2823
- const nodes = [];
2824
- state.doc.nodesBetween(from, to, node => {
2825
- nodes.push(node);
2826
- });
2827
- const node = nodes
2828
- .reverse()
2829
- .find(nodeItem => nodeItem.type.name === type.name);
2830
- if (!node) {
2831
- return {};
2832
- }
2833
- return { ...node.attrs };
2834
- }
2835
-
2836
- function getAttributes(state, typeOrName) {
2837
- const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2838
- ? typeOrName
2839
- : typeOrName.name, state.schema);
2840
- if (schemaType === 'node') {
2841
- return getNodeAttributes(state, typeOrName);
2842
- }
2843
- if (schemaType === 'mark') {
2844
- return getMarkAttributes(state, typeOrName);
2845
- }
2846
- return {};
2847
- }
2848
-
2849
- function getHTMLFromFragment(fragment, schema) {
2850
- const documentFragment = prosemirrorModel.DOMSerializer
2851
- .fromSchema(schema)
2852
- .serializeFragment(fragment);
2853
- const temporaryDocument = document.implementation.createHTMLDocument();
2854
- const container = temporaryDocument.createElement('div');
2855
- container.appendChild(documentFragment);
2856
- return container.innerHTML;
2857
- }
2858
-
2859
- function getText(node, options) {
2860
- const range = {
2861
- from: 0,
2862
- to: node.content.size,
2863
- };
2864
- return getTextBetween(node, range, options);
2865
- }
2866
-
2867
- function isActive(state, name, attributes = {}) {
2868
- if (!name) {
2869
- return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
2870
- }
2871
- const schemaType = getSchemaTypeNameByName(name, state.schema);
2872
- if (schemaType === 'node') {
2873
- return isNodeActive(state, name, attributes);
2874
- }
2875
- if (schemaType === 'mark') {
2876
- return isMarkActive(state, name, attributes);
2877
- }
2878
- return false;
2879
- }
2880
-
2881
- function isNodeEmpty(node) {
2882
- var _a;
2883
- const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
2884
- const content = node.toJSON();
2885
- return JSON.stringify(defaultContent) === JSON.stringify(content);
2886
- }
3177
+ FocusEvents: FocusEvents,
3178
+ Keymap: Keymap,
3179
+ Tabindex: Tabindex
3180
+ });
2887
3181
 
2888
3182
  const style = `.ProseMirror {
2889
3183
  position: relative;
@@ -3189,407 +3483,143 @@ class Editor extends EventEmitter {
3189
3483
  /**
3190
3484
  * Creates all node views.
3191
3485
  */
3192
- createNodeViews() {
3193
- this.view.setProps({
3194
- nodeViews: this.extensionManager.nodeViews,
3195
- });
3196
- }
3197
- captureTransaction(fn) {
3198
- this.isCapturingTransaction = true;
3199
- fn();
3200
- this.isCapturingTransaction = false;
3201
- const tr = this.capturedTransaction;
3202
- this.capturedTransaction = null;
3203
- return tr;
3204
- }
3205
- /**
3206
- * The callback over which to send transactions (state updates) produced by the view.
3207
- *
3208
- * @param transaction An editor state transaction
3209
- */
3210
- dispatchTransaction(transaction) {
3211
- if (this.isCapturingTransaction) {
3212
- if (!this.capturedTransaction) {
3213
- this.capturedTransaction = transaction;
3214
- return;
3215
- }
3216
- transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
3217
- return;
3218
- }
3219
- const state = this.state.apply(transaction);
3220
- const selectionHasChanged = !this.state.selection.eq(state.selection);
3221
- this.view.updateState(state);
3222
- this.emit('transaction', {
3223
- editor: this,
3224
- transaction,
3225
- });
3226
- if (selectionHasChanged) {
3227
- this.emit('selectionUpdate', {
3228
- editor: this,
3229
- transaction,
3230
- });
3231
- }
3232
- const focus = transaction.getMeta('focus');
3233
- const blur = transaction.getMeta('blur');
3234
- if (focus) {
3235
- this.emit('focus', {
3236
- editor: this,
3237
- event: focus.event,
3238
- transaction,
3239
- });
3240
- }
3241
- if (blur) {
3242
- this.emit('blur', {
3243
- editor: this,
3244
- event: blur.event,
3245
- transaction,
3246
- });
3247
- }
3248
- if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
3249
- return;
3250
- }
3251
- this.emit('update', {
3252
- editor: this,
3253
- transaction,
3254
- });
3255
- }
3256
- /**
3257
- * Get attributes of the currently selected node or mark.
3258
- */
3259
- getAttributes(nameOrType) {
3260
- return getAttributes(this.state, nameOrType);
3261
- }
3262
- isActive(nameOrAttributes, attributesOrUndefined) {
3263
- const name = typeof nameOrAttributes === 'string'
3264
- ? nameOrAttributes
3265
- : null;
3266
- const attributes = typeof nameOrAttributes === 'string'
3267
- ? attributesOrUndefined
3268
- : nameOrAttributes;
3269
- return isActive(this.state, name, attributes);
3270
- }
3271
- /**
3272
- * Get the document as JSON.
3273
- */
3274
- getJSON() {
3275
- return this.state.doc.toJSON();
3276
- }
3277
- /**
3278
- * Get the document as HTML.
3279
- */
3280
- getHTML() {
3281
- return getHTMLFromFragment(this.state.doc.content, this.schema);
3282
- }
3283
- /**
3284
- * Get the document as text.
3285
- */
3286
- getText(options) {
3287
- const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3288
- return getText(this.state.doc, {
3289
- blockSeparator,
3290
- textSerializers: {
3291
- ...textSerializers,
3292
- ...getTextSerializersFromSchema(this.schema),
3293
- },
3294
- });
3295
- }
3296
- /**
3297
- * Check if there is no content.
3298
- */
3299
- get isEmpty() {
3300
- return isNodeEmpty(this.state.doc);
3301
- }
3302
- /**
3303
- * Get the number of characters for the current document.
3304
- *
3305
- * @deprecated
3306
- */
3307
- getCharacterCount() {
3308
- console.warn('[tiptap warn]: "editor.getCharacterCount()" is deprecated. Please use "editor.storage.characterCount.characters()" instead.');
3309
- return this.state.doc.content.size - 2;
3310
- }
3311
- /**
3312
- * Destroy the editor.
3313
- */
3314
- destroy() {
3315
- this.emit('destroy');
3316
- if (this.view) {
3317
- this.view.destroy();
3318
- }
3319
- this.removeAllListeners();
3320
- }
3321
- /**
3322
- * Check if the editor is already destroyed.
3323
- */
3324
- get isDestroyed() {
3325
- var _a;
3326
- // @ts-ignore
3327
- return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3328
- }
3329
- }
3330
-
3331
- /**
3332
- * Returns a new `Transform` based on all steps of the passed transactions.
3333
- */
3334
- function combineTransactionSteps(oldDoc, transactions) {
3335
- const transform = new prosemirrorTransform.Transform(oldDoc);
3336
- transactions.forEach(transaction => {
3337
- transaction.steps.forEach(step => {
3338
- transform.step(step);
3339
- });
3340
- });
3341
- return transform;
3342
- }
3343
-
3344
- function findChildren(node, predicate) {
3345
- const nodesWithPos = [];
3346
- node.descendants((child, pos) => {
3347
- if (predicate(child)) {
3348
- nodesWithPos.push({
3349
- node: child,
3350
- pos,
3351
- });
3352
- }
3353
- });
3354
- return nodesWithPos;
3355
- }
3356
-
3357
- /**
3358
- * Same as `findChildren` but searches only within a `range`.
3359
- */
3360
- function findChildrenInRange(node, range, predicate) {
3361
- const nodesWithPos = [];
3362
- // if (range.from === range.to) {
3363
- // const nodeAt = node.nodeAt(range.from)
3364
- // if (nodeAt) {
3365
- // nodesWithPos.push({
3366
- // node: nodeAt,
3367
- // pos: range.from,
3368
- // })
3369
- // }
3370
- // }
3371
- node.nodesBetween(range.from, range.to, (child, pos) => {
3372
- if (predicate(child)) {
3373
- nodesWithPos.push({
3374
- node: child,
3375
- pos,
3376
- });
3377
- }
3378
- });
3379
- return nodesWithPos;
3380
- }
3381
-
3382
- function getSchema(extensions) {
3383
- const resolvedExtensions = ExtensionManager.resolve(extensions);
3384
- return getSchemaByResolvedExtensions(resolvedExtensions);
3385
- }
3386
-
3387
- function generateHTML(doc, extensions) {
3388
- const schema = getSchema(extensions);
3389
- const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
3390
- return getHTMLFromFragment(contentNode.content, schema);
3391
- }
3392
-
3393
- function generateJSON(html, extensions) {
3394
- const schema = getSchema(extensions);
3395
- const dom = elementFromString(html);
3396
- return prosemirrorModel.DOMParser.fromSchema(schema)
3397
- .parse(dom)
3398
- .toJSON();
3399
- }
3400
-
3401
- function generateText(doc, extensions, options) {
3402
- const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3403
- const schema = getSchema(extensions);
3404
- const contentNode = prosemirrorModel.Node.fromJSON(schema, doc);
3405
- return getText(contentNode, {
3406
- blockSeparator,
3407
- textSerializers: {
3408
- ...textSerializers,
3409
- ...getTextSerializersFromSchema(schema),
3410
- },
3411
- });
3412
- }
3413
-
3414
- /**
3415
- * Removes duplicated values within an array.
3416
- * Supports numbers, strings and objects.
3417
- */
3418
- function removeDuplicates(array, by = JSON.stringify) {
3419
- const seen = {};
3420
- return array.filter(item => {
3421
- const key = by(item);
3422
- return Object.prototype.hasOwnProperty.call(seen, key)
3423
- ? false
3424
- : (seen[key] = true);
3425
- });
3426
- }
3427
-
3428
- /**
3429
- * Removes duplicated ranges and ranges that are
3430
- * fully captured by other ranges.
3431
- */
3432
- function simplifyChangedRanges(changes) {
3433
- const uniqueChanges = removeDuplicates(changes);
3434
- return uniqueChanges.length === 1
3435
- ? uniqueChanges
3436
- : uniqueChanges.filter((change, index) => {
3437
- const rest = uniqueChanges.filter((_, i) => i !== index);
3438
- return !rest.some(otherChange => {
3439
- return change.oldRange.from >= otherChange.oldRange.from
3440
- && change.oldRange.to <= otherChange.oldRange.to
3441
- && change.newRange.from >= otherChange.newRange.from
3442
- && change.newRange.to <= otherChange.newRange.to;
3443
- });
3486
+ createNodeViews() {
3487
+ this.view.setProps({
3488
+ nodeViews: this.extensionManager.nodeViews,
3444
3489
  });
3445
- }
3446
- /**
3447
- * Returns a list of changed ranges
3448
- * based on the first and last state of all steps.
3449
- */
3450
- function getChangedRanges(transform) {
3451
- const { mapping, steps } = transform;
3452
- const changes = [];
3453
- mapping.maps.forEach((stepMap, index) => {
3454
- const ranges = [];
3455
- // This accounts for step changes where no range was actually altered
3456
- // e.g. when setting a mark, node attribute, etc.
3457
- // @ts-ignore
3458
- if (!stepMap.ranges.length) {
3459
- const { from, to } = steps[index];
3460
- if (from === undefined || to === undefined) {
3490
+ }
3491
+ captureTransaction(fn) {
3492
+ this.isCapturingTransaction = true;
3493
+ fn();
3494
+ this.isCapturingTransaction = false;
3495
+ const tr = this.capturedTransaction;
3496
+ this.capturedTransaction = null;
3497
+ return tr;
3498
+ }
3499
+ /**
3500
+ * The callback over which to send transactions (state updates) produced by the view.
3501
+ *
3502
+ * @param transaction An editor state transaction
3503
+ */
3504
+ dispatchTransaction(transaction) {
3505
+ if (this.isCapturingTransaction) {
3506
+ if (!this.capturedTransaction) {
3507
+ this.capturedTransaction = transaction;
3461
3508
  return;
3462
3509
  }
3463
- ranges.push({ from, to });
3510
+ transaction.steps.forEach(step => { var _a; return (_a = this.capturedTransaction) === null || _a === void 0 ? void 0 : _a.step(step); });
3511
+ return;
3464
3512
  }
3465
- else {
3466
- stepMap.forEach((from, to) => {
3467
- ranges.push({ from, to });
3513
+ const state = this.state.apply(transaction);
3514
+ const selectionHasChanged = !this.state.selection.eq(state.selection);
3515
+ this.view.updateState(state);
3516
+ this.emit('transaction', {
3517
+ editor: this,
3518
+ transaction,
3519
+ });
3520
+ if (selectionHasChanged) {
3521
+ this.emit('selectionUpdate', {
3522
+ editor: this,
3523
+ transaction,
3468
3524
  });
3469
3525
  }
3470
- ranges.forEach(({ from, to }) => {
3471
- const newStart = mapping.slice(index).map(from, -1);
3472
- const newEnd = mapping.slice(index).map(to);
3473
- const oldStart = mapping.invert().map(newStart, -1);
3474
- const oldEnd = mapping.invert().map(newEnd);
3475
- changes.push({
3476
- oldRange: {
3477
- from: oldStart,
3478
- to: oldEnd,
3479
- },
3480
- newRange: {
3481
- from: newStart,
3482
- to: newEnd,
3483
- },
3526
+ const focus = transaction.getMeta('focus');
3527
+ const blur = transaction.getMeta('blur');
3528
+ if (focus) {
3529
+ this.emit('focus', {
3530
+ editor: this,
3531
+ event: focus.event,
3532
+ transaction,
3484
3533
  });
3485
- });
3486
- });
3487
- return simplifyChangedRanges(changes);
3488
- }
3489
-
3490
- function getDebugJSON(node, startOffset = 0) {
3491
- const isTopNode = node.type === node.type.schema.topNodeType;
3492
- const increment = isTopNode ? 0 : 1;
3493
- const from = startOffset;
3494
- const to = from + node.nodeSize;
3495
- const marks = node.marks.map(mark => {
3496
- const output = {
3497
- type: mark.type.name,
3498
- };
3499
- if (Object.keys(mark.attrs).length) {
3500
- output.attrs = { ...mark.attrs };
3501
3534
  }
3502
- return output;
3503
- });
3504
- const attrs = { ...node.attrs };
3505
- const output = {
3506
- type: node.type.name,
3507
- from,
3508
- to,
3509
- };
3510
- if (Object.keys(attrs).length) {
3511
- output.attrs = attrs;
3535
+ if (blur) {
3536
+ this.emit('blur', {
3537
+ editor: this,
3538
+ event: blur.event,
3539
+ transaction,
3540
+ });
3541
+ }
3542
+ if (!transaction.docChanged || transaction.getMeta('preventUpdate')) {
3543
+ return;
3544
+ }
3545
+ this.emit('update', {
3546
+ editor: this,
3547
+ transaction,
3548
+ });
3512
3549
  }
3513
- if (marks.length) {
3514
- output.marks = marks;
3550
+ /**
3551
+ * Get attributes of the currently selected node or mark.
3552
+ */
3553
+ getAttributes(nameOrType) {
3554
+ return getAttributes(this.state, nameOrType);
3515
3555
  }
3516
- if (node.content.childCount) {
3517
- output.content = [];
3518
- node.forEach((child, offset) => {
3519
- var _a;
3520
- (_a = output.content) === null || _a === void 0 ? void 0 : _a.push(getDebugJSON(child, startOffset + offset + increment));
3521
- });
3556
+ isActive(nameOrAttributes, attributesOrUndefined) {
3557
+ const name = typeof nameOrAttributes === 'string'
3558
+ ? nameOrAttributes
3559
+ : null;
3560
+ const attributes = typeof nameOrAttributes === 'string'
3561
+ ? attributesOrUndefined
3562
+ : nameOrAttributes;
3563
+ return isActive(this.state, name, attributes);
3522
3564
  }
3523
- if (node.text) {
3524
- output.text = node.text;
3565
+ /**
3566
+ * Get the document as JSON.
3567
+ */
3568
+ getJSON() {
3569
+ return this.state.doc.toJSON();
3525
3570
  }
3526
- return output;
3527
- }
3528
-
3529
- function getMarksBetween(from, to, doc) {
3530
- const marks = [];
3531
- // get all inclusive marks on empty selection
3532
- if (from === to) {
3533
- doc
3534
- .resolve(from)
3535
- .marks()
3536
- .forEach(mark => {
3537
- const $pos = doc.resolve(from - 1);
3538
- const range = getMarkRange($pos, mark.type);
3539
- if (!range) {
3540
- return;
3541
- }
3542
- marks.push({
3543
- mark,
3544
- ...range,
3545
- });
3546
- });
3571
+ /**
3572
+ * Get the document as HTML.
3573
+ */
3574
+ getHTML() {
3575
+ return getHTMLFromFragment(this.state.doc.content, this.schema);
3547
3576
  }
3548
- else {
3549
- doc.nodesBetween(from, to, (node, pos) => {
3550
- marks.push(...node.marks.map(mark => ({
3551
- from: pos,
3552
- to: pos + node.nodeSize,
3553
- mark,
3554
- })));
3577
+ /**
3578
+ * Get the document as text.
3579
+ */
3580
+ getText(options) {
3581
+ const { blockSeparator = '\n\n', textSerializers = {}, } = options || {};
3582
+ return getText(this.state.doc, {
3583
+ blockSeparator,
3584
+ textSerializers: {
3585
+ ...textSerializers,
3586
+ ...getTextSerializersFromSchema(this.schema),
3587
+ },
3555
3588
  });
3556
3589
  }
3557
- return marks;
3558
- }
3559
-
3560
- function isNodeSelection(value) {
3561
- return value instanceof prosemirrorState.NodeSelection;
3562
- }
3563
-
3564
- function posToDOMRect(view, from, to) {
3565
- const minPos = 0;
3566
- const maxPos = view.state.doc.content.size;
3567
- const resolvedFrom = minMax(from, minPos, maxPos);
3568
- const resolvedEnd = minMax(to, minPos, maxPos);
3569
- const start = view.coordsAtPos(resolvedFrom);
3570
- const end = view.coordsAtPos(resolvedEnd, -1);
3571
- const top = Math.min(start.top, end.top);
3572
- const bottom = Math.max(start.bottom, end.bottom);
3573
- const left = Math.min(start.left, end.left);
3574
- const right = Math.max(start.right, end.right);
3575
- const width = right - left;
3576
- const height = bottom - top;
3577
- const x = left;
3578
- const y = top;
3579
- const data = {
3580
- top,
3581
- bottom,
3582
- left,
3583
- right,
3584
- width,
3585
- height,
3586
- x,
3587
- y,
3588
- };
3589
- return {
3590
- ...data,
3591
- toJSON: () => data,
3592
- };
3590
+ /**
3591
+ * Check if there is no content.
3592
+ */
3593
+ get isEmpty() {
3594
+ return isNodeEmpty(this.state.doc);
3595
+ }
3596
+ /**
3597
+ * Get the number of characters for the current document.
3598
+ *
3599
+ * @deprecated
3600
+ */
3601
+ getCharacterCount() {
3602
+ console.warn('[tiptap warn]: "editor.getCharacterCount()" is deprecated. Please use "editor.storage.characterCount.characters()" instead.');
3603
+ return this.state.doc.content.size - 2;
3604
+ }
3605
+ /**
3606
+ * Destroy the editor.
3607
+ */
3608
+ destroy() {
3609
+ this.emit('destroy');
3610
+ if (this.view) {
3611
+ this.view.destroy();
3612
+ }
3613
+ this.removeAllListeners();
3614
+ }
3615
+ /**
3616
+ * Check if the editor is already destroyed.
3617
+ */
3618
+ get isDestroyed() {
3619
+ var _a;
3620
+ // @ts-ignore
3621
+ return !((_a = this.view) === null || _a === void 0 ? void 0 : _a.docView);
3622
+ }
3593
3623
  }
3594
3624
 
3595
3625
  /**