Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix language being discarded when copy and pasting code block in new editor #1391

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silent-singers-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystatic/core': patch
---

Fix language being discarded when copy and pasting code block in new editor
24 changes: 16 additions & 8 deletions packages/keystatic/src/form/fields/markdoc/editor/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ const dividerDOM: DOMOutputSpec = [
}),
},
];
const codeDOM: DOMOutputSpec = [
'pre',
{ spellcheck: 'false' },
['code', {}, 0],
];
const hardBreakDOM: DOMOutputSpec = ['br'];

const olDOM: DOMOutputSpec = ['ol', {}, 0];
Expand Down Expand Up @@ -191,9 +186,22 @@ const nodeSpecs = {
},
marks: '',
code: true,
parseDOM: [{ tag: 'pre', preserveWhitespace: 'full' }],
toDOM() {
return codeDOM;
parseDOM: [
{
tag: 'pre',
preserveWhitespace: 'full',
getAttrs(node) {
if (typeof node === 'string') return {};
return { language: node.getAttribute('data-language') ?? '' };
},
},
],
toDOM(node) {
return [
'pre',
{ spellcheck: 'false', 'data-language': node.attrs.language },
['code', {}, 0],
];
},
},
list_item: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,42 @@ test('blockquote pasting', async () => {
</doc>
`);
});

test('codeblock pasting with language', async () => {
let dataTransfer;
const codeBlock = (
<code_block language="javascript">
<text>console.log(1);</text>
</code_block>
);
{
const { user } = renderEditor(
<doc>
<paragraph>
<anchor />
</paragraph>
{codeBlock}
<paragraph>
<head />
</paragraph>
</doc>
);

dataTransfer = await user.copy();
}
const { state, user } = renderEditor(
<doc>
<paragraph />
</doc>
);
await user.paste(dataTransfer);
expect(state()).toEqual(
<doc>
<paragraph />
{codeBlock}
<paragraph>
<cursor />
</paragraph>
</doc>
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ export function renderEditor(editorState: EditorStateDescription): {
rendered: ReturnType<typeof render>;
user: ReturnType<(typeof userEvent)['setup']>;
state: () => EditorStateDescription;
contentElement: HTMLElement;
} {
const viewRef = createRef<{ view: EditorView | null }>();
const user = userEvent.setup();
Expand All @@ -537,10 +538,19 @@ export function renderEditor(editorState: EditorStateDescription): {

viewRef.current!.view!.focus();

const contentElement = rendered.baseElement.querySelector(
'[contenteditable="true"]'
)!;

if (!(contentElement instanceof HTMLElement)) {
throw new Error('content element not found/not HTMLElement');
}

return {
state: () => editorState,
user,
rendered,
contentElement,
};
}

Expand Down
Loading