|
| 1 | +import { AbstractViewField } from '../AbstractViewField'; |
| 2 | +import { type ViewFieldMDRC, type ViewFieldVariable } from '../../renderChildren/ViewFieldMDRC'; |
| 3 | +import { type ViewFieldDeclaration } from '../../parsers/viewFieldParser/ViewFieldDeclaration'; |
| 4 | +import { Signal } from '../../utils/Signal'; |
| 5 | +import { getUUID } from '../../utils/Utils'; |
| 6 | +import { ErrorLevel, MetaBindExpressionError, MetaBindValidationError } from '../../utils/errors/MetaBindErrors'; |
| 7 | +import { type BindTargetDeclaration } from '../../parsers/inputFieldParser/InputFieldDeclaration'; |
| 8 | +import { isMdLink, parseMdLinkList } from '../../parsers/MarkdownLinkParser'; |
| 9 | +import LinkListComponent from '../../utils/LinkListComponent.svelte'; |
| 10 | + |
| 11 | +export class LinkVF extends AbstractViewField { |
| 12 | + component?: LinkListComponent; |
| 13 | + |
| 14 | + constructor(renderChild: ViewFieldMDRC) { |
| 15 | + super(renderChild); |
| 16 | + } |
| 17 | + |
| 18 | + public buildVariables(declaration: ViewFieldDeclaration): ViewFieldVariable[] { |
| 19 | + // filter out empty strings |
| 20 | + const entries: (string | BindTargetDeclaration)[] = declaration.templateDeclaration.filter(x => (typeof x === 'string' ? x : true)); |
| 21 | + |
| 22 | + if (entries.length !== 1) { |
| 23 | + throw new MetaBindValidationError({ |
| 24 | + errorLevel: ErrorLevel.ERROR, |
| 25 | + effect: 'can not create view field', |
| 26 | + cause: 'link view filed only supports exactly a single bind target and not text content', |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + const firstEntry = entries[0]; |
| 31 | + if (typeof firstEntry === 'string') { |
| 32 | + throw new MetaBindValidationError({ |
| 33 | + errorLevel: ErrorLevel.ERROR, |
| 34 | + effect: 'can not create view field', |
| 35 | + cause: 'link view filed only supports exactly a single bind target and not text content', |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + const variable: ViewFieldVariable = { |
| 40 | + bindTargetDeclaration: firstEntry, |
| 41 | + inputSignal: new Signal<unknown>(undefined), |
| 42 | + uuid: getUUID(), |
| 43 | + contextName: `MB_VAR_0`, |
| 44 | + }; |
| 45 | + |
| 46 | + return [variable]; |
| 47 | + } |
| 48 | + |
| 49 | + protected _render(container: HTMLElement): void { |
| 50 | + this.component = new LinkListComponent({ |
| 51 | + target: container, |
| 52 | + props: { |
| 53 | + mdLinkList: [], |
| 54 | + }, |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + protected async _update(container: HTMLElement, text: string): Promise<void> { |
| 59 | + const linkList = parseMdLinkList(text); |
| 60 | + this.component = new LinkListComponent({ |
| 61 | + target: container, |
| 62 | + props: { |
| 63 | + mdLinkList: linkList, |
| 64 | + }, |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public destroy(): void { |
| 69 | + this.component?.$destroy(); |
| 70 | + } |
| 71 | + |
| 72 | + computeValue(variables: ViewFieldVariable[]): string { |
| 73 | + if (variables.length !== 1) { |
| 74 | + throw new MetaBindExpressionError({ |
| 75 | + errorLevel: ErrorLevel.CRITICAL, |
| 76 | + effect: 'failed to evaluate link view field', |
| 77 | + cause: 'there should be exactly one variable', |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + const variable = variables[0]; |
| 82 | + const content = variable.inputSignal.get(); |
| 83 | + |
| 84 | + if (typeof content === 'string') { |
| 85 | + return this.convertToLink(content); |
| 86 | + } else if (Array.isArray(content)) { |
| 87 | + return (content.filter(x => typeof x === 'string') as string[]).map(x => this.convertToLink(x)).join(', '); |
| 88 | + } else { |
| 89 | + return ''; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + convertToLink(str: string): string { |
| 94 | + if (isMdLink(str)) { |
| 95 | + return str; |
| 96 | + } else { |
| 97 | + return `[[${str}]]`; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public getDefaultDisplayValue(): string { |
| 102 | + return ''; |
| 103 | + } |
| 104 | +} |
0 commit comments