@dosgato/templating 0.0.36 → 0.0.39

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.
@@ -192,11 +192,11 @@ export declare type GraphQLQueryFn = <T>(query: string, variables?: any) => Prom
192
192
  * This function is used by API template definitions to help them identify links inside large blocks
193
193
  * of text and return them for indexing.
194
194
  */
195
- export declare function extractLinksFromText(text: string): LinkDefinition[];
195
+ export declare function extractLinksFromText(text: string | undefined): LinkDefinition[];
196
196
  /**
197
197
  * This function is used by API template definitions to help them identify all the searchable
198
198
  * words in a large block of text and return them for indexing.
199
199
  */
200
- export declare function getKeywords(text: string, options?: {
200
+ export declare function getKeywords(text?: string, options?: {
201
201
  stopwords?: boolean;
202
202
  }): string[];
@@ -10,6 +10,8 @@ export var ValidationMessageType;
10
10
  * of text and return them for indexing.
11
11
  */
12
12
  export function extractLinksFromText(text) {
13
+ if (!text)
14
+ return [];
13
15
  const matches = text.matchAll(/{.*"type"\s?:\s+"\w+".*?}/gi);
14
16
  return Array.from(matches).map(m => JSON.parse(m[0]));
15
17
  }
@@ -18,6 +20,8 @@ export function extractLinksFromText(text) {
18
20
  * words in a large block of text and return them for indexing.
19
21
  */
20
22
  export function getKeywords(text, options) {
23
+ if (!text)
24
+ return [];
21
25
  return Array.from(new Set(text
22
26
  .toLocaleLowerCase()
23
27
  .normalize('NFD').replace(/\p{Diacritic}/gu, '')
@@ -19,6 +19,12 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
19
19
  parent?: Component;
20
20
  page?: Page;
21
21
  hadError: boolean;
22
+ /**
23
+ * This property will be set during page render and you may refer to it at any time to
24
+ * determine whether you are doing your work in edit mode or regular rendering mode.
25
+ * The editBar and newBar methods will automatically use it to blank out the editing UI.
26
+ */
27
+ editMode: boolean;
22
28
  /**
23
29
  * The rendering server will provide an instance of the APIClient interface so that
24
30
  * you can run any API GraphQL query you like in your `fetch` function. There are also
@@ -63,7 +69,7 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
63
69
  * you may add them to your this.areas map, e.g.
64
70
  * `this.areas.get('myarea').push(new Component(inheritedData, this.path + '/myarea/inherit1', this))`
65
71
  */
66
- fetch(editMode: boolean): Promise<FetchedType>;
72
+ fetch(): Promise<FetchedType>;
67
73
  /**
68
74
  * The second phase of rendering a component is the context phase. This step is TOP-DOWN and
69
75
  * NON-MUTATING. Each component will receive the parent component's context and then pass a
@@ -79,14 +85,14 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
79
85
  * the context received from the parent, but use it sparingly since it will stall the process.
80
86
  * Try to do all asynchronous work in the fetch phase.
81
87
  */
82
- setContext(renderCtxFromParent: RenderContextType, editMode: boolean): RenderContextType | Promise<RenderContextType>;
88
+ setContext(renderCtxFromParent: RenderContextType): RenderContextType | Promise<RenderContextType>;
83
89
  /**
84
90
  * The final phase of rendering a component is the render phase. This step is BOTTOM-UP -
85
91
  * components at the bottom of the hierarchy will be rendered first, and the result of the
86
92
  * render will be passed to parent components so that the HTML can be included during the
87
93
  * render of the parent component.
88
94
  */
89
- abstract render(renderedAreas: Map<string, string[]>, editMode: boolean): string;
95
+ abstract render(renderedAreas: Map<string, string[]>): string;
90
96
  /**
91
97
  * Sometimes pages are requested with an alternate extension like .rss or .ics. In these
92
98
  * situations, each component should consider whether it should output anything. For
@@ -100,7 +106,7 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
100
106
  * will be skipped.
101
107
  */
102
108
  renderVariation(extension: string, renderedAreas: Map<string, string>): string;
103
- constructor(data: DataType, path: string, parent: Component | undefined);
109
+ constructor(data: DataType, path: string, parent: Component | undefined, editMode: boolean);
104
110
  /**
105
111
  * For logging errors during rendering without crashing the render. If your fetch, setContext,
106
112
  * render, or renderVariation functions throw, the error will be logged but the page render will
@@ -198,5 +204,5 @@ export declare abstract class Page<DataType extends PageData = any, FetchedType
198
204
  */
199
205
  headContent: string;
200
206
  protected passError(e: Error, path: string): void;
201
- constructor(page: PageRecord<DataType>);
207
+ constructor(page: PageRecord<DataType>, editMode: boolean);
202
208
  }
package/dist/component.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { isNotBlank } from 'txstate-utils';
1
2
  import { editBar, newBar } from './editbar.js';
2
3
  import { ResourceProvider } from './provider.js';
3
4
  /**
@@ -10,11 +11,12 @@ import { ResourceProvider } from './provider.js';
10
11
  export class Component extends ResourceProvider {
11
12
  // the constructor is part of the recursive hydration mechanism: constructing
12
13
  // a Component will also construct/hydrate all its child components
13
- constructor(data, path, parent) {
14
+ constructor(data, path, parent, editMode) {
14
15
  super();
15
16
  // properties for use during hydration, you do not have to provide these when
16
17
  // building a template, but you can use them in the functions you do provide
17
18
  this.areas = new Map();
19
+ this.editMode = editMode;
18
20
  this.parent = parent;
19
21
  const { areas, ...ownData } = data;
20
22
  this.data = ownData;
@@ -41,7 +43,7 @@ export class Component extends ResourceProvider {
41
43
  * you may add them to your this.areas map, e.g.
42
44
  * `this.areas.get('myarea').push(new Component(inheritedData, this.path + '/myarea/inherit1', this))`
43
45
  */
44
- async fetch(editMode) {
46
+ async fetch() {
45
47
  return undefined;
46
48
  }
47
49
  /**
@@ -59,7 +61,7 @@ export class Component extends ResourceProvider {
59
61
  * the context received from the parent, but use it sparingly since it will stall the process.
60
62
  * Try to do all asynchronous work in the fetch phase.
61
63
  */
62
- setContext(renderCtxFromParent, editMode) {
64
+ setContext(renderCtxFromParent) {
63
65
  return renderCtxFromParent;
64
66
  }
65
67
  /**
@@ -150,6 +152,7 @@ export class Component extends ResourceProvider {
150
152
  editBar(opts = {}) {
151
153
  opts.label ?? (opts.label = this.editLabel());
152
154
  opts.extraClass ?? (opts.extraClass = this.editClass());
155
+ opts.editMode = this.editMode;
153
156
  return editBar(this.path, opts);
154
157
  }
155
158
  /**
@@ -160,12 +163,13 @@ export class Component extends ResourceProvider {
160
163
  newBar(areaName, opts = {}) {
161
164
  opts.label ?? (opts.label = this.newLabel(areaName));
162
165
  opts.extraClass ?? (opts.extraClass = this.newClass(areaName));
163
- return newBar(this.path + '.' + areaName, opts);
166
+ opts.editMode = this.editMode;
167
+ return newBar([this.path, 'areas', areaName].filter(isNotBlank).join('.'), opts);
164
168
  }
165
169
  }
166
170
  export class Page extends Component {
167
- constructor(page) {
168
- super(page.data, '/', undefined);
171
+ constructor(page, editMode) {
172
+ super(page.data, '', undefined, editMode);
169
173
  this.pagePath = page.path;
170
174
  }
171
175
  passError(e, path) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dosgato/templating",
3
- "version": "0.0.36",
3
+ "version": "0.0.39",
4
4
  "description": "A library to support building templates for dosgato CMS.",
5
5
  "type": "module",
6
6
  "exports": {