@tiptap/core 2.0.0-beta.154 → 2.0.0-beta.158

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.
@@ -1422,6 +1422,9 @@ const joinListBackwards = (tr, listType) => {
1422
1422
  return true;
1423
1423
  }
1424
1424
  const before = tr.doc.resolve(Math.max(0, list.pos - 1)).before(list.depth);
1425
+ if (before === undefined) {
1426
+ return true;
1427
+ }
1425
1428
  const nodeBefore = tr.doc.nodeAt(before);
1426
1429
  const canJoinBackwards = list.node.type === (nodeBefore === null || nodeBefore === void 0 ? void 0 : nodeBefore.type)
1427
1430
  && canJoin(tr.doc, list.pos);
@@ -1437,6 +1440,9 @@ const joinListForwards = (tr, listType) => {
1437
1440
  return true;
1438
1441
  }
1439
1442
  const after = tr.doc.resolve(list.start).after(list.depth);
1443
+ if (after === undefined) {
1444
+ return true;
1445
+ }
1440
1446
  const nodeAfter = tr.doc.nodeAt(after);
1441
1447
  const canJoinForwards = list.node.type === (nodeAfter === null || nodeAfter === void 0 ? void 0 : nodeAfter.type)
1442
1448
  && canJoin(tr.doc, after);
@@ -1863,144 +1869,6 @@ const FocusEvents = Extension.create({
1863
1869
  },
1864
1870
  });
1865
1871
 
1866
- const Keymap = Extension.create({
1867
- name: 'keymap',
1868
- addKeyboardShortcuts() {
1869
- const handleBackspace = () => this.editor.commands.first(({ commands }) => [
1870
- () => commands.undoInputRule(),
1871
- () => commands.deleteSelection(),
1872
- () => commands.joinBackward(),
1873
- () => commands.selectNodeBackward(),
1874
- ]);
1875
- const handleDelete = () => this.editor.commands.first(({ commands }) => [
1876
- () => commands.deleteSelection(),
1877
- () => commands.joinForward(),
1878
- () => commands.selectNodeForward(),
1879
- ]);
1880
- return {
1881
- Enter: () => this.editor.commands.first(({ commands }) => [
1882
- () => commands.newlineInCode(),
1883
- () => commands.createParagraphNear(),
1884
- () => commands.liftEmptyBlock(),
1885
- () => commands.splitBlock(),
1886
- ]),
1887
- 'Mod-Enter': () => this.editor.commands.exitCode(),
1888
- Backspace: handleBackspace,
1889
- 'Mod-Backspace': handleBackspace,
1890
- 'Shift-Backspace': handleBackspace,
1891
- Delete: handleDelete,
1892
- 'Mod-Delete': handleDelete,
1893
- 'Mod-a': () => this.editor.commands.selectAll(),
1894
- };
1895
- },
1896
- });
1897
-
1898
- const Tabindex = Extension.create({
1899
- name: 'tabindex',
1900
- addProseMirrorPlugins() {
1901
- return [
1902
- new Plugin({
1903
- key: new PluginKey('tabindex'),
1904
- props: {
1905
- attributes: {
1906
- tabindex: '0',
1907
- },
1908
- },
1909
- }),
1910
- ];
1911
- },
1912
- });
1913
-
1914
- var extensions = /*#__PURE__*/Object.freeze({
1915
- __proto__: null,
1916
- ClipboardTextSerializer: ClipboardTextSerializer,
1917
- Commands: Commands,
1918
- Editable: Editable,
1919
- FocusEvents: FocusEvents,
1920
- Keymap: Keymap,
1921
- Tabindex: Tabindex
1922
- });
1923
-
1924
- function getNodeAttributes(state, typeOrName) {
1925
- const type = getNodeType(typeOrName, state.schema);
1926
- const { from, to } = state.selection;
1927
- const nodes = [];
1928
- state.doc.nodesBetween(from, to, node => {
1929
- nodes.push(node);
1930
- });
1931
- const node = nodes
1932
- .reverse()
1933
- .find(nodeItem => nodeItem.type.name === type.name);
1934
- if (!node) {
1935
- return {};
1936
- }
1937
- return { ...node.attrs };
1938
- }
1939
-
1940
- function getAttributes(state, typeOrName) {
1941
- const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
1942
- ? typeOrName
1943
- : typeOrName.name, state.schema);
1944
- if (schemaType === 'node') {
1945
- return getNodeAttributes(state, typeOrName);
1946
- }
1947
- if (schemaType === 'mark') {
1948
- return getMarkAttributes(state, typeOrName);
1949
- }
1950
- return {};
1951
- }
1952
-
1953
- function isActive(state, name, attributes = {}) {
1954
- if (!name) {
1955
- return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
1956
- }
1957
- const schemaType = getSchemaTypeNameByName(name, state.schema);
1958
- if (schemaType === 'node') {
1959
- return isNodeActive(state, name, attributes);
1960
- }
1961
- if (schemaType === 'mark') {
1962
- return isMarkActive(state, name, attributes);
1963
- }
1964
- return false;
1965
- }
1966
-
1967
- function getHTMLFromFragment(fragment, schema) {
1968
- const documentFragment = DOMSerializer
1969
- .fromSchema(schema)
1970
- .serializeFragment(fragment);
1971
- const temporaryDocument = document.implementation.createHTMLDocument();
1972
- const container = temporaryDocument.createElement('div');
1973
- container.appendChild(documentFragment);
1974
- return container.innerHTML;
1975
- }
1976
-
1977
- function getText(node, options) {
1978
- const range = {
1979
- from: 0,
1980
- to: node.content.size,
1981
- };
1982
- return getTextBetween(node, range, options);
1983
- }
1984
-
1985
- function isNodeEmpty(node) {
1986
- var _a;
1987
- const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
1988
- const content = node.toJSON();
1989
- return JSON.stringify(defaultContent) === JSON.stringify(content);
1990
- }
1991
-
1992
- function createStyleTag(style) {
1993
- const tipTapStyleTag = document.querySelector('style[data-tiptap-style]');
1994
- if (tipTapStyleTag !== null) {
1995
- return tipTapStyleTag;
1996
- }
1997
- const styleNode = document.createElement('style');
1998
- styleNode.setAttribute('data-tiptap-style', '');
1999
- styleNode.innerHTML = style;
2000
- document.getElementsByTagName('head')[0].appendChild(styleNode);
2001
- return styleNode;
2002
- }
2003
-
2004
1872
  function createChainableState(config) {
2005
1873
  const { state, transaction } = config;
2006
1874
  let { selection } = transaction;
@@ -2143,6 +2011,199 @@ class CommandManager {
2143
2011
  }
2144
2012
  }
2145
2013
 
2014
+ const Keymap = Extension.create({
2015
+ name: 'keymap',
2016
+ addKeyboardShortcuts() {
2017
+ const handleBackspace = () => this.editor.commands.first(({ commands }) => [
2018
+ () => commands.undoInputRule(),
2019
+ // maybe convert first text block node to default node
2020
+ () => commands.command(({ tr }) => {
2021
+ const { selection, doc } = tr;
2022
+ const { empty, $anchor } = selection;
2023
+ const { pos, parent } = $anchor;
2024
+ const isAtStart = Selection.atStart(doc).from === pos;
2025
+ if (!empty
2026
+ || !isAtStart
2027
+ || !parent.type.isTextblock
2028
+ || parent.textContent.length) {
2029
+ return false;
2030
+ }
2031
+ return commands.clearNodes();
2032
+ }),
2033
+ () => commands.deleteSelection(),
2034
+ () => commands.joinBackward(),
2035
+ () => commands.selectNodeBackward(),
2036
+ ]);
2037
+ const handleDelete = () => this.editor.commands.first(({ commands }) => [
2038
+ () => commands.deleteSelection(),
2039
+ () => commands.joinForward(),
2040
+ () => commands.selectNodeForward(),
2041
+ ]);
2042
+ return {
2043
+ Enter: () => this.editor.commands.first(({ commands }) => [
2044
+ () => commands.newlineInCode(),
2045
+ () => commands.createParagraphNear(),
2046
+ () => commands.liftEmptyBlock(),
2047
+ () => commands.splitBlock(),
2048
+ ]),
2049
+ 'Mod-Enter': () => this.editor.commands.exitCode(),
2050
+ Backspace: handleBackspace,
2051
+ 'Mod-Backspace': handleBackspace,
2052
+ 'Shift-Backspace': handleBackspace,
2053
+ Delete: handleDelete,
2054
+ 'Mod-Delete': handleDelete,
2055
+ 'Mod-a': () => this.editor.commands.selectAll(),
2056
+ };
2057
+ },
2058
+ addProseMirrorPlugins() {
2059
+ return [
2060
+ // With this plugin we check if the whole document was selected and deleted.
2061
+ // In this case we will additionally call `clearNodes()` to convert e.g. a heading
2062
+ // to a paragraph if necessary.
2063
+ // This is an alternative to ProseMirror's `AllSelection`, which doesn’t work well
2064
+ // with many other commands.
2065
+ new Plugin({
2066
+ key: new PluginKey('clearDocument'),
2067
+ appendTransaction: (transactions, oldState, newState) => {
2068
+ const docChanges = transactions.some(transaction => transaction.docChanged)
2069
+ && !oldState.doc.eq(newState.doc);
2070
+ if (!docChanges) {
2071
+ return;
2072
+ }
2073
+ const { empty, from, to } = oldState.selection;
2074
+ const allFrom = Selection.atStart(oldState.doc).from;
2075
+ const allEnd = Selection.atEnd(oldState.doc).to;
2076
+ const allWasSelected = from === allFrom && to === allEnd;
2077
+ const isEmpty = newState.doc.textBetween(0, newState.doc.content.size, ' ', ' ').length === 0;
2078
+ if (empty || !allWasSelected || !isEmpty) {
2079
+ return;
2080
+ }
2081
+ const tr = newState.tr;
2082
+ const state = createChainableState({
2083
+ state: newState,
2084
+ transaction: tr,
2085
+ });
2086
+ const { commands } = new CommandManager({
2087
+ editor: this.editor,
2088
+ state,
2089
+ });
2090
+ commands.clearNodes();
2091
+ if (!tr.steps.length) {
2092
+ return;
2093
+ }
2094
+ return tr;
2095
+ },
2096
+ }),
2097
+ ];
2098
+ },
2099
+ });
2100
+
2101
+ const Tabindex = Extension.create({
2102
+ name: 'tabindex',
2103
+ addProseMirrorPlugins() {
2104
+ return [
2105
+ new Plugin({
2106
+ key: new PluginKey('tabindex'),
2107
+ props: {
2108
+ attributes: {
2109
+ tabindex: '0',
2110
+ },
2111
+ },
2112
+ }),
2113
+ ];
2114
+ },
2115
+ });
2116
+
2117
+ var extensions = /*#__PURE__*/Object.freeze({
2118
+ __proto__: null,
2119
+ ClipboardTextSerializer: ClipboardTextSerializer,
2120
+ Commands: Commands,
2121
+ Editable: Editable,
2122
+ FocusEvents: FocusEvents,
2123
+ Keymap: Keymap,
2124
+ Tabindex: Tabindex
2125
+ });
2126
+
2127
+ function getNodeAttributes(state, typeOrName) {
2128
+ const type = getNodeType(typeOrName, state.schema);
2129
+ const { from, to } = state.selection;
2130
+ const nodes = [];
2131
+ state.doc.nodesBetween(from, to, node => {
2132
+ nodes.push(node);
2133
+ });
2134
+ const node = nodes
2135
+ .reverse()
2136
+ .find(nodeItem => nodeItem.type.name === type.name);
2137
+ if (!node) {
2138
+ return {};
2139
+ }
2140
+ return { ...node.attrs };
2141
+ }
2142
+
2143
+ function getAttributes(state, typeOrName) {
2144
+ const schemaType = getSchemaTypeNameByName(typeof typeOrName === 'string'
2145
+ ? typeOrName
2146
+ : typeOrName.name, state.schema);
2147
+ if (schemaType === 'node') {
2148
+ return getNodeAttributes(state, typeOrName);
2149
+ }
2150
+ if (schemaType === 'mark') {
2151
+ return getMarkAttributes(state, typeOrName);
2152
+ }
2153
+ return {};
2154
+ }
2155
+
2156
+ function isActive(state, name, attributes = {}) {
2157
+ if (!name) {
2158
+ return isNodeActive(state, null, attributes) || isMarkActive(state, null, attributes);
2159
+ }
2160
+ const schemaType = getSchemaTypeNameByName(name, state.schema);
2161
+ if (schemaType === 'node') {
2162
+ return isNodeActive(state, name, attributes);
2163
+ }
2164
+ if (schemaType === 'mark') {
2165
+ return isMarkActive(state, name, attributes);
2166
+ }
2167
+ return false;
2168
+ }
2169
+
2170
+ function getHTMLFromFragment(fragment, schema) {
2171
+ const documentFragment = DOMSerializer
2172
+ .fromSchema(schema)
2173
+ .serializeFragment(fragment);
2174
+ const temporaryDocument = document.implementation.createHTMLDocument();
2175
+ const container = temporaryDocument.createElement('div');
2176
+ container.appendChild(documentFragment);
2177
+ return container.innerHTML;
2178
+ }
2179
+
2180
+ function getText(node, options) {
2181
+ const range = {
2182
+ from: 0,
2183
+ to: node.content.size,
2184
+ };
2185
+ return getTextBetween(node, range, options);
2186
+ }
2187
+
2188
+ function isNodeEmpty(node) {
2189
+ var _a;
2190
+ const defaultContent = (_a = node.type.createAndFill()) === null || _a === void 0 ? void 0 : _a.toJSON();
2191
+ const content = node.toJSON();
2192
+ return JSON.stringify(defaultContent) === JSON.stringify(content);
2193
+ }
2194
+
2195
+ function createStyleTag(style) {
2196
+ const tipTapStyleTag = document.querySelector('style[data-tiptap-style]');
2197
+ if (tipTapStyleTag !== null) {
2198
+ return tipTapStyleTag;
2199
+ }
2200
+ const styleNode = document.createElement('style');
2201
+ styleNode.setAttribute('data-tiptap-style', '');
2202
+ styleNode.innerHTML = style;
2203
+ document.getElementsByTagName('head')[0].appendChild(styleNode);
2204
+ return styleNode;
2205
+ }
2206
+
2146
2207
  class InputRule {
2147
2208
  constructor(config) {
2148
2209
  this.find = config.find;
@@ -4190,12 +4251,17 @@ function getChangedRanges(transform) {
4190
4251
  function getDebugJSON(node, startOffset = 0) {
4191
4252
  const isTopNode = node.type === node.type.schema.topNodeType;
4192
4253
  const increment = isTopNode ? 0 : 1;
4193
- const from = startOffset; // + offset
4254
+ const from = startOffset;
4194
4255
  const to = from + node.nodeSize;
4195
- const marks = node.marks.map(mark => ({
4196
- type: mark.type.name,
4197
- attrs: { ...mark.attrs },
4198
- }));
4256
+ const marks = node.marks.map(mark => {
4257
+ const output = {
4258
+ type: mark.type.name,
4259
+ };
4260
+ if (Object.keys(mark.attrs).length) {
4261
+ output.attrs = { ...mark.attrs };
4262
+ }
4263
+ return output;
4264
+ });
4199
4265
  const attrs = { ...node.attrs };
4200
4266
  const output = {
4201
4267
  type: node.type.name,