Skip to content

Commit efe45c5

Browse files
committed
finish metadata manager modularization
1 parent bbcb381 commit efe45c5

File tree

11 files changed

+308
-110
lines changed

11 files changed

+308
-110
lines changed

exampleVault/Advanced Examples/Activity Tracker.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
---
22
activities:
3-
- {}
4-
- {}
3+
- from: 03:00
4+
to: 04:17
5+
activity: sudying
6+
status: 0
57
---
68

79
```js-engine

exampleVault/Examples.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
slider1: 7
2+
slider1: 9
33
suggest: test
44
toggle1: false
55
Domestic_tasks:
@@ -13,7 +13,7 @@ inlineSelect: 0
1313
nested:
1414
object: test
1515
number1: 2
16-
number2: 14
16+
number2: 18
1717
---
1818

1919
## Fields Work Everywhere

src/main.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type MarkdownPostProcessorContext, Plugin, stringifyYaml, TFile, type WorkspaceLeaf } from 'obsidian';
1+
import { type MarkdownPostProcessorContext, Plugin, stringifyYaml, type WorkspaceLeaf } from 'obsidian';
22
import { MetaBindSettingTab } from './settings/SettingsTab';
33
import { DateParser } from './parsers/DateParser';
44
import { MetadataManager } from './metadata/MetadataManager';
@@ -21,7 +21,7 @@ import { DependencyManager } from './utils/dependencies/DependencyManager';
2121
import { Version } from './utils/dependencies/Version';
2222
import { createEditorMenu } from './EditorMenu';
2323
import { BindTargetStorageType } from './parsers/bindTargetParser/BindTargetDeclaration';
24-
import { GlobalMetadataSource, InternalMetadataSource } from './metadata/InternalMetadataSources';
24+
import { GlobalMetadataSource, InternalMetadataSource, ScopeMetadataSource } from './metadata/InternalMetadataSources';
2525
import { ObsidianMetadataSource } from './metadata/ObsidianMetadataSource';
2626

2727
export enum MetaBindBuild {
@@ -126,22 +126,17 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
126126

127127
setUpMetadataManager(): void {
128128
this.metadataManager = new MetadataManager();
129-
130-
const obsidianMetadataSource = new ObsidianMetadataSource(
131-
this,
132-
BindTargetStorageType.FRONTMATTER,
133-
this.metadataManager,
129+
this.metadataManager.registerSource(
130+
new ObsidianMetadataSource(this, BindTargetStorageType.FRONTMATTER, this.metadataManager),
134131
);
135-
this.metadataManager.registerSource(obsidianMetadataSource);
136-
137-
const memoryMetadataSource = new InternalMetadataSource(BindTargetStorageType.MEMORY, this.metadataManager);
138-
this.metadataManager.registerSource(memoryMetadataSource);
139-
140-
const globalMemoryMetadataSource = new GlobalMetadataSource(
141-
BindTargetStorageType.GLOBAL_MEMORY,
142-
this.metadataManager,
132+
this.metadataManager.registerSource(
133+
new InternalMetadataSource(BindTargetStorageType.MEMORY, this.metadataManager),
143134
);
144-
this.metadataManager.registerSource(globalMemoryMetadataSource);
135+
this.metadataManager.registerSource(
136+
new GlobalMetadataSource(BindTargetStorageType.GLOBAL_MEMORY, this.metadataManager),
137+
);
138+
this.metadataManager.registerSource(new ScopeMetadataSource(BindTargetStorageType.SCOPE, this.metadataManager));
139+
this.metadataManager.setDefaultSource(BindTargetStorageType.FRONTMATTER);
145140

146141
this.registerEvent(
147142
this.app.vault.on('rename', (file, oldPath) => {
@@ -174,9 +169,11 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
174169
const content = codeBlock.innerText;
175170

176171
const mdrcType = InlineMDRCUtils.isDeclarationAndGetMDRCType(content);
172+
177173
if (mdrcType === undefined) {
178174
continue;
179175
}
176+
console.log(content, ctx.getSectionInfo(codeBlock)?.lineStart, ctx.getSectionInfo(codeBlock)?.lineEnd);
180177
InlineMDRCUtils.constructMDRC(mdrcType, content, ctx.sourcePath, codeBlock, ctx, this);
181178
}
182179
}, 1);
@@ -221,6 +218,7 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
221218
}
222219

223220
this.registerMarkdownCodeBlockProcessor('meta-bind-button', (source, el, ctx) => {
221+
console.log(ctx.getSectionInfo(el));
224222
this.api.createButtonFromString(source, ctx.sourcePath, el, ctx);
225223
});
226224
}

src/metadata/InternalMetadataSources.ts

Lines changed: 167 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
1-
import { type IMetadataSource, MapMetadataSource, type Metadata } from './MetadataSource';
2-
import { type GlobalMetadataCacheItem, type MapMetadataCacheItem } from './MetadataCacheItem';
1+
import { type IMetadataSource, FilePathMetadataSource, type Metadata } from './MetadataSource';
2+
import {
3+
type GlobalMetadataCacheItem,
4+
type FilePathMetadataCacheItem,
5+
type IMetadataCacheItem,
6+
} from './MetadataCacheItem';
37
import { type MetadataManager } from './MetadataManager';
48
import { type BindTargetDeclaration } from '../parsers/bindTargetParser/BindTargetDeclaration';
59
import { type PropPath } from '../utils/prop/PropPath';
610
import { type IMetadataSubscription } from './IMetadataSubscription';
711
import { PropUtils } from '../utils/prop/PropUtils';
812
import { ErrorLevel, MetaBindInternalError } from '../utils/errors/MetaBindErrors';
13+
import { type ParsingResultNode } from '../parsers/nomParsers/GeneralNomParsers';
14+
import { type BindTargetParser } from '../parsers/bindTargetParser/BindTargetParser';
15+
import { ParsingValidationError } from '../parsers/ParsingError';
16+
import { type BindTargetScope } from './BindTargetScope';
917

10-
export class InternalMetadataSource extends MapMetadataSource<MapMetadataCacheItem> {
11-
public getDefaultCacheItem(storagePath: string): MapMetadataCacheItem {
18+
export class InternalMetadataSource extends FilePathMetadataSource<FilePathMetadataCacheItem> {
19+
public getDefaultCacheItem(storagePath: string): FilePathMetadataCacheItem {
1220
return {
1321
data: {},
1422
storagePath: storagePath,
1523
...this.manager.getDefaultCacheItem(),
1624
};
1725
}
1826

19-
public async syncExternal(_cacheItem: MapMetadataCacheItem): Promise<void> {
27+
public async syncExternal(_cacheItem: FilePathMetadataCacheItem): Promise<void> {
2028
// Do nothing
2129
}
2230
}
@@ -36,6 +44,32 @@ export class GlobalMetadataSource implements IMetadataSource<GlobalMetadataCache
3644
};
3745
}
3846

47+
public validateStoragePath(
48+
storagePath: ParsingResultNode,
49+
hadStoragePath: boolean,
50+
bindTargetDeclaration: string,
51+
_parser: BindTargetParser,
52+
): string {
53+
if (hadStoragePath) {
54+
throw new ParsingValidationError(
55+
ErrorLevel.ERROR,
56+
'Bind Target Validator',
57+
`Failed to parse bind target. Bind target storage type 'global_memory' does not support a storage path.`,
58+
bindTargetDeclaration,
59+
storagePath.position,
60+
);
61+
}
62+
return '';
63+
}
64+
65+
public resolveBindTargetScope(
66+
bindTargetDeclaration: BindTargetDeclaration,
67+
_scope: BindTargetScope | undefined,
68+
_parser: BindTargetParser,
69+
): BindTargetDeclaration {
70+
return bindTargetDeclaration;
71+
}
72+
3973
public delete(_cacheItem: GlobalMetadataCacheItem): void {
4074
// noop
4175
}
@@ -101,3 +135,131 @@ export class GlobalMetadataSource implements IMetadataSource<GlobalMetadataCache
101135
return cacheItem.data;
102136
}
103137
}
138+
139+
export class ScopeMetadataSource implements IMetadataSource<IMetadataCacheItem> {
140+
public readonly id: string;
141+
public readonly manager: MetadataManager;
142+
143+
constructor(id: string, manager: MetadataManager) {
144+
this.id = id;
145+
this.manager = manager;
146+
}
147+
148+
public validateStoragePath(
149+
storagePath: ParsingResultNode,
150+
hadStoragePath: boolean,
151+
bindTargetDeclaration: string,
152+
_parser: BindTargetParser,
153+
): string {
154+
if (hadStoragePath) {
155+
throw new ParsingValidationError(
156+
ErrorLevel.ERROR,
157+
'Bind Target Validator',
158+
`Failed to parse bind target. Bind target storage type 'scope' does not support a storage path.`,
159+
bindTargetDeclaration,
160+
storagePath.position,
161+
);
162+
}
163+
return '';
164+
}
165+
166+
public resolveBindTargetScope(
167+
bindTargetDeclaration: BindTargetDeclaration,
168+
scope: BindTargetScope | undefined,
169+
parser: BindTargetParser,
170+
): BindTargetDeclaration {
171+
return parser.resolveScope(bindTargetDeclaration, scope);
172+
}
173+
174+
public delete(_cacheItem: IMetadataCacheItem): void {
175+
throw new MetaBindInternalError({
176+
errorLevel: ErrorLevel.CRITICAL,
177+
effect: 'action not permitted',
178+
cause: `source 'scope' should have no cache items or subscriptions`,
179+
});
180+
}
181+
182+
public getCacheItemForStoragePath(_storagePath: string): IMetadataCacheItem | undefined {
183+
throw new MetaBindInternalError({
184+
errorLevel: ErrorLevel.CRITICAL,
185+
effect: 'action not permitted',
186+
cause: `source 'scope' should have no cache items or subscriptions`,
187+
});
188+
}
189+
190+
public iterateCacheItems(): IterableIterator<IMetadataCacheItem> {
191+
return [][Symbol.iterator]();
192+
}
193+
194+
public onCycle(_cacheItem: IMetadataCacheItem): void {
195+
// noop
196+
}
197+
198+
public readCache(_bindTarget: BindTargetDeclaration): unknown {
199+
throw new MetaBindInternalError({
200+
errorLevel: ErrorLevel.CRITICAL,
201+
effect: 'action not permitted',
202+
cause: `source 'scope' should have no cache items or subscriptions`,
203+
});
204+
}
205+
206+
public readCacheItem(_cacheItem: IMetadataCacheItem, _propPath: PropPath): unknown {
207+
throw new MetaBindInternalError({
208+
errorLevel: ErrorLevel.CRITICAL,
209+
effect: 'action not permitted',
210+
cause: `source 'scope' should have no cache items or subscriptions`,
211+
});
212+
}
213+
214+
public shouldDelete(_cacheItem: IMetadataCacheItem): boolean {
215+
throw new MetaBindInternalError({
216+
errorLevel: ErrorLevel.CRITICAL,
217+
effect: 'action not permitted',
218+
cause: `source 'scope' should have no cache items or subscriptions`,
219+
});
220+
}
221+
222+
public subscribe(_subscription: IMetadataSubscription): IMetadataCacheItem {
223+
throw new MetaBindInternalError({
224+
errorLevel: ErrorLevel.CRITICAL,
225+
effect: 'action not permitted',
226+
cause: `source 'scope' should have no cache items or subscriptions`,
227+
});
228+
}
229+
230+
public syncExternal(_cacheItem: IMetadataCacheItem): void {
231+
// noop
232+
}
233+
234+
public unsubscribe(_subscription: IMetadataSubscription): IMetadataCacheItem {
235+
throw new MetaBindInternalError({
236+
errorLevel: ErrorLevel.CRITICAL,
237+
effect: 'action not permitted',
238+
cause: `source 'scope' should have no cache items or subscriptions`,
239+
});
240+
}
241+
242+
public update(_value: unknown, _subscription: IMetadataSubscription): IMetadataCacheItem {
243+
throw new MetaBindInternalError({
244+
errorLevel: ErrorLevel.CRITICAL,
245+
effect: 'action not permitted',
246+
cause: `source 'scope' should have no cache items or subscriptions`,
247+
});
248+
}
249+
250+
public updateEntireCache(_value: Metadata, _cacheItem: IMetadataCacheItem): void {
251+
throw new MetaBindInternalError({
252+
errorLevel: ErrorLevel.CRITICAL,
253+
effect: 'action not permitted',
254+
cause: `source 'scope' should have no cache items or subscriptions`,
255+
});
256+
}
257+
258+
public readEntireCacheItem(_cacheItem: IMetadataCacheItem): Metadata {
259+
throw new MetaBindInternalError({
260+
errorLevel: ErrorLevel.CRITICAL,
261+
effect: 'action not permitted',
262+
cause: `source 'scope' should have no cache items or subscriptions`,
263+
});
264+
}
265+
}

src/metadata/MetadataCacheItem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface IMetadataCacheItem {
2323
inactive: boolean;
2424
}
2525

26-
export interface MapMetadataCacheItem extends IMetadataCacheItem {
26+
export interface FilePathMetadataCacheItem extends IMetadataCacheItem {
2727
storagePath: string;
2828
data: Metadata;
2929
}

src/metadata/MetadataManager.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,15 +676,29 @@ export function bindTargetToString(a: BindTargetDeclaration | undefined): string
676676

677677
export class MetadataManager {
678678
sources: Map<string, MetadataSource>;
679+
defaultSource: string;
679680

680681
constructor() {
681682
this.sources = new Map<string, MetadataSource>();
683+
this.defaultSource = 'CHANGE_THE_DEFAULT_SOURCE';
682684
}
683685

684686
public registerSource(source: MetadataSource): void {
685687
this.sources.set(source.id, source);
686688
}
687689

690+
public setDefaultSource(id: string): void {
691+
if (this.sources.has(id)) {
692+
this.defaultSource = id;
693+
} else {
694+
throw new MetaBindInternalError({
695+
errorLevel: ErrorLevel.CRITICAL,
696+
effect: 'can not set default source',
697+
cause: `Source "${id}" does not exist`,
698+
});
699+
}
700+
}
701+
688702
public unregisterSource(source: MetadataSource): void {
689703
this.sources.delete(source.id);
690704
}
@@ -693,6 +707,10 @@ export class MetadataManager {
693707
return this.sources.get(id);
694708
}
695709

710+
public iterateSources(): IterableIterator<string> {
711+
return this.sources.keys();
712+
}
713+
696714
public subscribe(
697715
uuid: string,
698716
callbackSignal: Signal<unknown>,

0 commit comments

Comments
 (0)