Repository navigation
Closed
Labels
Description
Problem
The current cursor positioning implementation uses $effects that watch reactive state and then imperatively call controller methods. While functional, this approach:
- Doesn't truly leverage Svelte's reactive architecture (reactive triggers → imperative calls)
- Will become harder to maintain as we add features (multi-pane, node links, programmatic focus)
- Scatters cursor positioning logic across effects in multiple components
- Makes debugging difficult (effect execution order, hidden side effects)
Proposed Solution
Implement a reactive action-based architecture using Svelte actions to encapsulate imperative DOM manipulation while keeping state management purely reactive.
Architecture
FocusManager (unified cursorPosition state)
↓ reactive $derived
base-node (declarative props)
↓ use:positionCursor directive
Svelte Action (encapsulated DOM manipulation)
FocusManager Consolidation
Replace separate state properties with unified cursor position:
type CursorPosition =
| { nodeId: string; type: 'default' }
| { nodeId: string; type: 'line-column'; line: number; column: number }
| { nodeId: string; type: 'arrow-navigation'; direction: 'up' | 'down'; pixelOffset: number }
| { nodeId: string; type: 'link'; highlightDuration?: number };
class FocusManager {
cursorPosition = $state<CursorPosition | null>(null);
focusIntent = $state<'edit' | 'highlight' | 'scroll-to' | null>(null);
// Clean API for all focus operations
focusNode(nodeId: string, intent: 'edit' | 'highlight' = 'edit');
focusNodeAtLine(nodeId: string, line: number, column: number = 0);
focusNodeFromArrowNav(nodeId: string, direction: 'up' | 'down', pixelOffset: number);
focusNodeFromLink(nodeId: string, highlightDuration?: number);
}base-node.svelte - Declarative Template
<script>
let cursorData = $derived(
focusManager.cursorPosition?.nodeId === nodeId
? focusManager.cursorPosition
: null
);
let shouldHighlight = $derived(
cursorData && focusManager.focusIntent === 'scroll-to'
);
</script>
<div class:highlighted={shouldHighlight}>
{#if isEditing}
<textarea use:positionCursor={{ data: cursorData, controller }} />
{/if}
</div>positionCursor Action - Encapsulated DOM Logic
export const positionCursor: Action<HTMLTextAreaElement, { data: CursorPosition | null; controller: TextareaController }> = (element, params) => {
function apply(data: CursorPosition | null) {
if (!data) return;
requestAnimationFrame(() => {
switch (data.type) {
case 'arrow-navigation':
const pos = params.controller.getPositionFromPixelOffset(
data.direction === 'down' ? 0 : params.controller.getLineCount() - 1,
data.pixelOffset
);
element.selectionStart = element.selectionEnd = pos;
break;
// ... other cases
}
element.focus();
});
}
apply(params.data);
return {
update(newParams) { apply(newParams.data); }
};
};Benefits
- ✅ Truly reactive - State changes flow declaratively through $derived
- ✅ Scalable - Supports multi-pane, links, programmatic focus naturally
- ✅ Maintainable - Clear data flow, imperative code encapsulated in actions
- ✅ Debuggable - Inspect state in DevTools, trace action parameters
- ✅ Svelte idiomatic - Uses actions for their intended purpose
- ✅ Single source of truth - FocusManager is the authority for all cursor operations
Implementation Plan
- Create positionCursor action - Encapsulate DOM manipulation logic
- Consolidate FocusManager state - Replace separate properties with unified cursorPosition
- Update base-node.svelte - Use $derived and action directive
- Update all call sites - Use new FocusManager API methods
- Remove old $effects - Clean up imperative effect code
- Add tests - Verify all cursor positioning scenarios
Estimated Effort
- Timeline: 1-2 days
- Risk: Low (can test incrementally, feature-by-feature)
- ROI: High (foundation for all future focus features)
Related Issues
- Depends on: Current TextareaController migration (Migrate to textarea-based editor (Logseq approach) #274)
- Enables: Multi-pane support (future)
- Enables: Node link navigation with highlighting (future)
- Enables: Programmatic focus API (future)
Acceptance Criteria
- FocusManager has unified cursorPosition state
- base-node uses $derived for cursor data (no $effects for cursor positioning)
- positionCursor action handles all DOM cursor manipulation
- Arrow navigation maintains horizontal position (no regression)
- Click-to-edit works correctly
- Node split/merge cursor positioning works
- No visible cursor jumping
- Code is more maintainable than current implementation
Activity
Refactor cursor positioning to reactive action-based architecture
Update startup sequence to include pulling latest changes
Document reactive action-based cursor positioning architecture
Refactor cursor positioning to reactive action-based architecture (#289)
fix: Fix quote-block placeholder persistence, multiline content, and …
refactor: Replace $effect blocks with $derived patterns (Phase 1) (#316)
refactor: Move HeaderNode newline stripping from $effect to event han…
Refactor cursor positioning to reactive action-based architecture (#289)
fix: Fix quote-block placeholder persistence, multiline content, and …
refactor: Replace $effect blocks with $derived patterns (Phase 1) (#316)
refactor: Move HeaderNode newline stripping from $effect to event han…
Refactor cursor positioning to reactive action-based architecture (#289)
fix: Fix quote-block placeholder persistence, multiline content, and …
refactor: Replace $effect blocks with $derived patterns (Phase 1) (#316)
refactor: Move HeaderNode newline stripping from $effect to event han…
Refactor cursor positioning to reactive action-based architecture (#289)
fix: Fix quote-block placeholder persistence, multiline content, and …
refactor: Replace $effect blocks with $derived patterns (Phase 1) (#316)
refactor: Move HeaderNode newline stripping from $effect to event han…