Skip to content

Commit 28360e6

Browse files
committed
feat: make selection to placeholder
1 parent dcd64b2 commit 28360e6

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

web/src/components/MemoEditor/handlers.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ const styleHighlightedText = (editor: EditorRefActions, delimiter: string) => {
5454
};
5555

5656
export function insertResourceText(editor: EditorRefActions, resources: Resource[], placeholder: string) {
57+
if (!placeholder) return;
58+
5759
let text = editor.getContent();
5860
const pos = text.indexOf(placeholder);
5961
if (pos === -1) return;
@@ -73,11 +75,17 @@ export function insertResourceText(editor: EditorRefActions, resources: Resource
7375

7476
// compute new cursorPos
7577
let cursorPos = editor.getCursorPosition();
76-
if (cursorPos >= pos + placeholder.length) cursorPos += inserting.length - placeholder.length;
77-
else if (cursorPos > pos) cursorPos = pos + inserting.length;
78+
let selectionLength = 0;
79+
80+
if (cursorPos >= pos + placeholder.length) {
81+
cursorPos += inserting.length - placeholder.length;
82+
} else if (cursorPos >= pos) {
83+
cursorPos = pos;
84+
selectionLength = inserting.length;
85+
}
7886

7987
text = text.slice(0, pos) + inserting + text.slice(pos + placeholder.length);
8088

8189
editor.setContent(text);
82-
editor.setCursorPosition(cursorPos);
90+
editor.setCursorPosition(cursorPos, cursorPos + selectionLength);
8391
}

web/src/components/MemoEditor/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,11 @@ const MemoEditor = observer((props: Props) => {
309309
let placeholder = "";
310310

311311
if (editor) {
312+
// create a placeholder for uploaded files, and select it.
312313
placeholder = `<resource_${Date.now()}>`;
314+
const position = editor.getCursorPosition();
313315
editor.insertText(placeholder);
316+
editor.setCursorPosition(position, position + placeholder.length);
314317
}
315318

316319
const resources = await uploadMultiFiles(event.clipboardData.files);

0 commit comments

Comments
 (0)