Skip to content

Commit 1dd30c8

Browse files
authored
fix: don't paste url as image if shift pressed (#750)
1 parent 6f55691 commit 1dd30c8

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/extensions/markdown/Image/imageUrlPaste/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {Plugin} from 'prosemirror-state';
22

3+
import {InputState} from 'src/utils/input-state';
4+
35
import type {ParseInsertedUrlAsImage} from '../../../../bundle';
46
import type {ExtensionAuto} from '../../../../core';
57
import {DataTransferType} from '../../../behavior/Clipboard/utils';
@@ -13,13 +15,22 @@ export type ImageUrlPasteOptions = {
1315
};
1416

1517
export const imageUrlPaste: ExtensionAuto<ImageUrlPasteOptions> = (builder, opts) => {
18+
const inputState = new InputState();
19+
1620
builder.addPlugin(
1721
() =>
1822
new Plugin({
1923
props: {
2024
handleDOMEvents: {
25+
keydown(_view, e) {
26+
inputState.keydown(e);
27+
},
28+
keyup(_view, e) {
29+
inputState.keyup(e);
30+
},
2131
paste(view, e) {
2232
if (
33+
!inputState.shiftKey ||
2334
!opts.parseInsertedUrlAsImage ||
2435
!e.clipboardData ||
2536
view.state.selection.$from.parent.type.spec.code

src/extensions/yfm/ImgSize/ImagePaste/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import {Fragment, type Node, type Schema, Slice} from 'prosemirror-model';
33
import {Plugin} from 'prosemirror-state';
44
import {dropPoint} from 'prosemirror-transform';
55

6+
import {InputState} from 'src/utils/input-state';
7+
68
import type {ParseInsertedUrlAsImage} from '../../../../bundle';
79
import {type ExtensionAuto, getLoggerFromState} from '../../../../core';
810
import {isFunction} from '../../../../lodash';
@@ -37,11 +39,19 @@ export const ImagePaste: ExtensionAuto<ImagePasteOptions> = (builder, opts) => {
3739
} is not a function`,
3840
);
3941

42+
const inputState = new InputState();
43+
4044
builder.addPlugin(
4145
() =>
4246
new Plugin({
4347
props: {
4448
handleDOMEvents: {
49+
keydown(_view, e) {
50+
inputState.keydown(e);
51+
},
52+
keyup(_view, e) {
53+
inputState.keyup(e);
54+
},
4555
paste(view, e) {
4656
const logger = getLoggerFromState(view.state).nested({
4757
plugin: 'image-paste',
@@ -60,7 +70,7 @@ export const ImagePaste: ExtensionAuto<ImagePasteOptions> = (builder, opts) => {
6070
opts,
6171
).run();
6272
return true;
63-
} else if (parseInsertedUrlAsImage) {
73+
} else if (!inputState.shiftKey && parseInsertedUrlAsImage) {
6474
const {imageUrl, title} =
6575
parseInsertedUrlAsImage(
6676
e.clipboardData?.getData(DataTransferType.Text) ?? '',

src/markup/codemirror/create.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
placeholder,
1818
} from '@codemirror/view';
1919

20+
import {InputState} from 'src/utils/input-state';
21+
2022
import type {ParseInsertedUrlAsImage} from '../../bundle';
2123
import type {EventMap} from '../../bundle/Editor';
2224
import {ActionName} from '../../bundle/config/action-names';
@@ -121,6 +123,8 @@ export function createCodemirror(params: CreateCodemirrorParams) {
121123
extensions.push(history());
122124
}
123125

126+
const inputState = new InputState();
127+
124128
extensions.push(
125129
syntaxHighlighting(gravityHighlightStyle),
126130
LoggerFacet.of(logger),
@@ -177,6 +181,12 @@ export function createCodemirror(params: CreateCodemirrorParams) {
177181
scroll(event) {
178182
onScroll(event);
179183
},
184+
keydown(event) {
185+
inputState.keydown(event);
186+
},
187+
keyup(event) {
188+
inputState.keyup(event);
189+
},
180190
paste(event, editor) {
181191
if (!event.clipboardData) return false;
182192

@@ -235,7 +245,7 @@ export function createCodemirror(params: CreateCodemirrorParams) {
235245
}
236246
}
237247

238-
if (parseInsertedUrlAsImage) {
248+
if (!inputState.shiftKey && parseInsertedUrlAsImage) {
239249
const linkMatches = currentLine.matchAll(linkRegex);
240250
const cursorPositionInCurrentLine = from - line.from;
241251
const isInsertedInsideLink = linkMatches.some(

src/utils/input-state.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export class InputState {
2+
#shiftKey = false;
3+
4+
get shiftKey() {
5+
return this.#shiftKey;
6+
}
7+
8+
keydown(e: KeyboardEvent) {
9+
this.#shiftKey = e.code.startsWith('Shift') || e.shiftKey;
10+
}
11+
12+
keyup(e: KeyboardEvent) {
13+
if (e.code.startsWith('Shift')) this.#shiftKey = false;
14+
}
15+
}

0 commit comments

Comments
 (0)