Skip to content

Commit b46fcc6

Browse files
committed
refactor: trying to fit it in
1 parent 809ea0a commit b46fcc6

File tree

25 files changed

+585
-674
lines changed

25 files changed

+585
-674
lines changed

packages/core/src/blks/Audio/definition.ts

Lines changed: 76 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { createFigureWithCaption } from "../../blocks/FileBlockContent/helpers/t
66
import { createLinkWithCaption } from "../../blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.js";
77
import {
88
createBlockConfig,
9-
createBlockSpec,
10-
} from "../../schema/blocks/playground.js";
9+
createBlockDefinition,
10+
} from "../../schema/index.js";
1111

1212
export const FILE_AUDIO_ICON_SVG =
1313
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor"><path d="M2 16.0001H5.88889L11.1834 20.3319C11.2727 20.405 11.3846 20.4449 11.5 20.4449C11.7761 20.4449 12 20.2211 12 19.9449V4.05519C12 3.93977 11.9601 3.8279 11.887 3.73857C11.7121 3.52485 11.3971 3.49335 11.1834 3.66821L5.88889 8.00007H2C1.44772 8.00007 1 8.44778 1 9.00007V15.0001C1 15.5524 1.44772 16.0001 2 16.0001ZM23 12C23 15.292 21.5539 18.2463 19.2622 20.2622L17.8445 18.8444C19.7758 17.1937 21 14.7398 21 12C21 9.26016 19.7758 6.80629 17.8445 5.15557L19.2622 3.73779C21.5539 5.75368 23 8.70795 23 12ZM18 12C18 10.0883 17.106 8.38548 15.7133 7.28673L14.2842 8.71584C15.3213 9.43855 16 10.64 16 12C16 13.36 15.3213 14.5614 14.2842 15.2841L15.7133 16.7132C17.106 15.6145 18 13.9116 18 12Z"></path></svg>';
@@ -42,89 +42,91 @@ const config = createBlockConfig((_ctx: AudioOptions) => ({
4242
},
4343
}));
4444

45-
export const definition = createBlockSpec(config).implementation((config) => ({
46-
parse: (element) => {
47-
if (element.tagName === "AUDIO") {
48-
// Ignore if parent figure has already been parsed.
49-
if (element.closest("figure")) {
50-
return undefined;
51-
}
52-
53-
return parseAudioElement(element as HTMLAudioElement);
54-
}
45+
export const definition = createBlockDefinition(config).implementation(
46+
(config) => ({
47+
parse: (element) => {
48+
if (element.tagName === "AUDIO") {
49+
// Ignore if parent figure has already been parsed.
50+
if (element.closest("figure")) {
51+
return undefined;
52+
}
5553

56-
if (element.tagName === "FIGURE") {
57-
const parsedFigure = parseFigureElement(element, "audio");
58-
if (!parsedFigure) {
59-
return undefined;
54+
return parseAudioElement(element as HTMLAudioElement);
6055
}
6156

62-
const { targetElement, caption } = parsedFigure;
57+
if (element.tagName === "FIGURE") {
58+
const parsedFigure = parseFigureElement(element, "audio");
59+
if (!parsedFigure) {
60+
return undefined;
61+
}
6362

64-
return {
65-
...parseAudioElement(targetElement as HTMLAudioElement),
66-
caption,
67-
};
68-
}
63+
const { targetElement, caption } = parsedFigure;
6964

70-
return undefined;
71-
},
72-
render: (block, editor) => {
73-
const icon = document.createElement("div");
74-
icon.innerHTML = config.icon ?? FILE_AUDIO_ICON_SVG;
65+
return {
66+
...parseAudioElement(targetElement as HTMLAudioElement),
67+
caption,
68+
};
69+
}
7570

76-
const audio = document.createElement("audio");
77-
audio.className = "bn-audio";
78-
if (editor.resolveFileUrl) {
79-
editor.resolveFileUrl(block.props.url).then((downloadUrl) => {
80-
audio.src = downloadUrl;
81-
});
82-
} else {
83-
audio.src = block.props.url;
84-
}
85-
audio.controls = true;
86-
audio.contentEditable = "false";
87-
audio.draggable = false;
71+
return undefined;
72+
},
73+
render: (block, editor) => {
74+
const icon = document.createElement("div");
75+
icon.innerHTML = config.icon ?? FILE_AUDIO_ICON_SVG;
8876

89-
return createFileBlockWrapper(
90-
block,
91-
editor,
92-
{ dom: audio },
93-
editor.dictionary.file_blocks.audio.add_button_text,
94-
icon.firstElementChild as HTMLElement,
95-
);
96-
},
97-
toExternalHTML(block) {
98-
if (!block.props.url) {
99-
const div = document.createElement("p");
100-
div.textContent = "Add audio";
77+
const audio = document.createElement("audio");
78+
audio.className = "bn-audio";
79+
if (editor.resolveFileUrl) {
80+
editor.resolveFileUrl(block.props.url).then((downloadUrl) => {
81+
audio.src = downloadUrl;
82+
});
83+
} else {
84+
audio.src = block.props.url;
85+
}
86+
audio.controls = true;
87+
audio.contentEditable = "false";
88+
audio.draggable = false;
10189

102-
return {
103-
dom: div,
104-
};
105-
}
90+
return createFileBlockWrapper(
91+
block,
92+
editor,
93+
{ dom: audio },
94+
editor.dictionary.file_blocks.audio.add_button_text,
95+
icon.firstElementChild as HTMLElement,
96+
);
97+
},
98+
toExternalHTML(block) {
99+
if (!block.props.url) {
100+
const div = document.createElement("p");
101+
div.textContent = "Add audio";
106102

107-
let audio;
108-
if (block.props.showPreview) {
109-
audio = document.createElement("audio");
110-
audio.src = block.props.url;
111-
} else {
112-
audio = document.createElement("a");
113-
audio.href = block.props.url;
114-
audio.textContent = block.props.name || block.props.url;
115-
}
103+
return {
104+
dom: div,
105+
};
106+
}
116107

117-
if (block.props.caption) {
108+
let audio;
118109
if (block.props.showPreview) {
119-
return createFigureWithCaption(audio, block.props.caption);
110+
audio = document.createElement("audio");
111+
audio.src = block.props.url;
120112
} else {
121-
return createLinkWithCaption(audio, block.props.caption);
113+
audio = document.createElement("a");
114+
audio.href = block.props.url;
115+
audio.textContent = block.props.name || block.props.url;
122116
}
123-
}
124117

125-
return {
126-
dom: audio,
127-
};
128-
},
129-
runsBefore: ["file"],
130-
}));
118+
if (block.props.caption) {
119+
if (block.props.showPreview) {
120+
return createFigureWithCaption(audio, block.props.caption);
121+
} else {
122+
return createLinkWithCaption(audio, block.props.caption);
123+
}
124+
}
125+
126+
return {
127+
dom: audio,
128+
};
129+
},
130+
runsBefore: ["file"],
131+
}),
132+
);

packages/core/src/blks/BulletListItem/definition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { defaultProps } from "../../blocks/defaultProps.js";
44
import { getListItemContent } from "../../blocks/ListItemBlockContent/getListItemContent.js";
55
import {
66
createBlockConfig,
7+
createBlockDefinition,
78
createBlockNoteExtension,
8-
createBlockSpec,
9-
} from "../../schema/blocks/playground.js";
9+
} from "../../schema/index.js";
1010
import { handleEnter } from "../utils/listItemEnterHandler.js";
1111

1212
const config = createBlockConfig(() => ({
@@ -17,7 +17,7 @@ const config = createBlockConfig(() => ({
1717
content: "inline",
1818
}));
1919

20-
export const definition = createBlockSpec(config).implementation(
20+
export const definition = createBlockDefinition(config).implementation(
2121
() => ({
2222
parse(element) {
2323
if (element.tagName !== "LI") {

packages/core/src/blks/CheckListItem/definition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { defaultProps } from "../../blocks/defaultProps.js";
44
import { getListItemContent } from "../../blocks/ListItemBlockContent/getListItemContent.js";
55
import {
66
createBlockConfig,
7+
createBlockDefinition,
78
createBlockNoteExtension,
8-
createBlockSpec,
9-
} from "../../schema/blocks/playground.js";
9+
} from "../../schema/index.js";
1010
import { handleEnter } from "../utils/listItemEnterHandler.js";
1111

1212
const config = createBlockConfig(() => ({
@@ -18,7 +18,7 @@ const config = createBlockConfig(() => ({
1818
content: "inline",
1919
}));
2020

21-
export const definition = createBlockSpec(config).implementation(
21+
export const definition = createBlockDefinition(config).implementation(
2222
() => ({
2323
parse(element) {
2424
if (element.tagName === "input") {

packages/core/src/blks/Code/definition.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { HighlighterGeneric } from "@shikijs/types";
22
import {
33
createBlockConfig,
4+
createBlockDefinition,
45
createBlockNoteExtension,
5-
createBlockSpec,
6-
} from "../../schema/blocks/playground.js";
6+
} from "../../schema/index.js";
77
import { lazyShikiPlugin } from "./shiki.js";
88

99
export type CodeBlockOptions = {
@@ -54,7 +54,7 @@ export type CodeBlockOptions = {
5454
};
5555

5656
const config = createBlockConfig(
57-
({ defaultLanguage = "text" }: CodeBlockOptions) => ({
57+
({ defaultLanguage = "text" }: CodeBlockOptions = {}) => ({
5858
type: "codeBlock" as const,
5959
propSchema: {
6060
language: {
@@ -69,8 +69,8 @@ const config = createBlockConfig(
6969
}),
7070
);
7171

72-
export const definition = createBlockSpec(config).implementation(
73-
(options) => ({
72+
export const definition = createBlockDefinition(config).implementation(
73+
(options = {}) => ({
7474
parse: (e) => {
7575
const pre = e.querySelector("pre");
7676
if (!pre) {
@@ -130,7 +130,7 @@ export const definition = createBlockSpec(config).implementation(
130130
};
131131
},
132132
}),
133-
(options) => {
133+
(options = {}) => {
134134
return [
135135
createBlockNoteExtension({
136136
key: "code-block-highlighter",

packages/core/src/blks/File/definition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { createFileBlockWrapper } from "../../blocks/FileBlockContent/helpers/re
55
import { createLinkWithCaption } from "../../blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.js";
66
import {
77
createBlockConfig,
8-
createBlockSpec,
9-
} from "../../schema/blocks/playground.js";
8+
createBlockDefinition,
9+
} from "../../schema/index.js";
1010

1111
const config = createBlockConfig(() => ({
1212
type: "file" as const,
@@ -31,7 +31,7 @@ const config = createBlockConfig(() => ({
3131
},
3232
}));
3333

34-
export const definition = createBlockSpec(config).implementation(() => ({
34+
export const definition = createBlockDefinition(config).implementation(() => ({
3535
parse: (element) => {
3636
if (element.tagName === "EMBED") {
3737
// Ignore if parent figure has already been parsed.

packages/core/src/blks/Heading/definition.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { createToggleWrapper } from "../../blocks/ToggleWrapper/createToggleWrap
44
import {
55
createBlockConfig,
66
createBlockNoteExtension,
7-
createBlockSpec,
8-
} from "../../schema/blocks/playground.js";
7+
createBlockDefinition,
8+
} from "../../schema/index.js";
99

1010
const HEADING_LEVELS = [1, 2, 3, 4, 5, 6] as const;
1111

@@ -21,7 +21,7 @@ const config = createBlockConfig(
2121
defaultLevel = 1,
2222
levels = HEADING_LEVELS,
2323
allowToggleHeadings = true,
24-
}: HeadingOptions) => ({
24+
}: HeadingOptions = {}) => ({
2525
type: "heading" as const,
2626
propSchema: {
2727
level: { default: defaultLevel, values: levels },
@@ -31,8 +31,8 @@ const config = createBlockConfig(
3131
}),
3232
);
3333

34-
export const definition = createBlockSpec(config).implementation(
35-
({ allowToggleHeadings = true }) => ({
34+
export const definition = createBlockDefinition(config).implementation(
35+
({ allowToggleHeadings = true }: HeadingOptions = {}) => ({
3636
parse(e) {
3737
const heading = e.querySelector("h1, h2, h3, h4, h5, h6");
3838
if (!heading) {
@@ -59,11 +59,11 @@ export const definition = createBlockSpec(config).implementation(
5959
};
6060
},
6161
}),
62-
(options) => [
62+
({ levels = HEADING_LEVELS }: HeadingOptions = {}) => [
6363
createBlockNoteExtension({
6464
key: "heading-shortcuts",
6565
keyboardShortcuts: Object.fromEntries(
66-
(options.levels ?? HEADING_LEVELS).map((level) => [
66+
levels.map((level) => [
6767
`Mod-Alt-${level}`,
6868
({ editor }) =>
6969
editor.transact((tr) => {
@@ -88,7 +88,7 @@ export const definition = createBlockSpec(config).implementation(
8888
}),
8989
]) ?? [],
9090
),
91-
inputRules: (options.levels ?? HEADING_LEVELS).map((level) => ({
91+
inputRules: levels.map((level) => ({
9292
find: new RegExp(`^(#{${level}})\\s$`),
9393
replace({ match }: { match: RegExpMatchArray }) {
9494
return {

0 commit comments

Comments
 (0)