Skip to content

Commit a7cc2b0

Browse files
committed
refactor: simplify view.loadDocument
1 parent 5b36116 commit a7cc2b0

8 files changed

Lines changed: 87 additions & 95 deletions

File tree

src/lib/format-detection/detect-document-format.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { hasNBulletListItems } from 'src/lib/format-detection/has-n-bullet-list-
55
import { hasNHtmlElementMarker } from 'src/lib/format-detection/has-n-html-element-markers';
66

77
export const detectDocumentFormat = (text: string, strict = true) => {
8-
const { data } = extractFrontmatter(text);
8+
const { body } = extractFrontmatter(text);
99

10-
if (hasNHtmlCommentMarker(data, 1)) return 'sections';
11-
if (hasNHtmlElementMarker(data, 1)) return 'html-element';
12-
if (isOutline(data)) return 'outline';
10+
if (hasNHtmlCommentMarker(body, 1)) return 'sections';
11+
if (hasNHtmlElementMarker(body, 1)) return 'html-element';
12+
if (isOutline(body)) return 'outline';
1313
if (!strict) {
1414
if (hasNBulletListItems(text, 1)) return 'outline';
1515
}

src/obsidian/commands/helpers/export-document/map-document-to-text.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ export const mapDocumentToText = (
99
fileData: string,
1010
format: LineageDocumentFormat,
1111
) => {
12-
const { data, frontmatter } = extractFrontmatter(fileData);
12+
const { body, frontmatter } = extractFrontmatter(fileData);
1313
const tree =
1414
format === 'outline'
15-
? outlineToJson(data)
15+
? outlineToJson(body)
1616
: format === 'html-element'
17-
? htmlElementToJson(data)
18-
: htmlCommentToJson(data);
17+
? htmlElementToJson(body)
18+
: htmlCommentToJson(body);
1919
/* if (tree.length < 2 && tree[0].children.length == 0) {
2020
throw new Error(`File ${basename} does not appear to be a tree`);
2121
}*/

src/obsidian/events/workspace/helpers/get-or-detect-document-format.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,18 @@ import { LineageView } from 'src/view/view';
22
import { LineageDocumentFormat } from 'src/stores/settings/settings-type';
33
import { detectDocumentFormat } from 'src/lib/format-detection/detect-document-format';
44
import { maybeGetDocumentFormat } from 'src/obsidian/events/workspace/helpers/maybe-get-document-format';
5-
import { outlineToJson } from 'src/lib/data-conversion/x-to-json/outline-to-json';
65

76
export const getOrDetectDocumentFormat = (
87
view: LineageView,
9-
data: string,
8+
body: string,
109
): LineageDocumentFormat => {
1110
const format = maybeGetDocumentFormat(view);
1211
if (format) {
1312
return format;
1413
}
1514

16-
const detected = detectDocumentFormat(view.data);
15+
const detected = detectDocumentFormat(body);
1716
if (detected) return detected;
1817

19-
const defaultFormat =
20-
view.plugin.settings.getValue().general.defaultDocumentFormat;
21-
if (defaultFormat === 'outline') {
22-
if (!data.trim()) return 'outline';
23-
// has a single item
24-
try {
25-
const tree = outlineToJson(data);
26-
if (tree.length <= 1 && tree[0]?.children?.length === 0)
27-
return 'outline';
28-
} catch {
29-
/* empty */
30-
}
31-
}
32-
return defaultFormat;
18+
return view.plugin.settings.getValue().general.defaultDocumentFormat;
3319
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { LineageDocumentFormat } from 'src/stores/settings/settings-type';
2+
import { LineageView } from 'src/view/view';
3+
4+
export const loadFullDocument = (
5+
view: LineageView,
6+
data: string,
7+
frontmatter: string,
8+
format: LineageDocumentFormat,
9+
activeSection: string | null,
10+
) => {
11+
view.documentStore.dispatch({
12+
payload: {
13+
document: { data: data, frontmatter, position: null },
14+
format,
15+
activeSection,
16+
},
17+
type: 'DOCUMENT/LOAD_FILE',
18+
});
19+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { LineageView } from 'src/view/view';
2+
3+
export const updateFrontmatter = (view: LineageView, frontmatter: string) => {
4+
view.documentStore.dispatch({
5+
type: 'FILE/UPDATE_FRONTMATTER',
6+
payload: {
7+
frontmatter,
8+
},
9+
});
10+
};

src/view/helpers/extract-frontmatter.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { describe, expect, it } from 'vitest';
44
describe('extractFrontmatter', () => {
55
it('should correctly extract frontmatter when it exists', () => {
66
const input = `---\ntitle: Example\ndate: 2024-02-27\n---\n# Content\nThis is the content of the markdown file.\n`;
7-
const { data, frontmatter } = extractFrontmatter(input);
8-
expect(data).toEqual(
7+
const { body, frontmatter } = extractFrontmatter(input);
8+
expect(body).toEqual(
99
'# Content\nThis is the content of the markdown file.\n',
1010
);
1111
expect(frontmatter).toEqual(
@@ -15,23 +15,23 @@ describe('extractFrontmatter', () => {
1515

1616
it('should return empty frontmatter when it does not exist', () => {
1717
const input = `# Content\nThis is the content of the markdown file.\n`;
18-
const { data, frontmatter } = extractFrontmatter(input);
19-
expect(data).toEqual(
18+
const { body, frontmatter } = extractFrontmatter(input);
19+
expect(body).toEqual(
2020
'# Content\nThis is the content of the markdown file.\n',
2121
);
2222
expect(frontmatter).toEqual('');
2323
});
2424

2525
it('should handle empty input', () => {
26-
const { data, frontmatter } = extractFrontmatter('');
27-
expect(data).toEqual('');
26+
const { body, frontmatter } = extractFrontmatter('');
27+
expect(body).toEqual('');
2828
expect(frontmatter).toEqual('');
2929
});
3030

3131
it('should handle input with only frontmatter', () => {
3232
const input = `---\ntitle: Only Frontmatter\n---\n`;
33-
const { data, frontmatter } = extractFrontmatter(input);
34-
expect(data).toEqual('');
33+
const { body, frontmatter } = extractFrontmatter(input);
34+
expect(body).toEqual('');
3535
expect(frontmatter).toEqual('---\ntitle: Only Frontmatter\n---\n');
3636
});
3737
});
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export const extractFrontmatter = (
22
markdown: string,
3-
): { data: string; frontmatter: string } => {
3+
): { body: string; frontmatter: string } => {
44
const frontmatterRegex = /^---\n([\s\S]+?)\n---\n/;
55
const match = markdown.match(frontmatterRegex);
66

77
if (match) {
88
const frontmatter = match[0];
99
const data = markdown.slice(frontmatter.length);
10-
return { data, frontmatter: frontmatter.trim() + '\n' };
10+
return { body: data, frontmatter: frontmatter.trim() + '\n' };
1111
} else {
12-
return { data: markdown, frontmatter: '' };
12+
return { body: markdown, frontmatter: '' };
1313
}
1414
};

src/view/view.ts

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import { AlignBranch } from 'src/stores/view/subscriptions/effects/align-branch/
4848
import { lang } from 'src/lang/lang';
4949
import { logger } from 'src/helpers/logger';
5050
import { DebouncedMinimapEffects } from 'src/stores/minimap/subscriptions/effects/debounced-minimap-effects';
51+
import { updateFrontmatter } from 'src/stores/view/subscriptions/actions/document/update-frontmatter';
52+
import { loadFullDocument } from 'src/stores/view/subscriptions/actions/document/load-full-document';
5153

5254
export const LINEAGE_VIEW_TYPE = 'lineage';
5355

@@ -173,21 +175,6 @@ export class LineageView extends TextFileView {
173175

174176
async onOpen() {}
175177

176-
/*private destroyStore = () => {
177-
const leavesOfType = this.plugin.app.workspace
178-
.getLeavesOfType(FILE_VIEW_TYPE)
179-
.filter(
180-
(l) =>
181-
l.view instanceof LineageView &&
182-
l.view.file?.path === this.activeFilePath &&
183-
l.view !== this,
184-
);
185-
if (leavesOfType.length === 0) {
186-
this.store.dispatch({ type: 'RESET_STORE' });
187-
if (this.file) delete stores[this.file.path];
188-
}
189-
};*/
190-
191178
async onClose() {
192179
return this.onUnloadFile();
193180
}
@@ -286,53 +273,43 @@ export class LineageView extends TextFileView {
286273
};
287274

288275
private loadDocumentToStore = (isInitialLoad = false) => {
289-
const { data, frontmatter } = extractFrontmatter(this.data);
276+
const { body, frontmatter } = extractFrontmatter(this.data);
277+
278+
const documentState = this.documentStore.getValue();
279+
const viewState = this.viewStore.getValue();
280+
const documentFormat = getOrDetectDocumentFormat(this, body);
281+
const existingBody = isInitialLoad
282+
? ''
283+
: stringifyDocument(documentState.document, documentFormat);
290284

291-
const state = this.documentStore.getValue();
292-
const format = getOrDetectDocumentFormat(this, data);
293-
const existingData = stringifyDocument(state.document, format);
294-
const bodyHasChanged = existingData !== data;
285+
const bodyHasChanged = existingBody !== body;
295286
const frontmatterHasChanged =
296-
!bodyHasChanged && frontmatter !== state.file.frontmatter;
297-
if (!existingData || bodyHasChanged || frontmatterHasChanged) {
298-
const isEditing =
299-
this.viewStore.getValue().document.editing.activeNodeId;
300-
if (frontmatterHasChanged && !isInitialLoad) {
301-
this.documentStore.dispatch({
302-
type: 'FILE/UPDATE_FRONTMATTER',
303-
payload: {
304-
frontmatter,
305-
},
306-
});
307-
} else if (!isEditing) {
308-
const activeNode =
309-
this.viewStore.getValue().document.activeNode;
310-
const activeSection = activeNode
311-
? this.documentStore.getValue().sections.id_section[
312-
activeNode
313-
]
314-
: null;
315-
this.documentStore.dispatch({
316-
payload: {
317-
document: { data: data, frontmatter, position: null },
318-
format,
319-
activeSection,
320-
},
321-
type: 'DOCUMENT/LOAD_FILE',
322-
});
323-
if (
324-
this.isActive &&
325-
!isInitialLoad &&
326-
existingData &&
327-
bodyHasChanged
328-
) {
329-
new Notice('Document changed externally');
330-
}
331-
if (!maybeGetDocumentFormat(this)) {
332-
invariant(this.file);
333-
setDocumentFormat(this.plugin, this.file.path, format);
334-
}
287+
frontmatter !== documentState.file.frontmatter;
288+
289+
const isEditing = Boolean(viewState.document.editing.activeNodeId);
290+
291+
if (isInitialLoad) {
292+
loadFullDocument(this, body, frontmatter, documentFormat, null);
293+
if (!maybeGetDocumentFormat(this)) {
294+
setDocumentFormat(this.plugin, this.file!.path, documentFormat);
295+
}
296+
} else if (bodyHasChanged && !isEditing) {
297+
const activeNode = viewState.document.activeNode;
298+
const activeSection = activeNode
299+
? documentState.sections.id_section[activeNode]
300+
: null;
301+
loadFullDocument(
302+
this,
303+
body,
304+
frontmatter,
305+
documentFormat,
306+
activeSection,
307+
);
308+
if (this.isActive && existingBody) {
309+
new Notice('Document changed externally');
335310
}
311+
} else if (frontmatterHasChanged) {
312+
updateFrontmatter(this, frontmatter);
336313
}
337314
};
338315

0 commit comments

Comments
 (0)