에디터 히스토리 로직 분리

This commit is contained in:
2026-07-24 17:39:29 +09:00
parent 31cae6f90b
commit bad5a9081a
7 changed files with 135 additions and 62 deletions

View File

@@ -16,6 +16,11 @@ import {
getMarkdownLineBodyPrefixLength,
getSelectionOffsetInElement
} from '../../lib/markdown-live-selection.js'
import {
createEditorHistoryEntry,
getEditorHistoryEntrySelection,
getEditorHistoryEntryValue
} from '../../lib/markdown-editor-history.js'
import {
filterSlashCommands,
getSlashCommandFocusTarget,
@@ -664,63 +669,6 @@ const isCodeFenceBodyLine = (lines, lineIndex) => {
return false
}
/**
* 에디터 히스토리 항목을 만든다.
* @param {string} value - 저장할 본문
* @returns {{ value: string, selectionStart: number, selectionEnd: number }} 히스토리 항목
*/
const createEditorHistoryEntry = (value) => {
const safeValue = String(value ?? '')
return {
value: safeValue,
selectionStart: Math.min(lastSelectionState.value.start, safeValue.length),
selectionEnd: Math.min(lastSelectionState.value.end, safeValue.length)
}
}
/**
* 히스토리 항목의 본문 값을 반환한다.
* @param {string|{ value?: string }} entry - 히스토리 항목
* @returns {string|null} 본문 값
*/
const getEditorHistoryEntryValue = (entry) => {
if (typeof entry === 'string') {
return entry
}
if (entry && typeof entry.value === 'string') {
return entry.value
}
return null
}
/**
* 히스토리 항목의 선택 위치를 반환한다.
* @param {string|{ value?: string, selectionStart?: number, selectionEnd?: number }} entry - 히스토리 항목
* @param {string} value - 복원된 본문
* @returns {{ start: number, end: number }|null} 선택 위치
*/
const getEditorHistoryEntrySelection = (entry, value) => {
if (!entry || typeof entry === 'string') {
return null
}
const length = String(value ?? '').length
const start = Number(entry.selectionStart)
const end = Number(entry.selectionEnd)
if (!Number.isFinite(start) || !Number.isFinite(end)) {
return null
}
return {
start: Math.min(Math.max(0, start), length),
end: Math.min(Math.max(0, end), length)
}
}
/**
* 현재 선택 위치에 맞춰 에디터 포커스를 복원한다.
* @param {{ start: number, end: number }|null} selection - 복원할 선택 영역
@@ -765,7 +713,7 @@ const recordEditorHistory = (previousValue) => {
const shouldGroup = now - lastEditorHistoryRecordAt < 900
if (!shouldGroup || !editorUndoStack.value.length) {
editorUndoStack.value = [...editorUndoStack.value, createEditorHistoryEntry(value)].slice(-EDITOR_UNDO_LIMIT)
editorUndoStack.value = [...editorUndoStack.value, createEditorHistoryEntry(value, lastSelectionState.value)].slice(-EDITOR_UNDO_LIMIT)
}
editorRedoStack.value = []
@@ -788,7 +736,7 @@ const restoreEditorHistory = (direction) => {
}
sourceStack.value = sourceStack.value.slice(0, -1)
targetStack.value = [...targetStack.value, createEditorHistoryEntry(markdownValue.value ?? '')].slice(-EDITOR_UNDO_LIMIT)
targetStack.value = [...targetStack.value, createEditorHistoryEntry(markdownValue.value ?? '', lastSelectionState.value)].slice(-EDITOR_UNDO_LIMIT)
isRestoringEditorHistory.value = true
markdownValue.value = nextValue
lastEditorHistoryRecordAt = 0