-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection-editor.ts
More file actions
200 lines (175 loc) · 5.81 KB
/
section-editor.ts
File metadata and controls
200 lines (175 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {
Component,
ElementRef,
input,
output,
viewChild,
effect,
OnDestroy,
signal,
computed,
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { EditorState } from '@codemirror/state';
import { EditorView, keymap, lineNumbers, highlightActiveLine } from '@codemirror/view';
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands';
import { markdown } from '@codemirror/lang-markdown';
import { oneDark } from '@codemirror/theme-one-dark';
import { syntaxHighlighting, defaultHighlightStyle } from '@codemirror/language';
import { type SpecSection } from '../../models/spec.model';
import {
type ContentBlock,
type MarkdownTable,
parseContentBlocks,
serializeContentBlocks,
} from '../../models/markdown-table';
import { TableEditorComponent } from '../table-editor/table-editor';
@Component({
selector: 'app-section-editor',
standalone: true,
imports: [FormsModule, TableEditorComponent],
templateUrl: './section-editor.html',
styleUrl: './section-editor.scss',
})
export class SectionEditorComponent implements OnDestroy {
readonly section = input.required<SpecSection>();
readonly sectionIndex = input.required<number>();
readonly totalSections = input.required<number>();
readonly contentChange = output<string>();
readonly headingChange = output<string>();
readonly navigate = output<number>();
private readonly editorHost = viewChild<ElementRef<HTMLDivElement>>('sectionEditorHost');
private view: EditorView | null = null;
private suppressUpdate = false;
private suppressBlockParse = false;
private currentIndex = -1;
protected readonly headingValue = signal('');
/** Parsed content blocks for structured editing mode */
protected readonly contentBlocks = signal<ContentBlock[]>([]);
/** Whether this section has any tables (and should use structured mode) */
protected readonly hasTable = computed(() =>
this.contentBlocks().some((b) => b.type === 'table'),
);
constructor() {
effect(() => {
const sec = this.section();
const idx = this.sectionIndex();
// Parse content blocks on every section change (unless change came from structured editor)
if (this.suppressBlockParse) {
this.suppressBlockParse = false;
} else {
const blocks = parseContentBlocks(sec.content);
this.contentBlocks.set(blocks);
}
this.headingValue.set(sec.heading);
// If section changed, reset
if (this.currentIndex !== idx) {
this.destroyEditor();
this.currentIndex = idx;
}
});
// Separate effect for CodeMirror — only runs when NOT in structured mode
effect(() => {
const host = this.editorHost()?.nativeElement;
if (!host) return; // host doesn't exist (structured mode or not rendered yet)
const sec = this.section();
const idx = this.sectionIndex();
if (this.hasTable()) {
this.destroyEditor();
return;
}
// Same index — update existing editor if needed
if (this.view && this.currentIndex === idx) {
if (!this.suppressUpdate) {
const current = this.view.state.doc.toString();
if (current !== sec.content) {
this.view.dispatch({
changes: { from: 0, to: current.length, insert: sec.content },
});
}
}
this.suppressUpdate = false;
return;
}
this.destroyEditor();
this.view = new EditorView({
state: EditorState.create({
doc: sec.content,
extensions: [
lineNumbers(),
highlightActiveLine(),
EditorView.lineWrapping,
history(),
keymap.of([...defaultKeymap, ...historyKeymap]),
markdown(),
syntaxHighlighting(defaultHighlightStyle),
oneDark,
EditorView.updateListener.of((update) => {
if (update.docChanged) {
this.suppressUpdate = true;
this.contentChange.emit(update.state.doc.toString());
}
}),
EditorView.theme({
'&': { height: '100%', fontSize: '14px' },
'.cm-scroller': { overflow: 'auto', padding: '8px 0' },
'.cm-content': { padding: '0 4px' },
}),
],
}),
parent: host,
});
});
}
ngOnDestroy(): void {
this.destroyEditor();
}
private destroyEditor(): void {
this.view?.destroy();
this.view = null;
}
/** Emit full serialized content from structured blocks */
private emitFromBlocks(blocks: ContentBlock[]): void {
this.contentBlocks.set(blocks);
const content = serializeContentBlocks(blocks);
this.suppressUpdate = true;
this.suppressBlockParse = true;
this.contentChange.emit(content);
}
protected onTableChange(blockIndex: number, table: MarkdownTable): void {
const blocks = this.contentBlocks().map((b, i) =>
i === blockIndex ? { ...b, table } : b,
);
this.emitFromBlocks(blocks);
}
protected onTextBlockChange(blockIndex: number, text: string): void {
const blocks = this.contentBlocks().map((b, i) =>
i === blockIndex ? { ...b, text } : b,
);
this.emitFromBlocks(blocks);
}
protected onHeadingChange(value: string): void {
this.headingValue.set(value);
this.headingChange.emit(value);
}
protected goPrev(): void {
const idx = this.sectionIndex();
if (idx > 0) {
this.navigate.emit(idx - 1);
} else {
this.navigate.emit(-1);
}
}
protected goNext(): void {
const idx = this.sectionIndex();
if (idx < this.totalSections() - 1) {
this.navigate.emit(idx + 1);
}
}
protected get hasPrev(): boolean {
return this.sectionIndex() >= 0;
}
protected get hasNext(): boolean {
return this.sectionIndex() < this.totalSections() - 1;
}
}