/** * 에디터 히스토리 항목을 만든다. * @param {string} value - 저장할 본문 * @param {{ start?: number, end?: number }} selection - 현재 선택 위치 * @returns {{ value: string, selectionStart: number, selectionEnd: number }} 히스토리 항목 */ export const createEditorHistoryEntry = (value, selection = {}) => { const safeValue = String(value ?? '') const start = Number(selection.start) const end = Number(selection.end) return { value: safeValue, selectionStart: Math.min(Math.max(0, Number.isFinite(start) ? start : 0), safeValue.length), selectionEnd: Math.min(Math.max(0, Number.isFinite(end) ? end : 0), safeValue.length) } } /** * 히스토리 항목의 본문 값을 반환한다. * @param {string|{ value?: string }} entry - 히스토리 항목 * @returns {string|null} 본문 값 */ export 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} 선택 위치 */ export 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) } }