Wrap selected text with typed pairs (#119)

Adds modular selection-pair handling for the rich editor and source mode so typing (, {, [, ', " or ` around a selection wraps it instead of replacing it.

Reviewed-on: https://codeberg.org/ArkHost/HelixNotes/pulls/119
This commit is contained in:
MF
2026-06-16 14:59:14 +02:00
committed by ArkHost
parent 7e480ad7e2
commit 8454f2eca8
4 changed files with 83 additions and 0 deletions
+24
View File
@@ -44,6 +44,8 @@
import { listen } from '@tauri-apps/api/event';
import { debounce } from '$lib/utils/debounce';
import { encryptSecretText, decryptSecretText, readSecretTitle } from '$lib/utils/secrets';
import { WrapSelectedText } from '$lib/editor/extensions/wrapSelectedText';
import { wrapTextareaSelection } from '$lib/editor/source/selectionPairs';
import GraphView from './GraphView.svelte';
import TagSuggestInput from './TagSuggestInput.svelte';
@@ -109,6 +111,25 @@
return true;
}
function handleSourceSelectionPair(event: KeyboardEvent) {
if (event.ctrlKey || event.metaKey || event.altKey || event.key.length !== 1) return false;
const ta = sourceElement;
if (!ta) return false;
const wrapped = wrapTextareaSelection(ta.value, ta.selectionStart, ta.selectionEnd, event.key);
if (!wrapped) return false;
event.preventDefault();
pushSourceHistoryImmediate();
sourceContent = wrapped.value;
tick().then(() => {
ta.setSelectionRange(wrapped.selectionStart, wrapped.selectionEnd);
pushSourceHistoryImmediate();
});
$editorDirty = true;
autoSave();
return true;
}
function closeAllDropdowns() {
headingDropdown = false;
colorDropdown = false;
@@ -3411,6 +3432,7 @@
}),
CtrlEndScrollPastEnd,
HeadingShortcuts,
WrapSelectedText,
SlashCommands,
MoveLineShortcuts,
TabIndent,
@@ -4880,6 +4902,7 @@
}}
onkeydown={(e) => {
if (handleSourceCtrlEnd(e)) return;
if (handleSourceSelectionPair(e)) return;
const mod = e.ctrlKey || e.metaKey;
if (e.key === 'Enter' && e.shiftKey && !mod) {
e.preventDefault();
@@ -4938,6 +4961,7 @@
}}
onkeydown={(e) => {
if (handleSourceCtrlEnd(e)) return;
if (handleSourceSelectionPair(e)) return;
const mod = e.ctrlKey || e.metaKey;
// Shift+Enter: insert two trailing spaces + newline for markdown hard break
if (e.key === 'Enter' && e.shiftKey && !mod) {