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
|
import { makeAutoObservable } from 'mobx';
import { Col } from './DocCreatorMenu';
import { TemplateFieldType, TemplateLayouts } from './TemplateBackend';
import { DynamicField } from './TemplateFieldTypes/DynamicField';
import { FieldSettings, TemplateField, ViewType } from './TemplateFieldTypes/TemplateField';
import { Conditional } from './Backend/TemplateManager';
import { TemplateDataField } from './TemplateFieldTypes/DataField';
export class Template {
_mainField: DynamicField;
private _dataFields: TemplateDataField[] = [];
/**
* A Template can be created from a description of its fields (FieldSettings) or from a DynamicField
* @param definition definition of template as settings or DynamicField
*/
constructor(definition: FieldSettings | DynamicField) {
makeAutoObservable(this);
this._mainField = definition instanceof DynamicField ? definition : this.setupMainField(definition);
}
get childFields() { return this._mainField?.getSubfields ?? []; } // prettier-ignore
get allFields() { return this._mainField?.getAllSubfields ?? []; } // prettier-ignore
get contentFields() { return this.allFields.filter(field => field.isContentField); } // prettier-ignore
get doc() { return this._mainField?.renderedDoc; } // prettier-ignore
get title() { return this._mainField?.getTitle(); } // prettier-ignore
get descriptionSummary() { return this.contentFields.map(f => `--- Field #${f.getID} (title: ${f.getTitle()}): ${f.getDescription ?? ''} ---`).join(); } // prettier-ignore
get compiledContent() { return this.contentFields.map(f => `--- Field #${f.getID} (title: ${f.getTitle()}): ${f.getContent() ?? ''} ---`).join(); } // prettier-ignore
cleanup = () => {
//dispose each subfields disposers, etc.
};
clone = (withContent: boolean = false) => {
const clone = new Template(this._mainField?.makeClone(undefined, withContent) ?? TemplateLayouts.BasicSettings);
this._dataFields.forEach(field => clone.addDataField(field.title));
return clone;
};
getRenderedDoc = () => this.doc;
getFieldByID = (id: number): TemplateField | undefined => this.allFields.filter(field => field.getID === id)[0];
getFieldByTitle = (title: string) => [...this.allFields, ...this._dataFields].filter(field => field.getTitle() === title)[0];
setupMainField = (templateInfo: FieldSettings) => TemplateField.CreateField(templateInfo, 1, undefined) as DynamicField;
assignColToField = (fieldID: number, col: Col) => {
const field = this.getFieldByID(fieldID);
field?.setContent(col.defaultContent ?? '', col.type === TemplateFieldType.VISUAL ? ViewType.IMG : ViewType.TEXT);
field?.setTitle(col.title);
};
addDataField = (title: string, content?: string) => this._dataFields.push(new TemplateDataField(title, content));
removeDataField = (title: string) => (this._dataFields = this._dataFields.filter(field => field.title !== title));
isValidTemplate = (cols: Col[]) => this.title !== 'template_framework' && this.maxMatches(this.getMatches(cols)) === this.contentFields.length;
applyConditionalLogicToField = (field: TemplateField | TemplateDataField, logic: Record<string, Conditional[]>) => {
if (field instanceof DynamicField) return;
const fieldStatements = logic[field.getTitle()];
const content = field.getContent();
fieldStatements?.forEach(statement => {
if (content === statement.condition) {
if (statement.target === 'Template') {
if (this._mainField.renderedDoc) {
this._mainField.renderedDoc[statement.attribute] = statement.value;
Object.assign(this._mainField.settings.opts, { [statement.attribute]: statement.value });
}
} else {
const targetField = this.getFieldByTitle(statement.target);
if (targetField instanceof TemplateField && targetField.renderedDoc) {
targetField.renderedDoc[statement.attribute] = statement.value;
Object.assign(targetField.settings.opts, { [statement.attribute]: statement.value });
}
}
}
});
};
applyConditionalLogic = (logic: Record<string, Conditional[]>) => {
[...this.allFields, ...this._dataFields].forEach(field => this.applyConditionalLogicToField(field, logic));
return this.getRenderedDoc();
};
setImageAsBackground(url: string, makeTransparent: boolean = false) {
const fieldSettings: FieldSettings = {
tl: [-1, -1],
br: [1, 1],
opts: {},
viewType: ViewType.IMG,
};
const field = TemplateField.CreateField(fieldSettings, Math.random() * 100 + 100, this._mainField);
field.setContent(url);
if (makeTransparent) {
this.allFields.forEach(aField => {
aField.updateDocSetting('backgroundColor', 'transparent');
aField.updateDocSetting('borderWidth', '0');
});
}
this._mainField.makeBackgroundField(field);
}
/**
* This function is just a hack for now to get around weird document icon stuff (specifically it misses the background)
*/
setMatteBackground(makeTransparent: boolean = false) {
if (this._mainField.hasBackground) {
return;
}
const fieldSettings: FieldSettings = {
tl: [-1, -1],
br: [1, 1],
opts: { backgroundColor: String(this._mainField.renderedDoc!.backgroundColor) },
viewType: ViewType.TEXT,
};
const field = TemplateField.CreateField(fieldSettings, Math.random() * 100 + 100, this._mainField);
makeTransparent &&
this.allFields.forEach(aField => {
aField.updateDocSetting('backgroundColor', 'transparent');
aField.updateDocSetting('borderWidth', '0');
});
this._mainField.makeBackgroundField(field);
}
getMatches = (cols: Col[]): number[][] => {
const numFields = this.contentFields.length;
if (cols.length !== numFields) return [];
const matches = Array<number[]>(numFields);
this.contentFields.forEach((field, i) => (matches[i] = field.matches(cols)));
return matches;
};
maxMatches = (matches: number[][]) => {
if (matches.length === 0) return 0;
const fieldsCt = this.contentFields.length;
const used = Array<boolean>(fieldsCt).fill(false);
const mt = Array<number>(fieldsCt).fill(-1);
const augmentingPath = (v: number): boolean => {
if (!used[v]) {
used[v] = true;
for (const to of matches[v]) {
if (mt[to] === -1 || augmentingPath(mt[to])) {
mt[to] = v;
return true;
}
}
}
return false;
};
for (let v = 0; v < fieldsCt; ++v) {
used.fill(false);
augmentingPath(v);
}
let count: number = 0;
for (let i = 0; i < fieldsCt; ++i) {
if (mt[i] !== -1) ++count;
}
return count;
};
}
|