@tiptap/extension-code-block 3.3.1 → 3.4.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.
- package/dist/index.cjs +87 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +87 -0
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/code-block.ts +119 -0
package/dist/index.cjs
CHANGED
|
@@ -40,6 +40,8 @@ var CodeBlock = import_core.Node.create({
|
|
|
40
40
|
exitOnTripleEnter: true,
|
|
41
41
|
exitOnArrowDown: true,
|
|
42
42
|
defaultLanguage: null,
|
|
43
|
+
enableTabIndentation: false,
|
|
44
|
+
tabSize: 4,
|
|
43
45
|
HTMLAttributes: {}
|
|
44
46
|
};
|
|
45
47
|
},
|
|
@@ -113,6 +115,91 @@ var CodeBlock = import_core.Node.create({
|
|
|
113
115
|
}
|
|
114
116
|
return false;
|
|
115
117
|
},
|
|
118
|
+
// handle tab indentation
|
|
119
|
+
Tab: ({ editor }) => {
|
|
120
|
+
if (!this.options.enableTabIndentation) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
const { state } = editor;
|
|
124
|
+
const { selection } = state;
|
|
125
|
+
const { $from, empty } = selection;
|
|
126
|
+
if ($from.parent.type !== this.type) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
const indent = " ".repeat(this.options.tabSize);
|
|
130
|
+
if (empty) {
|
|
131
|
+
return editor.commands.insertContent(indent);
|
|
132
|
+
}
|
|
133
|
+
return editor.commands.command(({ tr }) => {
|
|
134
|
+
const { from, to } = selection;
|
|
135
|
+
const text = state.doc.textBetween(from, to, "\n", "\n");
|
|
136
|
+
const lines = text.split("\n");
|
|
137
|
+
const indentedText = lines.map((line) => indent + line).join("\n");
|
|
138
|
+
tr.replaceWith(from, to, state.schema.text(indentedText));
|
|
139
|
+
return true;
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
// handle shift+tab reverse indentation
|
|
143
|
+
"Shift-Tab": ({ editor }) => {
|
|
144
|
+
if (!this.options.enableTabIndentation) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
const { state } = editor;
|
|
148
|
+
const { selection } = state;
|
|
149
|
+
const { $from, empty } = selection;
|
|
150
|
+
if ($from.parent.type !== this.type) {
|
|
151
|
+
return false;
|
|
152
|
+
}
|
|
153
|
+
if (empty) {
|
|
154
|
+
return editor.commands.command(({ tr }) => {
|
|
155
|
+
var _a;
|
|
156
|
+
const { pos } = $from;
|
|
157
|
+
const codeBlockStart = $from.start();
|
|
158
|
+
const codeBlockEnd = $from.end();
|
|
159
|
+
const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, "\n", "\n");
|
|
160
|
+
const lines = allText.split("\n");
|
|
161
|
+
let currentLineIndex = 0;
|
|
162
|
+
let charCount = 0;
|
|
163
|
+
const relativeCursorPos = pos - codeBlockStart;
|
|
164
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
165
|
+
if (charCount + lines[i].length >= relativeCursorPos) {
|
|
166
|
+
currentLineIndex = i;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
charCount += lines[i].length + 1;
|
|
170
|
+
}
|
|
171
|
+
const currentLine = lines[currentLineIndex];
|
|
172
|
+
const leadingSpaces = ((_a = currentLine.match(/^ */)) == null ? void 0 : _a[0]) || "";
|
|
173
|
+
const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize);
|
|
174
|
+
if (spacesToRemove === 0) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
let lineStartPos = codeBlockStart;
|
|
178
|
+
for (let i = 0; i < currentLineIndex; i += 1) {
|
|
179
|
+
lineStartPos += lines[i].length + 1;
|
|
180
|
+
}
|
|
181
|
+
tr.delete(lineStartPos, lineStartPos + spacesToRemove);
|
|
182
|
+
const cursorPosInLine = pos - lineStartPos;
|
|
183
|
+
if (cursorPosInLine <= spacesToRemove) {
|
|
184
|
+
tr.setSelection(import_state.TextSelection.create(tr.doc, lineStartPos));
|
|
185
|
+
}
|
|
186
|
+
return true;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return editor.commands.command(({ tr }) => {
|
|
190
|
+
const { from, to } = selection;
|
|
191
|
+
const text = state.doc.textBetween(from, to, "\n", "\n");
|
|
192
|
+
const lines = text.split("\n");
|
|
193
|
+
const reverseIndentText = lines.map((line) => {
|
|
194
|
+
var _a;
|
|
195
|
+
const leadingSpaces = ((_a = line.match(/^ */)) == null ? void 0 : _a[0]) || "";
|
|
196
|
+
const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize);
|
|
197
|
+
return line.slice(spacesToRemove);
|
|
198
|
+
}).join("\n");
|
|
199
|
+
tr.replaceWith(from, to, state.schema.text(reverseIndentText));
|
|
200
|
+
return true;
|
|
201
|
+
});
|
|
202
|
+
},
|
|
116
203
|
// exit node on triple enter
|
|
117
204
|
Enter: ({ editor }) => {
|
|
118
205
|
if (!this.options.exitOnTripleEnter) {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/code-block.ts"],"sourcesContent":["import { CodeBlock } from './code-block.js'\n\nexport * from './code-block.js'\n\nexport default CodeBlock\n","import { mergeAttributes, Node, textblockTypeInputRule } from '@tiptap/core'\nimport { Plugin, PluginKey, Selection, TextSelection } from '@tiptap/pm/state'\n\nexport interface CodeBlockOptions {\n /**\n * Adds a prefix to language classes that are applied to code tags.\n * @default 'language-'\n */\n languageClassPrefix: string\n /**\n * Define whether the node should be exited on triple enter.\n * @default true\n */\n exitOnTripleEnter: boolean\n /**\n * Define whether the node should be exited on arrow down if there is no node after it.\n * @default true\n */\n exitOnArrowDown: boolean\n /**\n * The default language.\n * @default null\n * @example 'js'\n */\n defaultLanguage: string | null | undefined\n /**\n * Custom HTML attributes that should be added to the rendered HTML tag.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n codeBlock: {\n /**\n * Set a code block\n * @param attributes Code block attributes\n * @example editor.commands.setCodeBlock({ language: 'javascript' })\n */\n setCodeBlock: (attributes?: { language: string }) => ReturnType\n /**\n * Toggle a code block\n * @param attributes Code block attributes\n * @example editor.commands.toggleCodeBlock({ language: 'javascript' })\n */\n toggleCodeBlock: (attributes?: { language: string }) => ReturnType\n }\n }\n}\n\n/**\n * Matches a code block with backticks.\n */\nexport const backtickInputRegex = /^```([a-z]+)?[\\s\\n]$/\n\n/**\n * Matches a code block with tildes.\n */\nexport const tildeInputRegex = /^~~~([a-z]+)?[\\s\\n]$/\n\n/**\n * This extension allows you to create code blocks.\n * @see https://tiptap.dev/api/nodes/code-block\n */\nexport const CodeBlock = Node.create<CodeBlockOptions>({\n name: 'codeBlock',\n\n addOptions() {\n return {\n languageClassPrefix: 'language-',\n exitOnTripleEnter: true,\n exitOnArrowDown: true,\n defaultLanguage: null,\n HTMLAttributes: {},\n }\n },\n\n content: 'text*',\n\n marks: '',\n\n group: 'block',\n\n code: true,\n\n defining: true,\n\n addAttributes() {\n return {\n language: {\n default: this.options.defaultLanguage,\n parseHTML: element => {\n const { languageClassPrefix } = this.options\n const classNames = [...(element.firstElementChild?.classList || [])]\n const languages = classNames\n .filter(className => className.startsWith(languageClassPrefix))\n .map(className => className.replace(languageClassPrefix, ''))\n const language = languages[0]\n\n if (!language) {\n return null\n }\n\n return language\n },\n rendered: false,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'pre',\n preserveWhitespace: 'full',\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'pre',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n [\n 'code',\n {\n class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null,\n },\n 0,\n ],\n ]\n },\n\n addCommands() {\n return {\n setCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.setNode(this.name, attributes)\n },\n toggleCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.toggleNode(this.name, 'paragraph', attributes)\n },\n }\n },\n\n addKeyboardShortcuts() {\n return {\n 'Mod-Alt-c': () => this.editor.commands.toggleCodeBlock(),\n\n // remove code block when at start of document or code block is empty\n Backspace: () => {\n const { empty, $anchor } = this.editor.state.selection\n const isAtStart = $anchor.pos === 1\n\n if (!empty || $anchor.parent.type.name !== this.name) {\n return false\n }\n\n if (isAtStart || !$anchor.parent.textContent.length) {\n return this.editor.commands.clearNodes()\n }\n\n return false\n },\n\n // exit node on triple enter\n Enter: ({ editor }) => {\n if (!this.options.exitOnTripleEnter) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n const endsWithDoubleNewline = $from.parent.textContent.endsWith('\\n\\n')\n\n if (!isAtEnd || !endsWithDoubleNewline) {\n return false\n }\n\n return editor\n .chain()\n .command(({ tr }) => {\n tr.delete($from.pos - 2, $from.pos)\n\n return true\n })\n .exitCode()\n .run()\n },\n\n // exit node on arrow down\n ArrowDown: ({ editor }) => {\n if (!this.options.exitOnArrowDown) {\n return false\n }\n\n const { state } = editor\n const { selection, doc } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n\n if (!isAtEnd) {\n return false\n }\n\n const after = $from.after()\n\n if (after === undefined) {\n return false\n }\n\n const nodeAfter = doc.nodeAt(after)\n\n if (nodeAfter) {\n return editor.commands.command(({ tr }) => {\n tr.setSelection(Selection.near(doc.resolve(after)))\n return true\n })\n }\n\n return editor.commands.exitCode()\n },\n }\n },\n\n addInputRules() {\n return [\n textblockTypeInputRule({\n find: backtickInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n textblockTypeInputRule({\n find: tildeInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n return [\n // this plugin creates a code block for pasted content from VS Code\n // we can also detect the copied code language\n new Plugin({\n key: new PluginKey('codeBlockVSCodeHandler'),\n props: {\n handlePaste: (view, event) => {\n if (!event.clipboardData) {\n return false\n }\n\n // don’t create a new code block within code blocks\n if (this.editor.isActive(this.type.name)) {\n return false\n }\n\n const text = event.clipboardData.getData('text/plain')\n const vscode = event.clipboardData.getData('vscode-editor-data')\n const vscodeData = vscode ? JSON.parse(vscode) : undefined\n const language = vscodeData?.mode\n\n if (!text || !language) {\n return false\n }\n\n const { tr, schema } = view.state\n\n // prepare a text node\n // strip carriage return chars from text pasted as code\n // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd\n const textNode = schema.text(text.replace(/\\r\\n?/g, '\\n'))\n\n // create a code block with the text node\n // replace selection with the code block\n tr.replaceSelectionWith(this.type.create({ language }, textNode))\n\n if (tr.selection.$from.parent.type !== this.type) {\n // put cursor inside the newly created code block\n tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))))\n }\n\n // store meta information\n // this is useful for other plugins that depends on the paste event\n // like the paste rule plugin\n tr.setMeta('paste', true)\n\n view.dispatch(tr)\n\n return true\n },\n },\n }),\n ]\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA8D;AAC9D,mBAA4D;AAsDrD,IAAM,qBAAqB;AAK3B,IAAM,kBAAkB;AAMxB,IAAM,YAAY,iBAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,qBAAqB;AAAA,MACrB,mBAAmB;AAAA,MACnB,iBAAiB;AAAA,MACjB,iBAAiB;AAAA,MACjB,gBAAgB,CAAC;AAA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AAM,cAAc,QAAQ,YAAY;AACrD,kBAAM,SAAS,MAAM,cAAc,QAAQ,oBAAoB;AAC/D,kBAAM,aAAa,SAAS,KAAK,MAAM,MAAM,IAAI;AACjD,kBAAM,WAAW,yCAAY;AAE7B,gBAAI,CAAC,QAAQ,CAAC,UAAU;AACtB,qBAAO;AAAA,YACT;AAEA,kBAAM,EAAE,IAAI,OAAO,IAAI,KAAK;AAK5B,kBAAM,WAAW,OAAO,KAAK,KAAK,QAAQ,UAAU,IAAI,CAAC;AAIzD,eAAG,qBAAqB,KAAK,KAAK,OAAO,EAAE,SAAS,GAAG,QAAQ,CAAC;AAEhE,gBAAI,GAAG,UAAU,MAAM,OAAO,SAAS,KAAK,MAAM;AAEhD,iBAAG,aAAa,2BAAc,KAAK,GAAG,IAAI,QAAQ,KAAK,IAAI,GAAG,GAAG,UAAU,OAAO,CAAC,CAAC,CAAC,CAAC;AAAA,YACxF;AAKA,eAAG,QAAQ,SAAS,IAAI;AAExB,iBAAK,SAAS,EAAE;AAEhB,mBAAO;AAAA,UACT;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ADxTD,IAAO,gBAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/code-block.ts"],"sourcesContent":["import { CodeBlock } from './code-block.js'\n\nexport * from './code-block.js'\n\nexport default CodeBlock\n","import { mergeAttributes, Node, textblockTypeInputRule } from '@tiptap/core'\nimport { Plugin, PluginKey, Selection, TextSelection } from '@tiptap/pm/state'\n\nexport interface CodeBlockOptions {\n /**\n * Adds a prefix to language classes that are applied to code tags.\n * @default 'language-'\n */\n languageClassPrefix: string\n /**\n * Define whether the node should be exited on triple enter.\n * @default true\n */\n exitOnTripleEnter: boolean\n /**\n * Define whether the node should be exited on arrow down if there is no node after it.\n * @default true\n */\n exitOnArrowDown: boolean\n /**\n * The default language.\n * @default null\n * @example 'js'\n */\n defaultLanguage: string | null | undefined\n /**\n * Enable tab key for indentation in code blocks.\n * @default false\n */\n enableTabIndentation: boolean\n /**\n * The number of spaces to use for tab indentation.\n * @default 4\n */\n tabSize: number\n /**\n * Custom HTML attributes that should be added to the rendered HTML tag.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n codeBlock: {\n /**\n * Set a code block\n * @param attributes Code block attributes\n * @example editor.commands.setCodeBlock({ language: 'javascript' })\n */\n setCodeBlock: (attributes?: { language: string }) => ReturnType\n /**\n * Toggle a code block\n * @param attributes Code block attributes\n * @example editor.commands.toggleCodeBlock({ language: 'javascript' })\n */\n toggleCodeBlock: (attributes?: { language: string }) => ReturnType\n }\n }\n}\n\n/**\n * Matches a code block with backticks.\n */\nexport const backtickInputRegex = /^```([a-z]+)?[\\s\\n]$/\n\n/**\n * Matches a code block with tildes.\n */\nexport const tildeInputRegex = /^~~~([a-z]+)?[\\s\\n]$/\n\n/**\n * This extension allows you to create code blocks.\n * @see https://tiptap.dev/api/nodes/code-block\n */\nexport const CodeBlock = Node.create<CodeBlockOptions>({\n name: 'codeBlock',\n\n addOptions() {\n return {\n languageClassPrefix: 'language-',\n exitOnTripleEnter: true,\n exitOnArrowDown: true,\n defaultLanguage: null,\n enableTabIndentation: false,\n tabSize: 4,\n HTMLAttributes: {},\n }\n },\n\n content: 'text*',\n\n marks: '',\n\n group: 'block',\n\n code: true,\n\n defining: true,\n\n addAttributes() {\n return {\n language: {\n default: this.options.defaultLanguage,\n parseHTML: element => {\n const { languageClassPrefix } = this.options\n const classNames = [...(element.firstElementChild?.classList || [])]\n const languages = classNames\n .filter(className => className.startsWith(languageClassPrefix))\n .map(className => className.replace(languageClassPrefix, ''))\n const language = languages[0]\n\n if (!language) {\n return null\n }\n\n return language\n },\n rendered: false,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'pre',\n preserveWhitespace: 'full',\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'pre',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n [\n 'code',\n {\n class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null,\n },\n 0,\n ],\n ]\n },\n\n addCommands() {\n return {\n setCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.setNode(this.name, attributes)\n },\n toggleCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.toggleNode(this.name, 'paragraph', attributes)\n },\n }\n },\n\n addKeyboardShortcuts() {\n return {\n 'Mod-Alt-c': () => this.editor.commands.toggleCodeBlock(),\n\n // remove code block when at start of document or code block is empty\n Backspace: () => {\n const { empty, $anchor } = this.editor.state.selection\n const isAtStart = $anchor.pos === 1\n\n if (!empty || $anchor.parent.type.name !== this.name) {\n return false\n }\n\n if (isAtStart || !$anchor.parent.textContent.length) {\n return this.editor.commands.clearNodes()\n }\n\n return false\n },\n\n // handle tab indentation\n Tab: ({ editor }) => {\n if (!this.options.enableTabIndentation) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if ($from.parent.type !== this.type) {\n return false\n }\n\n const indent = ' '.repeat(this.options.tabSize)\n\n if (empty) {\n return editor.commands.insertContent(indent)\n }\n\n return editor.commands.command(({ tr }) => {\n const { from, to } = selection\n const text = state.doc.textBetween(from, to, '\\n', '\\n')\n const lines = text.split('\\n')\n const indentedText = lines.map(line => indent + line).join('\\n')\n\n tr.replaceWith(from, to, state.schema.text(indentedText))\n return true\n })\n },\n\n // handle shift+tab reverse indentation\n 'Shift-Tab': ({ editor }) => {\n if (!this.options.enableTabIndentation) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if ($from.parent.type !== this.type) {\n return false\n }\n\n if (empty) {\n return editor.commands.command(({ tr }) => {\n const { pos } = $from\n const codeBlockStart = $from.start()\n const codeBlockEnd = $from.end()\n\n const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, '\\n', '\\n')\n const lines = allText.split('\\n')\n\n let currentLineIndex = 0\n let charCount = 0\n const relativeCursorPos = pos - codeBlockStart\n\n for (let i = 0; i < lines.length; i += 1) {\n if (charCount + lines[i].length >= relativeCursorPos) {\n currentLineIndex = i\n break\n }\n charCount += lines[i].length + 1\n }\n\n const currentLine = lines[currentLineIndex]\n const leadingSpaces = currentLine.match(/^ */)?.[0] || ''\n const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize)\n\n if (spacesToRemove === 0) {\n return true\n }\n\n let lineStartPos = codeBlockStart\n for (let i = 0; i < currentLineIndex; i += 1) {\n lineStartPos += lines[i].length + 1\n }\n\n tr.delete(lineStartPos, lineStartPos + spacesToRemove)\n\n const cursorPosInLine = pos - lineStartPos\n if (cursorPosInLine <= spacesToRemove) {\n tr.setSelection(TextSelection.create(tr.doc, lineStartPos))\n }\n\n return true\n })\n }\n\n return editor.commands.command(({ tr }) => {\n const { from, to } = selection\n const text = state.doc.textBetween(from, to, '\\n', '\\n')\n const lines = text.split('\\n')\n const reverseIndentText = lines\n .map(line => {\n const leadingSpaces = line.match(/^ */)?.[0] || ''\n const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize)\n return line.slice(spacesToRemove)\n })\n .join('\\n')\n\n tr.replaceWith(from, to, state.schema.text(reverseIndentText))\n return true\n })\n },\n\n // exit node on triple enter\n Enter: ({ editor }) => {\n if (!this.options.exitOnTripleEnter) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n const endsWithDoubleNewline = $from.parent.textContent.endsWith('\\n\\n')\n\n if (!isAtEnd || !endsWithDoubleNewline) {\n return false\n }\n\n return editor\n .chain()\n .command(({ tr }) => {\n tr.delete($from.pos - 2, $from.pos)\n\n return true\n })\n .exitCode()\n .run()\n },\n\n // exit node on arrow down\n ArrowDown: ({ editor }) => {\n if (!this.options.exitOnArrowDown) {\n return false\n }\n\n const { state } = editor\n const { selection, doc } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n\n if (!isAtEnd) {\n return false\n }\n\n const after = $from.after()\n\n if (after === undefined) {\n return false\n }\n\n const nodeAfter = doc.nodeAt(after)\n\n if (nodeAfter) {\n return editor.commands.command(({ tr }) => {\n tr.setSelection(Selection.near(doc.resolve(after)))\n return true\n })\n }\n\n return editor.commands.exitCode()\n },\n }\n },\n\n addInputRules() {\n return [\n textblockTypeInputRule({\n find: backtickInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n textblockTypeInputRule({\n find: tildeInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n return [\n // this plugin creates a code block for pasted content from VS Code\n // we can also detect the copied code language\n new Plugin({\n key: new PluginKey('codeBlockVSCodeHandler'),\n props: {\n handlePaste: (view, event) => {\n if (!event.clipboardData) {\n return false\n }\n\n // don’t create a new code block within code blocks\n if (this.editor.isActive(this.type.name)) {\n return false\n }\n\n const text = event.clipboardData.getData('text/plain')\n const vscode = event.clipboardData.getData('vscode-editor-data')\n const vscodeData = vscode ? JSON.parse(vscode) : undefined\n const language = vscodeData?.mode\n\n if (!text || !language) {\n return false\n }\n\n const { tr, schema } = view.state\n\n // prepare a text node\n // strip carriage return chars from text pasted as code\n // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd\n const textNode = schema.text(text.replace(/\\r\\n?/g, '\\n'))\n\n // create a code block with the text node\n // replace selection with the code block\n tr.replaceSelectionWith(this.type.create({ language }, textNode))\n\n if (tr.selection.$from.parent.type !== this.type) {\n // put cursor inside the newly created code block\n tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))))\n }\n\n // store meta information\n // this is useful for other plugins that depends on the paste event\n // like the paste rule plugin\n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AAG,QAAQ,SAAS,IAAI;AAExB,iBAAK,SAAS,EAAE;AAEhB,mBAAO;AAAA,UACT;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;AD/aD,IAAO,gBAAQ;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -22,6 +22,16 @@ interface CodeBlockOptions {
|
|
|
22
22
|
* @example 'js'
|
|
23
23
|
*/
|
|
24
24
|
defaultLanguage: string | null | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Enable tab key for indentation in code blocks.
|
|
27
|
+
* @default false
|
|
28
|
+
*/
|
|
29
|
+
enableTabIndentation: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* The number of spaces to use for tab indentation.
|
|
32
|
+
* @default 4
|
|
33
|
+
*/
|
|
34
|
+
tabSize: number;
|
|
25
35
|
/**
|
|
26
36
|
* Custom HTML attributes that should be added to the rendered HTML tag.
|
|
27
37
|
* @default {}
|
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,16 @@ interface CodeBlockOptions {
|
|
|
22
22
|
* @example 'js'
|
|
23
23
|
*/
|
|
24
24
|
defaultLanguage: string | null | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Enable tab key for indentation in code blocks.
|
|
27
|
+
* @default false
|
|
28
|
+
*/
|
|
29
|
+
enableTabIndentation: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* The number of spaces to use for tab indentation.
|
|
32
|
+
* @default 4
|
|
33
|
+
*/
|
|
34
|
+
tabSize: number;
|
|
25
35
|
/**
|
|
26
36
|
* Custom HTML attributes that should be added to the rendered HTML tag.
|
|
27
37
|
* @default {}
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,8 @@ var CodeBlock = Node.create({
|
|
|
11
11
|
exitOnTripleEnter: true,
|
|
12
12
|
exitOnArrowDown: true,
|
|
13
13
|
defaultLanguage: null,
|
|
14
|
+
enableTabIndentation: false,
|
|
15
|
+
tabSize: 4,
|
|
14
16
|
HTMLAttributes: {}
|
|
15
17
|
};
|
|
16
18
|
},
|
|
@@ -84,6 +86,91 @@ var CodeBlock = Node.create({
|
|
|
84
86
|
}
|
|
85
87
|
return false;
|
|
86
88
|
},
|
|
89
|
+
// handle tab indentation
|
|
90
|
+
Tab: ({ editor }) => {
|
|
91
|
+
if (!this.options.enableTabIndentation) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const { state } = editor;
|
|
95
|
+
const { selection } = state;
|
|
96
|
+
const { $from, empty } = selection;
|
|
97
|
+
if ($from.parent.type !== this.type) {
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
const indent = " ".repeat(this.options.tabSize);
|
|
101
|
+
if (empty) {
|
|
102
|
+
return editor.commands.insertContent(indent);
|
|
103
|
+
}
|
|
104
|
+
return editor.commands.command(({ tr }) => {
|
|
105
|
+
const { from, to } = selection;
|
|
106
|
+
const text = state.doc.textBetween(from, to, "\n", "\n");
|
|
107
|
+
const lines = text.split("\n");
|
|
108
|
+
const indentedText = lines.map((line) => indent + line).join("\n");
|
|
109
|
+
tr.replaceWith(from, to, state.schema.text(indentedText));
|
|
110
|
+
return true;
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
// handle shift+tab reverse indentation
|
|
114
|
+
"Shift-Tab": ({ editor }) => {
|
|
115
|
+
if (!this.options.enableTabIndentation) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
const { state } = editor;
|
|
119
|
+
const { selection } = state;
|
|
120
|
+
const { $from, empty } = selection;
|
|
121
|
+
if ($from.parent.type !== this.type) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (empty) {
|
|
125
|
+
return editor.commands.command(({ tr }) => {
|
|
126
|
+
var _a;
|
|
127
|
+
const { pos } = $from;
|
|
128
|
+
const codeBlockStart = $from.start();
|
|
129
|
+
const codeBlockEnd = $from.end();
|
|
130
|
+
const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, "\n", "\n");
|
|
131
|
+
const lines = allText.split("\n");
|
|
132
|
+
let currentLineIndex = 0;
|
|
133
|
+
let charCount = 0;
|
|
134
|
+
const relativeCursorPos = pos - codeBlockStart;
|
|
135
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
136
|
+
if (charCount + lines[i].length >= relativeCursorPos) {
|
|
137
|
+
currentLineIndex = i;
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
charCount += lines[i].length + 1;
|
|
141
|
+
}
|
|
142
|
+
const currentLine = lines[currentLineIndex];
|
|
143
|
+
const leadingSpaces = ((_a = currentLine.match(/^ */)) == null ? void 0 : _a[0]) || "";
|
|
144
|
+
const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize);
|
|
145
|
+
if (spacesToRemove === 0) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
let lineStartPos = codeBlockStart;
|
|
149
|
+
for (let i = 0; i < currentLineIndex; i += 1) {
|
|
150
|
+
lineStartPos += lines[i].length + 1;
|
|
151
|
+
}
|
|
152
|
+
tr.delete(lineStartPos, lineStartPos + spacesToRemove);
|
|
153
|
+
const cursorPosInLine = pos - lineStartPos;
|
|
154
|
+
if (cursorPosInLine <= spacesToRemove) {
|
|
155
|
+
tr.setSelection(TextSelection.create(tr.doc, lineStartPos));
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return editor.commands.command(({ tr }) => {
|
|
161
|
+
const { from, to } = selection;
|
|
162
|
+
const text = state.doc.textBetween(from, to, "\n", "\n");
|
|
163
|
+
const lines = text.split("\n");
|
|
164
|
+
const reverseIndentText = lines.map((line) => {
|
|
165
|
+
var _a;
|
|
166
|
+
const leadingSpaces = ((_a = line.match(/^ */)) == null ? void 0 : _a[0]) || "";
|
|
167
|
+
const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize);
|
|
168
|
+
return line.slice(spacesToRemove);
|
|
169
|
+
}).join("\n");
|
|
170
|
+
tr.replaceWith(from, to, state.schema.text(reverseIndentText));
|
|
171
|
+
return true;
|
|
172
|
+
});
|
|
173
|
+
},
|
|
87
174
|
// exit node on triple enter
|
|
88
175
|
Enter: ({ editor }) => {
|
|
89
176
|
if (!this.options.exitOnTripleEnter) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/code-block.ts","../src/index.ts"],"sourcesContent":["import { mergeAttributes, Node, textblockTypeInputRule } from '@tiptap/core'\nimport { Plugin, PluginKey, Selection, TextSelection } from '@tiptap/pm/state'\n\nexport interface CodeBlockOptions {\n /**\n * Adds a prefix to language classes that are applied to code tags.\n * @default 'language-'\n */\n languageClassPrefix: string\n /**\n * Define whether the node should be exited on triple enter.\n * @default true\n */\n exitOnTripleEnter: boolean\n /**\n * Define whether the node should be exited on arrow down if there is no node after it.\n * @default true\n */\n exitOnArrowDown: boolean\n /**\n * The default language.\n * @default null\n * @example 'js'\n */\n defaultLanguage: string | null | undefined\n /**\n * Custom HTML attributes that should be added to the rendered HTML tag.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n codeBlock: {\n /**\n * Set a code block\n * @param attributes Code block attributes\n * @example editor.commands.setCodeBlock({ language: 'javascript' })\n */\n setCodeBlock: (attributes?: { language: string }) => ReturnType\n /**\n * Toggle a code block\n * @param attributes Code block attributes\n * @example editor.commands.toggleCodeBlock({ language: 'javascript' })\n */\n toggleCodeBlock: (attributes?: { language: string }) => ReturnType\n }\n }\n}\n\n/**\n * Matches a code block with backticks.\n */\nexport const backtickInputRegex = /^```([a-z]+)?[\\s\\n]$/\n\n/**\n * Matches a code block with tildes.\n */\nexport const tildeInputRegex = /^~~~([a-z]+)?[\\s\\n]$/\n\n/**\n * This extension allows you to create code blocks.\n * @see https://tiptap.dev/api/nodes/code-block\n */\nexport const CodeBlock = Node.create<CodeBlockOptions>({\n name: 'codeBlock',\n\n addOptions() {\n return {\n languageClassPrefix: 'language-',\n exitOnTripleEnter: true,\n exitOnArrowDown: true,\n defaultLanguage: null,\n HTMLAttributes: {},\n }\n },\n\n content: 'text*',\n\n marks: '',\n\n group: 'block',\n\n code: true,\n\n defining: true,\n\n addAttributes() {\n return {\n language: {\n default: this.options.defaultLanguage,\n parseHTML: element => {\n const { languageClassPrefix } = this.options\n const classNames = [...(element.firstElementChild?.classList || [])]\n const languages = classNames\n .filter(className => className.startsWith(languageClassPrefix))\n .map(className => className.replace(languageClassPrefix, ''))\n const language = languages[0]\n\n if (!language) {\n return null\n }\n\n return language\n },\n rendered: false,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'pre',\n preserveWhitespace: 'full',\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'pre',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n [\n 'code',\n {\n class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null,\n },\n 0,\n ],\n ]\n },\n\n addCommands() {\n return {\n setCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.setNode(this.name, attributes)\n },\n toggleCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.toggleNode(this.name, 'paragraph', attributes)\n },\n }\n },\n\n addKeyboardShortcuts() {\n return {\n 'Mod-Alt-c': () => this.editor.commands.toggleCodeBlock(),\n\n // remove code block when at start of document or code block is empty\n Backspace: () => {\n const { empty, $anchor } = this.editor.state.selection\n const isAtStart = $anchor.pos === 1\n\n if (!empty || $anchor.parent.type.name !== this.name) {\n return false\n }\n\n if (isAtStart || !$anchor.parent.textContent.length) {\n return this.editor.commands.clearNodes()\n }\n\n return false\n },\n\n // exit node on triple enter\n Enter: ({ editor }) => {\n if (!this.options.exitOnTripleEnter) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n const endsWithDoubleNewline = $from.parent.textContent.endsWith('\\n\\n')\n\n if (!isAtEnd || !endsWithDoubleNewline) {\n return false\n }\n\n return editor\n .chain()\n .command(({ tr }) => {\n tr.delete($from.pos - 2, $from.pos)\n\n return true\n })\n .exitCode()\n .run()\n },\n\n // exit node on arrow down\n ArrowDown: ({ editor }) => {\n if (!this.options.exitOnArrowDown) {\n return false\n }\n\n const { state } = editor\n const { selection, doc } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n\n if (!isAtEnd) {\n return false\n }\n\n const after = $from.after()\n\n if (after === undefined) {\n return false\n }\n\n const nodeAfter = doc.nodeAt(after)\n\n if (nodeAfter) {\n return editor.commands.command(({ tr }) => {\n tr.setSelection(Selection.near(doc.resolve(after)))\n return true\n })\n }\n\n return editor.commands.exitCode()\n },\n }\n },\n\n addInputRules() {\n return [\n textblockTypeInputRule({\n find: backtickInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n textblockTypeInputRule({\n find: tildeInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n return [\n // this plugin creates a code block for pasted content from VS Code\n // we can also detect the copied code language\n new Plugin({\n key: new PluginKey('codeBlockVSCodeHandler'),\n props: {\n handlePaste: (view, event) => {\n if (!event.clipboardData) {\n return false\n }\n\n // don’t create a new code block within code blocks\n if (this.editor.isActive(this.type.name)) {\n return false\n }\n\n const text = event.clipboardData.getData('text/plain')\n const vscode = event.clipboardData.getData('vscode-editor-data')\n const vscodeData = vscode ? JSON.parse(vscode) : undefined\n const language = vscodeData?.mode\n\n if (!text || !language) {\n return false\n }\n\n const { tr, schema } = view.state\n\n // prepare a text node\n // strip carriage return chars from text pasted as code\n // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd\n const textNode = schema.text(text.replace(/\\r\\n?/g, '\\n'))\n\n // create a code block with the text node\n // replace selection with the code block\n tr.replaceSelectionWith(this.type.create({ language }, textNode))\n\n if (tr.selection.$from.parent.type !== this.type) {\n // put cursor inside the newly created code block\n tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))))\n }\n\n // store meta information\n // this is useful for other plugins that depends on the paste event\n // like the paste rule plugin\n tr.setMeta('paste', true)\n\n view.dispatch(tr)\n\n return true\n },\n },\n }),\n ]\n },\n})\n","import { CodeBlock } from './code-block.js'\n\nexport * from './code-block.js'\n\nexport default CodeBlock\n"],"mappings":";AAAA,SAAS,iBAAiB,MAAM,8BAA8B;AAC9D,SAAS,QAAQ,WAAW,WAAW,qBAAqB;AAsDrD,IAAM,qBAAqB;AAK3B,IAAM,kBAAkB;AAMxB,IAAM,YAAY,KAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,qBAAqB;AAAA,MACrB,mBAAmB;AAAA,MACnB,iBAAiB;AAAA,MACjB,iBAAiB;AAAA,MACjB,gBAAgB,CAAC;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,SAAS;AAAA,EAET,OAAO;AAAA,EAEP,OAAO;AAAA,EAEP,MAAM;AAAA,EAEN,UAAU;AAAA,EAEV,gBAAgB;AACd,WAAO;AAAA,MACL,UAAU;AAAA,QACR,SAAS,KAAK,QAAQ;AAAA,QACtB,WAAW,aAAW;AA7F9B;AA8FU,gBAAM,EAAE,oBAAoB,IAAI,KAAK;AACrC,gBAAM,aAAa,CAAC,KAAI,aAAQ,sBAAR,mBAA2B,cAAa,CAAC,CAAE;AACnE,gBAAM,YAAY,WACf,OAAO,eAAa,UAAU,WAAW,mBAAmB,CAAC,EAC7D,IAAI,eAAa,UAAU,QAAQ,qBAAqB,EAAE,CAAC;AAC9D,gBAAM,WAAW,UAAU,CAAC;AAE5B,cAAI,CAAC,UAAU;AACb,mBAAO;AAAA,UACT;A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,CAAC,CAAC,CAAC,CAAC;AAAA,YACxF;AAKA,eAAG,QAAQ,SAAS,IAAI;AAExB,iBAAK,SAAS,EAAE;AAEhB,mBAAO;AAAA,UACT;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;ACxTD,IAAO,gBAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/code-block.ts","../src/index.ts"],"sourcesContent":["import { mergeAttributes, Node, textblockTypeInputRule } from '@tiptap/core'\nimport { Plugin, PluginKey, Selection, TextSelection } from '@tiptap/pm/state'\n\nexport interface CodeBlockOptions {\n /**\n * Adds a prefix to language classes that are applied to code tags.\n * @default 'language-'\n */\n languageClassPrefix: string\n /**\n * Define whether the node should be exited on triple enter.\n * @default true\n */\n exitOnTripleEnter: boolean\n /**\n * Define whether the node should be exited on arrow down if there is no node after it.\n * @default true\n */\n exitOnArrowDown: boolean\n /**\n * The default language.\n * @default null\n * @example 'js'\n */\n defaultLanguage: string | null | undefined\n /**\n * Enable tab key for indentation in code blocks.\n * @default false\n */\n enableTabIndentation: boolean\n /**\n * The number of spaces to use for tab indentation.\n * @default 4\n */\n tabSize: number\n /**\n * Custom HTML attributes that should be added to the rendered HTML tag.\n * @default {}\n * @example { class: 'foo' }\n */\n HTMLAttributes: Record<string, any>\n}\n\ndeclare module '@tiptap/core' {\n interface Commands<ReturnType> {\n codeBlock: {\n /**\n * Set a code block\n * @param attributes Code block attributes\n * @example editor.commands.setCodeBlock({ language: 'javascript' })\n */\n setCodeBlock: (attributes?: { language: string }) => ReturnType\n /**\n * Toggle a code block\n * @param attributes Code block attributes\n * @example editor.commands.toggleCodeBlock({ language: 'javascript' })\n */\n toggleCodeBlock: (attributes?: { language: string }) => ReturnType\n }\n }\n}\n\n/**\n * Matches a code block with backticks.\n */\nexport const backtickInputRegex = /^```([a-z]+)?[\\s\\n]$/\n\n/**\n * Matches a code block with tildes.\n */\nexport const tildeInputRegex = /^~~~([a-z]+)?[\\s\\n]$/\n\n/**\n * This extension allows you to create code blocks.\n * @see https://tiptap.dev/api/nodes/code-block\n */\nexport const CodeBlock = Node.create<CodeBlockOptions>({\n name: 'codeBlock',\n\n addOptions() {\n return {\n languageClassPrefix: 'language-',\n exitOnTripleEnter: true,\n exitOnArrowDown: true,\n defaultLanguage: null,\n enableTabIndentation: false,\n tabSize: 4,\n HTMLAttributes: {},\n }\n },\n\n content: 'text*',\n\n marks: '',\n\n group: 'block',\n\n code: true,\n\n defining: true,\n\n addAttributes() {\n return {\n language: {\n default: this.options.defaultLanguage,\n parseHTML: element => {\n const { languageClassPrefix } = this.options\n const classNames = [...(element.firstElementChild?.classList || [])]\n const languages = classNames\n .filter(className => className.startsWith(languageClassPrefix))\n .map(className => className.replace(languageClassPrefix, ''))\n const language = languages[0]\n\n if (!language) {\n return null\n }\n\n return language\n },\n rendered: false,\n },\n }\n },\n\n parseHTML() {\n return [\n {\n tag: 'pre',\n preserveWhitespace: 'full',\n },\n ]\n },\n\n renderHTML({ node, HTMLAttributes }) {\n return [\n 'pre',\n mergeAttributes(this.options.HTMLAttributes, HTMLAttributes),\n [\n 'code',\n {\n class: node.attrs.language ? this.options.languageClassPrefix + node.attrs.language : null,\n },\n 0,\n ],\n ]\n },\n\n addCommands() {\n return {\n setCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.setNode(this.name, attributes)\n },\n toggleCodeBlock:\n attributes =>\n ({ commands }) => {\n return commands.toggleNode(this.name, 'paragraph', attributes)\n },\n }\n },\n\n addKeyboardShortcuts() {\n return {\n 'Mod-Alt-c': () => this.editor.commands.toggleCodeBlock(),\n\n // remove code block when at start of document or code block is empty\n Backspace: () => {\n const { empty, $anchor } = this.editor.state.selection\n const isAtStart = $anchor.pos === 1\n\n if (!empty || $anchor.parent.type.name !== this.name) {\n return false\n }\n\n if (isAtStart || !$anchor.parent.textContent.length) {\n return this.editor.commands.clearNodes()\n }\n\n return false\n },\n\n // handle tab indentation\n Tab: ({ editor }) => {\n if (!this.options.enableTabIndentation) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if ($from.parent.type !== this.type) {\n return false\n }\n\n const indent = ' '.repeat(this.options.tabSize)\n\n if (empty) {\n return editor.commands.insertContent(indent)\n }\n\n return editor.commands.command(({ tr }) => {\n const { from, to } = selection\n const text = state.doc.textBetween(from, to, '\\n', '\\n')\n const lines = text.split('\\n')\n const indentedText = lines.map(line => indent + line).join('\\n')\n\n tr.replaceWith(from, to, state.schema.text(indentedText))\n return true\n })\n },\n\n // handle shift+tab reverse indentation\n 'Shift-Tab': ({ editor }) => {\n if (!this.options.enableTabIndentation) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if ($from.parent.type !== this.type) {\n return false\n }\n\n if (empty) {\n return editor.commands.command(({ tr }) => {\n const { pos } = $from\n const codeBlockStart = $from.start()\n const codeBlockEnd = $from.end()\n\n const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, '\\n', '\\n')\n const lines = allText.split('\\n')\n\n let currentLineIndex = 0\n let charCount = 0\n const relativeCursorPos = pos - codeBlockStart\n\n for (let i = 0; i < lines.length; i += 1) {\n if (charCount + lines[i].length >= relativeCursorPos) {\n currentLineIndex = i\n break\n }\n charCount += lines[i].length + 1\n }\n\n const currentLine = lines[currentLineIndex]\n const leadingSpaces = currentLine.match(/^ */)?.[0] || ''\n const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize)\n\n if (spacesToRemove === 0) {\n return true\n }\n\n let lineStartPos = codeBlockStart\n for (let i = 0; i < currentLineIndex; i += 1) {\n lineStartPos += lines[i].length + 1\n }\n\n tr.delete(lineStartPos, lineStartPos + spacesToRemove)\n\n const cursorPosInLine = pos - lineStartPos\n if (cursorPosInLine <= spacesToRemove) {\n tr.setSelection(TextSelection.create(tr.doc, lineStartPos))\n }\n\n return true\n })\n }\n\n return editor.commands.command(({ tr }) => {\n const { from, to } = selection\n const text = state.doc.textBetween(from, to, '\\n', '\\n')\n const lines = text.split('\\n')\n const reverseIndentText = lines\n .map(line => {\n const leadingSpaces = line.match(/^ */)?.[0] || ''\n const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize)\n return line.slice(spacesToRemove)\n })\n .join('\\n')\n\n tr.replaceWith(from, to, state.schema.text(reverseIndentText))\n return true\n })\n },\n\n // exit node on triple enter\n Enter: ({ editor }) => {\n if (!this.options.exitOnTripleEnter) {\n return false\n }\n\n const { state } = editor\n const { selection } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n const endsWithDoubleNewline = $from.parent.textContent.endsWith('\\n\\n')\n\n if (!isAtEnd || !endsWithDoubleNewline) {\n return false\n }\n\n return editor\n .chain()\n .command(({ tr }) => {\n tr.delete($from.pos - 2, $from.pos)\n\n return true\n })\n .exitCode()\n .run()\n },\n\n // exit node on arrow down\n ArrowDown: ({ editor }) => {\n if (!this.options.exitOnArrowDown) {\n return false\n }\n\n const { state } = editor\n const { selection, doc } = state\n const { $from, empty } = selection\n\n if (!empty || $from.parent.type !== this.type) {\n return false\n }\n\n const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2\n\n if (!isAtEnd) {\n return false\n }\n\n const after = $from.after()\n\n if (after === undefined) {\n return false\n }\n\n const nodeAfter = doc.nodeAt(after)\n\n if (nodeAfter) {\n return editor.commands.command(({ tr }) => {\n tr.setSelection(Selection.near(doc.resolve(after)))\n return true\n })\n }\n\n return editor.commands.exitCode()\n },\n }\n },\n\n addInputRules() {\n return [\n textblockTypeInputRule({\n find: backtickInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n textblockTypeInputRule({\n find: tildeInputRegex,\n type: this.type,\n getAttributes: match => ({\n language: match[1],\n }),\n }),\n ]\n },\n\n addProseMirrorPlugins() {\n return [\n // this plugin creates a code block for pasted content from VS Code\n // we can also detect the copied code language\n new Plugin({\n key: new PluginKey('codeBlockVSCodeHandler'),\n props: {\n handlePaste: (view, event) => {\n if (!event.clipboardData) {\n return false\n }\n\n // don’t create a new code block within code blocks\n if (this.editor.isActive(this.type.name)) {\n return false\n }\n\n const text = event.clipboardData.getData('text/plain')\n const vscode = event.clipboardData.getData('vscode-editor-data')\n const vscodeData = vscode ? JSON.parse(vscode) : undefined\n const language = vscodeData?.mode\n\n if (!text || !language) {\n return false\n }\n\n const { tr, schema } = view.state\n\n // prepare a text node\n // strip carriage return chars from text pasted as code\n // see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd\n const textNode = schema.text(text.replace(/\\r\\n?/g, '\\n'))\n\n // create a code block with the text node\n // replace selection with the code block\n tr.replaceSelectionWith(this.type.create({ language }, textNode))\n\n if (tr.selection.$from.parent.type !== this.type) {\n // put cursor inside the newly created code block\n tr.setSelection(TextSelection.near(tr.doc.resolve(Math.max(0, tr.selection.from - 2))))\n }\n\n // store meta information\n // this is useful for other plugins that depends on the paste event\n // like the paste rule plugin\n tr.setMeta('paste', true)\n\n view.dispatch(tr)\n\n return true\n },\n },\n }),\n ]\n },\n})\n","import { CodeBlock } from './code-block.js'\n\nexport * from './code-block.js'\n\nexport default CodeBlock\n"],"mappings":";AAAA,SAAS,iBAAiB,MAAM,8BAA8B;AAC9D,SAAS,QAAQ,WAAW,WAAW,qBAAqB;AAgErD,IAAM,qBAAqB;AAK3B,IAAM,kBAAkB;AAMxB,IAAM,YAAY,KAAK,OAAyB;AAAA,EACrD,MAAM;AAAA,EAEN,aAAa;AACX,WAAO;AAAA,MACL,qBAAqB;AAAA,MACrB,mBAAmB;AAAA,MACnB,iBAAiB;AAAA,MACjB,iBAAiB;AAAA,MACjB,sBAAsB;AAAA,MACtB,SAAS;AAAA,MACT,gBAAgB,CAAC;AAAA,IACnB;AAAA,EACF;AAAA,EAEA,SAAS;AAAA,EAET,OAAO;AAAA,EAEP,OAAO;AAAA,EAEP,MAAM;AAAA,EAEN,UAAU;AAA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AAK,OAAO,SAAS,KAAK,KAAK,IAAI,GAAG;AACxC,qBAAO;AAAA,YACT;AAEA,kBAAM,OAAO,MAAM,cAAc,QAAQ,YAAY;AACrD,kBAAM,SAAS,MAAM,cAAc,QAAQ,oBAAoB;AAC/D,kBAAM,aAAa,SAAS,KAAK,MAAM,MAAM,IAAI;AACjD,kBAAM,WAAW,yCAAY;AAE7B,gBAAI,CAAC,QAAQ,CAAC,UAAU;AACtB,qBAAO;AAAA,YACT;AAEA,kBAAM,EAAE,IAAI,OAAO,IAAI,KAAK;AAK5B,kBAAM,WAAW,OAAO,KAAK,KAAK,QAAQ,UAAU,IAAI,CAAC;AAIzD,eAAG,qBAAqB,KAAK,KAAK,OAAO,EAAE,SAAS,GAAG,QAAQ,CAAC;AAEhE,gBAAI,GAAG,UAAU,MAAM,OAAO,SAAS,KAAK,MAAM;AAEhD,iBAAG,aAAa,cAAc,KAAK,GAAG,IAAI,QAAQ,KAAK,IAAI,GAAG,GAAG,UAAU,OAAO,CAAC,CAAC,CAAC,CAAC;AAAA,YACxF;AAKA,eAAG,QAAQ,SAAS,IAAI;AAExB,iBAAK,SAAS,EAAE;AAEhB,mBAAO;AAAA,UACT;AAAA,QACF;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACF,CAAC;;;AC/aD,IAAO,gBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tiptap/extension-code-block",
|
|
3
3
|
"description": "code block extension for tiptap",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.4.0",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@tiptap/core": "^3.
|
|
35
|
-
"@tiptap/pm": "^3.
|
|
34
|
+
"@tiptap/core": "^3.4.0",
|
|
35
|
+
"@tiptap/pm": "^3.4.0"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@tiptap/core": "^3.
|
|
39
|
-
"@tiptap/pm": "^3.
|
|
38
|
+
"@tiptap/core": "^3.4.0",
|
|
39
|
+
"@tiptap/pm": "^3.4.0"
|
|
40
40
|
},
|
|
41
41
|
"repository": {
|
|
42
42
|
"type": "git",
|
package/src/code-block.ts
CHANGED
|
@@ -23,6 +23,16 @@ export interface CodeBlockOptions {
|
|
|
23
23
|
* @example 'js'
|
|
24
24
|
*/
|
|
25
25
|
defaultLanguage: string | null | undefined
|
|
26
|
+
/**
|
|
27
|
+
* Enable tab key for indentation in code blocks.
|
|
28
|
+
* @default false
|
|
29
|
+
*/
|
|
30
|
+
enableTabIndentation: boolean
|
|
31
|
+
/**
|
|
32
|
+
* The number of spaces to use for tab indentation.
|
|
33
|
+
* @default 4
|
|
34
|
+
*/
|
|
35
|
+
tabSize: number
|
|
26
36
|
/**
|
|
27
37
|
* Custom HTML attributes that should be added to the rendered HTML tag.
|
|
28
38
|
* @default {}
|
|
@@ -73,6 +83,8 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
|
|
|
73
83
|
exitOnTripleEnter: true,
|
|
74
84
|
exitOnArrowDown: true,
|
|
75
85
|
defaultLanguage: null,
|
|
86
|
+
enableTabIndentation: false,
|
|
87
|
+
tabSize: 4,
|
|
76
88
|
HTMLAttributes: {},
|
|
77
89
|
}
|
|
78
90
|
},
|
|
@@ -168,6 +180,113 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
|
|
|
168
180
|
return false
|
|
169
181
|
},
|
|
170
182
|
|
|
183
|
+
// handle tab indentation
|
|
184
|
+
Tab: ({ editor }) => {
|
|
185
|
+
if (!this.options.enableTabIndentation) {
|
|
186
|
+
return false
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const { state } = editor
|
|
190
|
+
const { selection } = state
|
|
191
|
+
const { $from, empty } = selection
|
|
192
|
+
|
|
193
|
+
if ($from.parent.type !== this.type) {
|
|
194
|
+
return false
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const indent = ' '.repeat(this.options.tabSize)
|
|
198
|
+
|
|
199
|
+
if (empty) {
|
|
200
|
+
return editor.commands.insertContent(indent)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return editor.commands.command(({ tr }) => {
|
|
204
|
+
const { from, to } = selection
|
|
205
|
+
const text = state.doc.textBetween(from, to, '\n', '\n')
|
|
206
|
+
const lines = text.split('\n')
|
|
207
|
+
const indentedText = lines.map(line => indent + line).join('\n')
|
|
208
|
+
|
|
209
|
+
tr.replaceWith(from, to, state.schema.text(indentedText))
|
|
210
|
+
return true
|
|
211
|
+
})
|
|
212
|
+
},
|
|
213
|
+
|
|
214
|
+
// handle shift+tab reverse indentation
|
|
215
|
+
'Shift-Tab': ({ editor }) => {
|
|
216
|
+
if (!this.options.enableTabIndentation) {
|
|
217
|
+
return false
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const { state } = editor
|
|
221
|
+
const { selection } = state
|
|
222
|
+
const { $from, empty } = selection
|
|
223
|
+
|
|
224
|
+
if ($from.parent.type !== this.type) {
|
|
225
|
+
return false
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (empty) {
|
|
229
|
+
return editor.commands.command(({ tr }) => {
|
|
230
|
+
const { pos } = $from
|
|
231
|
+
const codeBlockStart = $from.start()
|
|
232
|
+
const codeBlockEnd = $from.end()
|
|
233
|
+
|
|
234
|
+
const allText = state.doc.textBetween(codeBlockStart, codeBlockEnd, '\n', '\n')
|
|
235
|
+
const lines = allText.split('\n')
|
|
236
|
+
|
|
237
|
+
let currentLineIndex = 0
|
|
238
|
+
let charCount = 0
|
|
239
|
+
const relativeCursorPos = pos - codeBlockStart
|
|
240
|
+
|
|
241
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
242
|
+
if (charCount + lines[i].length >= relativeCursorPos) {
|
|
243
|
+
currentLineIndex = i
|
|
244
|
+
break
|
|
245
|
+
}
|
|
246
|
+
charCount += lines[i].length + 1
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const currentLine = lines[currentLineIndex]
|
|
250
|
+
const leadingSpaces = currentLine.match(/^ */)?.[0] || ''
|
|
251
|
+
const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize)
|
|
252
|
+
|
|
253
|
+
if (spacesToRemove === 0) {
|
|
254
|
+
return true
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
let lineStartPos = codeBlockStart
|
|
258
|
+
for (let i = 0; i < currentLineIndex; i += 1) {
|
|
259
|
+
lineStartPos += lines[i].length + 1
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
tr.delete(lineStartPos, lineStartPos + spacesToRemove)
|
|
263
|
+
|
|
264
|
+
const cursorPosInLine = pos - lineStartPos
|
|
265
|
+
if (cursorPosInLine <= spacesToRemove) {
|
|
266
|
+
tr.setSelection(TextSelection.create(tr.doc, lineStartPos))
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return true
|
|
270
|
+
})
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return editor.commands.command(({ tr }) => {
|
|
274
|
+
const { from, to } = selection
|
|
275
|
+
const text = state.doc.textBetween(from, to, '\n', '\n')
|
|
276
|
+
const lines = text.split('\n')
|
|
277
|
+
const reverseIndentText = lines
|
|
278
|
+
.map(line => {
|
|
279
|
+
const leadingSpaces = line.match(/^ */)?.[0] || ''
|
|
280
|
+
const spacesToRemove = Math.min(leadingSpaces.length, this.options.tabSize)
|
|
281
|
+
return line.slice(spacesToRemove)
|
|
282
|
+
})
|
|
283
|
+
.join('\n')
|
|
284
|
+
|
|
285
|
+
tr.replaceWith(from, to, state.schema.text(reverseIndentText))
|
|
286
|
+
return true
|
|
287
|
+
})
|
|
288
|
+
},
|
|
289
|
+
|
|
171
290
|
// exit node on triple enter
|
|
172
291
|
Enter: ({ editor }) => {
|
|
173
292
|
if (!this.options.exitOnTripleEnter) {
|