Skip to content

Commit c3b5cbc

Browse files
committed
refactor: simplify get-document-format
1 parent a012f27 commit c3b5cbc

10 files changed

Lines changed: 43 additions & 55 deletions

File tree

src/obsidian/commands/helpers/export-document/eject-document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { onPluginError } from 'src/lib/store/on-plugin-error';
22
import { mapDocumentToText } from 'src/obsidian/commands/helpers/export-document/map-document-to-text';
3-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
3+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
44
import { LineageView } from 'src/view/view';
55
import { saveNodeContent } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/save-node-content';
66
import { setViewType } from 'src/stores/settings/actions/set-view-type';
@@ -22,7 +22,7 @@ export const ejectDocument = async (view: LineageView) => {
2222
return;
2323
}
2424
const fileData = await view.plugin.app.vault.read(file);
25-
const format = getDocumentFormat(view);
25+
const format = getPersistedDocumentFormat(view);
2626
const text = mapDocumentToText(fileData, format);
2727
await view.plugin.app.vault.modify(file, text);
2828
toggleObsidianViewType(view.plugin, view.leaf, 'markdown');

src/obsidian/commands/helpers/export-document/export-document.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createNewFile } from 'src/obsidian/events/workspace/effects/create-new-
22
import { openFile } from 'src/obsidian/events/workspace/effects/open-file';
33
import { onPluginError } from 'src/lib/store/on-plugin-error';
44
import { mapDocumentToText } from 'src/obsidian/commands/helpers/export-document/map-document-to-text';
5-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
5+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
66
import { LineageView } from 'src/view/view';
77
import { saveNodeContent } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/save-node-content';
88

@@ -22,7 +22,7 @@ export const exportDocument = async (view: LineageView) => {
2222
return;
2323
}
2424
const fileData = await view.plugin.app.vault.read(file);
25-
const format = getDocumentFormat(view);
25+
const format = getPersistedDocumentFormat(view);
2626
const output = mapDocumentToText(fileData, format);
2727
const newFile = await createNewFile(
2828
view.plugin,

src/obsidian/commands/helpers/extract-branch/extract-branch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import invariant from 'tiny-invariant';
66
import { openFileInLineage } from 'src/obsidian/events/workspace/effects/open-file-in-lineage';
77
import { getFileNameOfExtractedBranch } from 'src/obsidian/commands/helpers/extract-branch/helpers/get-file-name-of-extracted-branch/get-file-name-of-extracted-branch';
88
import { onPluginError } from 'src/lib/store/on-plugin-error';
9-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
9+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
1010
import { branchToOutline } from 'src/lib/data-conversion/branch-to-x/branch-to-outline';
1111
import { branchToHtmlElement } from 'src/lib/data-conversion/branch-to-x/branch-to-html-element';
1212
import { saveNodeContent } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/save-node-content';
@@ -34,7 +34,7 @@ export const extractBranch = async (view: LineageView) => {
3434
'copy',
3535
);
3636

37-
const format = getDocumentFormat(view);
37+
const format = getPersistedDocumentFormat(view);
3838
const text =
3939
format === 'outline'
4040
? branchToOutline([branch])

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/obsidian/events/workspace/helpers/get-document-format.ts renamed to src/obsidian/events/workspace/helpers/get-persisted-document-format.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { LineageView } from 'src/view/view';
22
import invariant from 'tiny-invariant';
33
import { LineageDocumentFormat } from 'src/stores/settings/settings-type';
44

5-
export const getDocumentFormat = (view: LineageView): LineageDocumentFormat => {
5+
export const getPersistedDocumentFormat = (
6+
view: LineageView,
7+
fail = true,
8+
): LineageDocumentFormat => {
69
invariant(view.file);
710
const format =
811
view.plugin.settings.getValue().documents[view.file.path]
912
?.documentFormat;
1013

11-
invariant(format);
14+
if (fail) invariant(format);
1215
return format;
1316
};

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/view/actions/context-menu/view-context-menu/show-view-context-menu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
MenuItemObject,
44
renderContextMenu,
55
} from 'src/obsidian/context-menu/render-context-menu';
6-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
6+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
77
import { lang } from 'src/lang/lang';
88
import { setDocumentFormat } from 'src/stores/settings/actions/set-document-format';
99
import { exportDocument } from 'src/obsidian/commands/helpers/export-document/export-document';
@@ -15,7 +15,7 @@ export const showViewContextMenu = (event: MouseEvent, view: LineageView) => {
1515
const file = view.file;
1616
if (!file) return;
1717

18-
const format = getDocumentFormat(view);
18+
const format = getPersistedDocumentFormat(view);
1919
const isOutline = format === 'outline';
2020
const isHtmlElement = format === 'html-element';
2121
const isHtmlComments = format === 'sections';

src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/copy-active-branches-to-clipboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
1+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
22
import { mapBranchesToText } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/map-branches-to-text';
33
import { LineageView } from 'src/view/view';
44
import { getActiveNodes } from 'src/view/actions/keyboard-shortcuts/helpers/commands/commands/helpers/clipboard/get-active-nodes';
@@ -12,7 +12,7 @@ export const copyActiveBranchesToClipboard = async (
1212
const text = mapBranchesToText(
1313
view.documentStore.getValue().document,
1414
nodes,
15-
formatted ? getDocumentFormat(view) : 'unformatted-text',
15+
formatted ? getPersistedDocumentFormat(view) : 'unformatted-text',
1616
);
1717
await navigator.clipboard.writeText(text);
1818
};

src/view/components/container/column/components/group/components/card/components/card-buttons/tree-index-button.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { getView } from '../../../../../../../context';
33
import { ActiveStatus } from 'src/view/components/container/column/components/group/components/active-status.enum';
4-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
4+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
55
import {
66
findSectionPosition
77
} from 'src/view/components/container/column/components/group/components/card/components/card-buttons/helpers/find-section-position';
@@ -27,7 +27,7 @@
2727
const openFile = async () => {
2828
if (!view.file) return;
2929
30-
const format = getDocumentFormat(view);
30+
const format = getPersistedDocumentFormat(view);
3131
const i =
3232
format === 'sections'
3333
? findSectionPosition(view, nodeId)

src/view/view.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ import invariant from 'tiny-invariant';
3131
import { customIcons } from 'src/helpers/load-custom-icons';
3232

3333
import { setViewType } from 'src/stores/settings/actions/set-view-type';
34-
import { getDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-document-format';
34+
import { getPersistedDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-persisted-document-format';
3535
import { stringifyDocument } from 'src/view/helpers/stringify-document';
36-
import { getOrDetectDocumentFormat } from 'src/obsidian/events/workspace/helpers/get-or-detect-document-format';
37-
import { maybeGetDocumentFormat } from 'src/obsidian/events/workspace/helpers/maybe-get-document-format';
3836
import { setDocumentFormat } from 'src/stores/settings/actions/set-document-format';
3937
import { toggleObsidianViewType } from 'src/obsidian/events/workspace/effects/toggle-obsidian-view-type';
4038
import { DocumentSearch } from 'src/stores/view/subscriptions/effects/document-search/document-search';
@@ -50,6 +48,8 @@ import { DebouncedMinimapEffects } from 'src/stores/minimap/subscriptions/effect
5048
import { updateFrontmatter } from 'src/stores/view/subscriptions/actions/document/update-frontmatter';
5149
import { loadFullDocument } from 'src/stores/view/subscriptions/actions/document/load-full-document';
5250
import { refreshActiveViewOfDocument } from 'src/stores/plugin/actions/refresh-active-view-of-document';
51+
import { detectDocumentFormat } from 'src/lib/format-detection/detect-document-format';
52+
import { LineageDocumentFormat } from 'src/stores/settings/settings-type';
5353

5454
export const LINEAGE_VIEW_TYPE = 'lineage';
5555

@@ -191,7 +191,7 @@ export class LineageView extends TextFileView {
191191
const state = clone(this.documentStore.getValue());
192192
const data: string =
193193
state.file.frontmatter +
194-
stringifyDocument(state.document, getDocumentFormat(this));
194+
stringifyDocument(state.document, getPersistedDocumentFormat(this));
195195
if (data !== this.data) {
196196
if (data.trim().length === 0) {
197197
throw new Error(lang.error_save_empty_data);
@@ -211,7 +211,7 @@ export class LineageView extends TextFileView {
211211
} else {
212212
this.createStore();
213213
}
214-
this.loadDocumentToStore();
214+
this.loadDocumentToStore('view-mount');
215215
if (!this.inlineEditor) {
216216
this.inlineEditor = new InlineEditor(this);
217217
await this.inlineEditor.onload();
@@ -250,13 +250,13 @@ export class LineageView extends TextFileView {
250250
].documentStore;
251251
};
252252

253-
private loadDocumentToStore = () => {
253+
private loadDocumentToStore = (event?: 'view-mount') => {
254254
const { body, frontmatter } = extractFrontmatter(this.data);
255255

256256
const documentState = this.documentStore.getValue();
257257
const viewState = this.viewStore.getValue();
258-
const isEmptyStore = documentState.history.items.length === 0;
259-
const format = getOrDetectDocumentFormat(this, body);
258+
const format = this.getDocumentFormat(body);
259+
const emptyStore = documentState.history.items.length === 0;
260260
const existingBody = stringifyDocument(documentState.document, format);
261261

262262
const bodyHasChanged = existingBody !== body;
@@ -269,19 +269,31 @@ export class LineageView extends TextFileView {
269269
const activeSection = activeNode
270270
? documentState.sections.id_section[activeNode]
271271
: null;
272-
if (bodyHasChanged && !isEditing) {
272+
if (emptyStore || (bodyHasChanged && !isEditing)) {
273273
loadFullDocument(this, body, frontmatter, format, activeSection);
274-
if (this.isActive && existingBody && !isEmptyStore) {
275-
new Notice('Document changed externally');
274+
if (this.isActive && event !== 'view-mount') {
275+
new Notice('Document updated externally');
276276
}
277277
} else if (frontmatterHasChanged) {
278278
updateFrontmatter(this, frontmatter);
279279
}
280-
if (isEmptyStore && !maybeGetDocumentFormat(this)) {
281-
setDocumentFormat(this.plugin, this.file!.path, format);
282-
}
283280
};
284281

282+
private getDocumentFormat(body: string) {
283+
let format: LineageDocumentFormat;
284+
format = getPersistedDocumentFormat(this, false);
285+
if (format) {
286+
return format;
287+
}
288+
289+
format =
290+
detectDocumentFormat(body) ||
291+
this.plugin.settings.getValue().general.defaultDocumentFormat;
292+
293+
setDocumentFormat(this.plugin, this.file!.path, format);
294+
return format;
295+
}
296+
285297
private debouncedLoadDocumentToStore = debounce(
286298
this.loadDocumentToStore,
287299
250,

0 commit comments

Comments
 (0)