Skip to content

Commit 013818d

Browse files
areknawoYousefED
andauthored
feat: Code block (#1177)
* feat: Code block * fix: Export code block from core * feat: Customize code block util * feat: Code block fixes, improvements & tests * fix: Update snapshots * fix: Update snapshots (link menu) * fix: Update snapshots (link menu) * fix merge mistake * export customizeCodeBlock --------- Co-authored-by: Yousef <[email protected]>
1 parent 6f2e806 commit 013818d

File tree

60 files changed

+1632
-584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1632
-584
lines changed

examples/01-basic/03-all-blocks/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ export default function App() {
4444
type: "checkListItem",
4545
content: "Check List Item",
4646
},
47+
{
48+
type: "codeBlock",
49+
props: { language: "javascript" },
50+
content: "console.log('Hello, world!');",
51+
},
4752
{
4853
type: "table",
4954
content: {

package-lock.json

Lines changed: 208 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/core/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@
9797
"uuid": "^8.3.2",
9898
"y-prosemirror": "1.2.12",
9999
"y-protocols": "^1.0.6",
100-
"yjs": "^13.6.15"
100+
"yjs": "^13.6.15",
101+
"prosemirror-highlight": "^0.9.0",
102+
"shiki": "^1.22.0"
101103
},
102104
"devDependencies": {
103105
"@types/emoji-mart": "^3.0.14",
104-
"@types/hast": "^2.3.4",
106+
"@types/hast": "^3.0.0",
105107
"@types/uuid": "^8.3.4",
106108
"eslint": "^8.10.0",
107109
"jsdom": "^21.1.0",

packages/core/src/api/clipboard/fromClipboard/acceptedMIMETypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const acceptedMIMETypes = [
2+
"vscode-editor-data",
23
"blocknote/html",
34
"Files",
45
"text/html",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { BlockNoteEditor } from "../../../editor/BlockNoteEditor.js";
2+
import {
3+
BlockSchema,
4+
InlineContentSchema,
5+
StyleSchema,
6+
} from "../../../schema/index.js";
7+
8+
export async function handleVSCodePaste<
9+
BSchema extends BlockSchema,
10+
I extends InlineContentSchema,
11+
S extends StyleSchema
12+
>(event: ClipboardEvent, editor: BlockNoteEditor<BSchema, I, S>) {
13+
const view = editor.prosemirrorView;
14+
const { schema } = view.state;
15+
16+
if (!event.clipboardData) {
17+
return false;
18+
}
19+
20+
const text = event.clipboardData!.getData("text/plain");
21+
const vscode = event.clipboardData!.getData("vscode-editor-data");
22+
const vscodeData = vscode ? JSON.parse(vscode) : undefined;
23+
const language = vscodeData?.mode;
24+
25+
if (!text) {
26+
return false;
27+
}
28+
29+
if (!schema.nodes.codeBlock) {
30+
view.pasteText(text);
31+
32+
return true;
33+
}
34+
35+
if (!language) {
36+
return false;
37+
}
38+
39+
// strip carriage return chars from text pasted as code
40+
// see: https://github.com/ProseMirror/prosemirror-view/commit/a50a6bcceb4ce52ac8fcc6162488d8875613aacd
41+
editor._tiptapEditor.view.pasteHTML(
42+
`<pre><code class="language-${language}">${text.replace(
43+
/\r\n?/g,
44+
"\n"
45+
)}</code></pre>`
46+
);
47+
48+
return true;
49+
}

packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { nestedListsToBlockNoteStructure } from "../../parsers/html/util/nestedLists.js";
1111
import { acceptedMIMETypes } from "./acceptedMIMETypes.js";
1212
import { handleFileInsertion } from "./handleFileInsertion.js";
13+
import { handleVSCodePaste } from "./handleVSCodePaste.js";
1314

1415
export const createPasteFromClipboardExtension = <
1516
BSchema extends BlockSchema,
@@ -43,6 +44,11 @@ export const createPasteFromClipboardExtension = <
4344
return true;
4445
}
4546

47+
if (format === "vscode-editor-data") {
48+
handleVSCodePaste(event, editor);
49+
return true;
50+
}
51+
4652
if (format === "Files") {
4753
handleFileInsertion(event, editor);
4854
return true;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<pre><code class="bn-inline-content language-javascript">console.log('Hello, world!');</code></pre>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1"><div class="bn-block-content" data-content-type="codeBlock"><pre><code class="bn-inline-content language-javascript">console.log('Hello, world!');</code></pre></div></div></div></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<pre><code class="bn-inline-content language-javascript"></code></pre>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div class="bn-block-group" data-node-type="blockGroup"><div class="bn-block-outer" data-node-type="blockOuter" data-id="1"><div class="bn-block" data-node-type="blockContainer" data-id="1"><div class="bn-block-content" data-content-type="codeBlock"><pre><code class="bn-inline-content language-javascript"></code></pre></div></div></div></div>

0 commit comments

Comments
 (0)