@dosgato/templating 0.0.61 → 0.0.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -110,8 +110,25 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
110
110
  * will be skipped.
111
111
  */
112
112
  renderVariation(extension: string): string;
113
+ /**
114
+ * helper function to print an area's component list, but you can also override this if you
115
+ * need to do something advanced like wrap each component in a div
116
+ */
113
117
  renderComponents(components?: RenderedComponent[] | string, opts?: {
114
118
  hideInheritBars?: boolean;
119
+ editBarOpts?: RenderAreaEditBarOpts;
120
+ }): string;
121
+ /**
122
+ * helper function to print an area and set a minimum or maximum number of components
123
+ */
124
+ renderArea(areaName: string, opts?: {
125
+ min?: number;
126
+ max?: number;
127
+ hideMaxWarning?: boolean;
128
+ maxWarning?: string;
129
+ hideInheritBars?: boolean;
130
+ newBarOpts: NewBarOpts;
131
+ editBarOpts: RenderAreaEditBarOpts;
115
132
  }): string;
116
133
  /**
117
134
  * During rendering, each component should determine the CSS blocks that it needs. This may
@@ -165,13 +182,13 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
165
182
  *
166
183
  * Generally should not be overridden - override newLabel and newClass instead
167
184
  */
168
- newBar(areaName: string, opts?: EditBarOpts): string;
185
+ newBar(areaName: string, opts?: NewBarOpts): string;
169
186
  /**
170
187
  * These functions will be provided by the rendering server to assist in the
171
188
  * rendering process.
172
189
  */
173
190
  static editBar: (path: string, opts: EditBarOpts) => string;
174
- static newBar: (path: string, opts: EditBarOpts) => string;
191
+ static newBar: (path: string, opts: NewBarOpts) => string;
175
192
  constructor(data: DataType, path: string, parent: Component | undefined, editMode: boolean);
176
193
  areas: Map<string, Component<any, any, any>[]>;
177
194
  data: Omit<DataType, 'areas'>;
@@ -205,6 +222,8 @@ export interface SiteInfo {
205
222
  export interface PageRecord<DataType extends PageData = PageData> {
206
223
  id: string;
207
224
  linkId: string;
225
+ createdAt: Date;
226
+ modifiedAt: Date;
208
227
  path: string;
209
228
  data: DataType;
210
229
  site: SiteInfo;
@@ -235,11 +254,21 @@ export interface ContextBase {
235
254
  */
236
255
  headerLevel: number;
237
256
  }
238
- export interface EditBarOpts {
239
- extraClass?: string;
240
- label?: string;
257
+ interface BarOpts {
241
258
  editMode?: boolean;
259
+ label?: string;
260
+ extraClass?: string;
261
+ }
262
+ export interface EditBarOpts extends BarOpts {
242
263
  inheritedFrom?: string;
264
+ disableDelete?: boolean;
265
+ }
266
+ export interface RenderAreaEditBarOpts extends Omit<Omit<EditBarOpts, 'label'>, 'extraClass'> {
267
+ label?: string | ((c: Component) => string);
268
+ extraClass?: string | ((c: Component) => string);
269
+ }
270
+ export interface NewBarOpts extends BarOpts {
271
+ disabled?: boolean;
243
272
  }
244
273
  export interface RenderedComponent<C extends Component = Component> {
245
274
  component: C;
@@ -278,3 +307,4 @@ export declare abstract class Page<DataType extends PageData = any, FetchedType
278
307
  protected passError(e: Error, path: string): void;
279
308
  constructor(page: PageRecord<DataType>, editMode: boolean);
280
309
  }
310
+ export {};
package/dist/component.js CHANGED
@@ -82,12 +82,38 @@ export class Component extends ResourceProvider {
82
82
  renderVariation(extension) {
83
83
  return Array.from(this.renderedAreas.values()).flatMap(ras => ras.map(ra => ra.output)).join('');
84
84
  }
85
- // helper function to help you print an area, but you can also override this if you
86
- // need to do something advanced like wrap each component in a div
85
+ /**
86
+ * helper function to print an area's component list, but you can also override this if you
87
+ * need to do something advanced like wrap each component in a div
88
+ */
87
89
  renderComponents(components = [], opts) {
88
90
  if (!Array.isArray(components))
89
91
  components = this.renderedAreas.get(components) ?? [];
90
- return components.flatMap(c => c.component.inheritedFrom && opts?.hideInheritBars ? [c.output] : [c.component.editBar(), c.output]).join('');
92
+ return components
93
+ .flatMap(c => c.component.inheritedFrom &&
94
+ opts?.hideInheritBars
95
+ ? [c.output]
96
+ : [c.component.editBar({
97
+ ...opts?.editBarOpts,
98
+ label: typeof opts?.editBarOpts?.label === 'function' ? opts.editBarOpts.label(c.component) : opts?.editBarOpts?.label,
99
+ extraClass: typeof opts?.editBarOpts?.extraClass === 'function' ? opts.editBarOpts.extraClass(c.component) : opts?.editBarOpts?.extraClass
100
+ }), c.output]).join('');
101
+ }
102
+ /**
103
+ * helper function to print an area and set a minimum or maximum number of components
104
+ */
105
+ renderArea(areaName, opts) {
106
+ const components = this.renderedAreas.get(areaName) ?? [];
107
+ const ownedComponentCount = components.filter(c => !c.component.inheritedFrom).length;
108
+ let output = this.renderComponents(components, { hideInheritBars: opts?.hideInheritBars, editBarOpts: { ...opts?.editBarOpts, disableDelete: ownedComponentCount <= (opts?.min ?? 0) } });
109
+ if (opts?.max && ownedComponentCount >= opts.max) {
110
+ if (!opts.hideMaxWarning)
111
+ output += this.newBar(areaName, { ...opts.newBarOpts, label: opts.maxWarning ?? 'Maximum Reached', disabled: true });
112
+ }
113
+ else {
114
+ output += this.newBar(areaName, opts?.newBarOpts);
115
+ }
116
+ return output;
91
117
  }
92
118
  /**
93
119
  * During rendering, each component should determine the CSS blocks that it needs. This may
@@ -140,11 +166,12 @@ export class Component extends ResourceProvider {
140
166
  * Generally should not be overridden - override editLabel and editClass instead
141
167
  */
142
168
  editBar(opts = {}) {
143
- opts.label ?? (opts.label = this.editLabel() ?? this.autoLabel);
144
- opts.extraClass ?? (opts.extraClass = this.editClass());
145
- opts.editMode ?? (opts.editMode = this.editMode);
146
- opts.inheritedFrom ?? (opts.inheritedFrom = this.inheritedFrom);
147
- return Component.editBar(this.path, opts);
169
+ const options = { ...opts };
170
+ options.label ?? (options.label = this.editLabel() ?? this.autoLabel);
171
+ options.extraClass ?? (options.extraClass = this.editClass());
172
+ options.editMode ?? (options.editMode = this.editMode);
173
+ options.inheritedFrom ?? (options.inheritedFrom = this.inheritedFrom);
174
+ return Component.editBar(this.path, options);
148
175
  }
149
176
  /**
150
177
  * Components may override this function to provide a custom new bar
@@ -155,7 +182,6 @@ export class Component extends ResourceProvider {
155
182
  opts.label ?? (opts.label = this.newLabel(areaName) ?? (this.areas.size > 1 ? `Add ${areaName} Content` : `Add ${this.autoLabel} Content`));
156
183
  opts.extraClass ?? (opts.extraClass = this.newClass(areaName));
157
184
  opts.editMode ?? (opts.editMode = this.editMode);
158
- opts.inheritedFrom ?? (opts.inheritedFrom = this.inheritedFrom);
159
185
  return Component.newBar([this.path, 'areas', areaName].filter(isNotBlank).join('.'), opts);
160
186
  }
161
187
  /**
@@ -1,11 +1,16 @@
1
- import { IconifyIcon } from '@iconify/svelte';
2
- import { SvelteComponent } from 'svelte';
1
+ import type { IconifyIcon } from '@iconify/svelte';
2
+ export interface SvelteComponent {
3
+ $set: (props?: Record<string, any>) => void;
4
+ $on: (event: string, callback: (event: any) => void) => () => void;
5
+ $destroy: () => void;
6
+ [accessor: string]: any;
7
+ }
3
8
  export interface IconOrSVG extends IconifyIcon {
4
9
  raw?: true;
5
10
  }
6
11
  export interface UITemplate {
7
12
  templateKey: string;
8
- dialog: typeof SvelteComponent;
13
+ dialog: SvelteComponent;
9
14
  preview?: IconOrSVG;
10
15
  icon?: IconOrSVG;
11
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dosgato/templating",
3
- "version": "0.0.61",
3
+ "version": "0.0.63",
4
4
  "description": "A library to support building templates for dosgato CMS.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -10,16 +10,16 @@
10
10
  "types": "dist/index.d.ts",
11
11
  "scripts": {
12
12
  "prepublishOnly": "npm run build",
13
+ "lint": "eslint --ignore-path .gitignore .",
13
14
  "build": "rm -rf dist && tsc"
14
15
  },
15
16
  "dependencies": {
16
- "@iconify/svelte": "^2.2.1",
17
- "svelte": "^3.48.0",
17
+ "@iconify/svelte": "^3.0.0",
18
18
  "txstate-utils": "^1.7.4"
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/node": "^18.7.11",
22
- "eslint-config-standard-with-typescript": "^22.0.0",
22
+ "eslint-config-standard-with-typescript": "^23.0.0",
23
23
  "typescript": "^4.4.2"
24
24
  },
25
25
  "repository": {