fix(workspace): keep the cursor in a block that turns into a code block
MindSift CI & Docker Release / Build, Test in Container & Push (push) Successful in 1m58s

Converting via /code or ``` swapped the text field for the code editor's
and left the focus nowhere, so further typing triggered app shortcuts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sandrodachandClaude Opus 5.5 committed 2026-10-29 22:48:35 +01:00
1 parent 9124809f54
commit cc68d3f334
2 files changed
+19

No files matched your search

@@ -289,6 +289,20 @@ export const OutlinerBlock: React.FC<OutlinerBlockProps> = ({
adjustHeight();
}, [localContent, block.type, block.level, activeFocus, adjustHeight]);
// Turning the block into another type (slash menu, ``` or markdown shortcut) can swap the text field
// for a new one (e.g. the code editor); the cursor must stay in the block so typing goes on there
const refocusAfterTypeChangeRef = useRef(false);
useLayoutEffect(() => {
if (!refocusAfterTypeChangeRef.current) return;
refocusAfterTypeChangeRef.current = false;
const ta = textareaRef.current;
if (ta && document.activeElement !== ta) {
ta.focus();
const end = ta.value.length;
ta.setSelectionRange(end, end);
}
}, [block.type]);
useEffect(() => {
window.addEventListener('resize', adjustHeight);
// The field can be measured while hidden (phone layout shows the editor only after selecting a note);
@@ -335,6 +349,7 @@ export const OutlinerBlock: React.FC<OutlinerBlockProps> = ({
: undefined;
setLocalContent(clean);
localContentRef.current = clean;
if (cmd.type !== block.type) refocusAfterTypeChangeRef.current = true;
flushDebounce(clean, {
type: cmd.type,
...(langOverride !== undefined ? { lang: langOverride } : {}),
@@ -423,6 +438,7 @@ export const OutlinerBlock: React.FC<OutlinerBlockProps> = ({
}
if (typeOverride) {
refocusAfterTypeChangeRef.current = true;
flushDebounce(val, {
type: typeOverride,
...(langOverride !== undefined ? { lang: langOverride } : {}),
@@ -578,6 +594,7 @@ export const OutlinerBlock: React.FC<OutlinerBlockProps> = ({
const resolvedLang = rawLang ? resolveLanguageId(rawLang) || rawLang.toLowerCase() : '';
setLocalContent('');
localContentRef.current = '';
refocusAfterTypeChangeRef.current = true;
flushDebounce('', {
type: 'code',
lang: resolvedLang,