Skip to content

Commit 7ae1d59

Browse files
committed
fix(markup): fixed paste handling in markup mode
1 parent cdadad4 commit 7ae1d59

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

demo/components/Playground.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type PlaygroundProps = {
8282
directiveSyntax?: DirectiveSyntaxValue;
8383
disabledHTMLBlockModes?: EmbeddingMode[];
8484
disableMarkdownItAttrs?: boolean;
85+
markupParseHtmlOnPaste?: boolean;
8586
} & Pick<UseMarkdownEditorProps, 'experimental' | 'wysiwygConfig'> &
8687
Pick<
8788
MarkdownEditorViewProps,
@@ -133,6 +134,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
133134
directiveSyntax,
134135
disabledHTMLBlockModes,
135136
disableMarkdownItAttrs,
137+
markupParseHtmlOnPaste,
136138
} = props;
137139
const [editorMode, setEditorMode] = useState<MarkdownEditorMode>(initialEditor ?? 'wysiwyg');
138140
const [mdRaw, setMdRaw] = useState<MarkupString>(initial || '');
@@ -236,6 +238,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
236238
: undefined,
237239
},
238240
markupConfig: {
241+
parseHtmlOnPaste: true,
239242
extensions: markupConfigExtensions,
240243
parseInsertedUrlAsImage,
241244
renderPreview,
@@ -260,6 +263,7 @@ export const Playground = memo<PlaygroundProps>((props) => {
260263
experimental?.prepareRawMarkup,
261264
directiveSyntax,
262265
disableMarkdownItAttrs,
266+
markupParseHtmlOnPaste,
263267
],
264268
);
265269

src/markup/codemirror/create.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export function createCodemirror(params: CreateCodemirrorParams) {
178178
onScroll(event);
179179
},
180180
paste(event, editor) {
181-
if (!event.clipboardData) return;
181+
if (!event.clipboardData) return false;
182182

183183
const pasteLogger = logger.nested({
184184
domEvent: 'paste',
@@ -197,7 +197,7 @@ export function createCodemirror(params: CreateCodemirrorParams) {
197197
logger.event({event: 'paste-markup'});
198198
const reindentedYfmContent = smartReindent(yfmContent, currentLine);
199199
editor.dispatch(editor.state.replaceSelection(reindentedYfmContent));
200-
return;
200+
return true;
201201
}
202202

203203
// checking if a copy buffer content is suitable for convertion
@@ -231,7 +231,7 @@ export function createCodemirror(params: CreateCodemirrorParams) {
231231
currentLine,
232232
);
233233
editor.dispatch(editor.state.replaceSelection(reindentedHtmlContent));
234-
return;
234+
return true;
235235
}
236236
}
237237

@@ -260,6 +260,7 @@ export function createCodemirror(params: CreateCodemirrorParams) {
260260
title,
261261
},
262262
])(editor);
263+
return true;
263264
}
264265
}
265266
}
@@ -271,7 +272,10 @@ export function createCodemirror(params: CreateCodemirrorParams) {
271272
if (pastedText !== reindentedText) {
272273
editor.dispatch(editor.state.replaceSelection(reindentedText));
273274
event.preventDefault();
275+
return true;
274276
}
277+
278+
return false;
275279
},
276280
}),
277281
);

src/utils/clipboard.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ export function shouldSkipHtmlConversion(clipboardData: DataTransfer): boolean {
5555
// If there's no HTML content, skip conversion
5656
if (!hasHtml) return true;
5757

58+
// Check for HTML only (text/html)
59+
if (clipboardData.types.length === 1) return false;
60+
5861
// Check for standard HTML clipboard (text/plain + text/html)
5962
if (clipboardData.types.length === 2) return false;
6063

0 commit comments

Comments
 (0)