@dosgato/templating 0.0.22 → 0.0.23

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.
@@ -1,6 +1,6 @@
1
1
  import { EditBarOpts } from './editbar.js';
2
- import { LinkDefinition } from './links.js';
3
2
  import { ResourceProvider } from './provider.js';
3
+ import { APIClient } from './render.js';
4
4
  /**
5
5
  * This is the primary templating class to build your templates. Subclass it and provide
6
6
  * at least a render function.
@@ -20,28 +20,48 @@ export declare abstract class Component<DataType extends ComponentData = any, Fe
20
20
  page?: Page;
21
21
  hadError: boolean;
22
22
  /**
23
- * This function will be provided by the rendering server and should be used inside your fetch,
24
- * method to convert a link, as input by a user, into a URL suitable for an href, or optionally
25
- * an absolute URL suitable for a backend http request or non-HTML document like an RSS feed.
23
+ * The rendering server will provide an instance of the APIClient interface so that
24
+ * you can run any API GraphQL query you like in your `fetch` function. There are also
25
+ * some useful methods there like processRich to help you convert links in rich text
26
+ * strings.
27
+ *
28
+ * Do NOT mutate data received from the API as it may be cached and given to other
29
+ * Component instances that run the same type of query.
26
30
  */
27
- resolveLink: (link: string | LinkDefinition, absolute?: boolean) => Promise<string>;
31
+ api: APIClient;
28
32
  /**
29
- * This function will be provided by the rendering server and should be used inside your fetch
30
- * method to prepare editor-provided HTML for rendering. It will do things like find and resolve
31
- * link definitions in the internal dosgato format and clean up tags that were accidentally left
32
- * open to protect overall page integrity.
33
+ * Retrieve the data for the root page of the page this component is on. Useful for
34
+ * implementing inheritance schemes.
35
+ *
36
+ * This function will be provided by the rendering service.
37
+ *
38
+ * Do NOT mutate the data returned by this function, as it may be cached and given to
39
+ * other Component instances.
33
40
  */
34
- processRich: (text: string) => Promise<string>;
41
+ getRootPageData: () => Promise<PageData>;
42
+ /**
43
+ * Retrieve the data for all ancestor pages of the page this component is on. Useful
44
+ * for implementing inheritance schemes.
45
+ *
46
+ * This function will be provided by the rendering service.
47
+ *
48
+ * Do NOT mutate the data returned by this function, as it may be cached and given to
49
+ * other Component instances.
50
+ */
51
+ getAncestorPageData: () => Promise<PageData[]>;
35
52
  /**
36
53
  * The first phase of rendering a component is the fetch phase. Each component may
37
54
  * provide a fetch method that looks up data it needs from external sources. This step
38
55
  * is FLAT - it will be executed concurrently for all the components on the page for
39
56
  * maximum speed.
40
57
  *
41
- * Note that this.page will be available, along with its ancestors property containing
42
- * all the data from ancestor pages, in case there is a need for inheritance. It is
43
- * recommended to copy any needed data into the return object, as future phases will not
44
- * want to resolve the inheritance again.
58
+ * Place any needed data into the return object, and it will be available to you as `this.fetched`
59
+ * during the rendering phase.
60
+ *
61
+ * Note that this.page will be available, and getRootPageData and getAncestorPageData are
62
+ * available in case there is a need for inheritance. If you need to inherit entire components,
63
+ * you may add them to your this.areas map, e.g.
64
+ * `this.areas.get('myarea').push(new Component(inheritedData, this.path + '/myarea/inherit1', this))`
45
65
  */
46
66
  fetch(editMode: boolean): Promise<FetchedType>;
47
67
  /**
@@ -156,6 +176,10 @@ export interface ComponentData {
156
176
  export interface PageData extends ComponentData {
157
177
  savedAtVersion: Date;
158
178
  }
179
+ export interface DataData {
180
+ templateKey: string;
181
+ savedAtVersion: Date;
182
+ }
159
183
  export interface ContextBase {
160
184
  /**
161
185
  * For accessibility, every component should consider whether it is creating headers
package/dist/component.js CHANGED
@@ -33,10 +33,13 @@ export class Component extends ResourceProvider {
33
33
  * is FLAT - it will be executed concurrently for all the components on the page for
34
34
  * maximum speed.
35
35
  *
36
- * Note that this.page will be available, along with its ancestors property containing
37
- * all the data from ancestor pages, in case there is a need for inheritance. It is
38
- * recommended to copy any needed data into the return object, as future phases will not
39
- * want to resolve the inheritance again.
36
+ * Place any needed data into the return object, and it will be available to you as `this.fetched`
37
+ * during the rendering phase.
38
+ *
39
+ * Note that this.page will be available, and getRootPageData and getAncestorPageData are
40
+ * available in case there is a need for inheritance. If you need to inherit entire components,
41
+ * you may add them to your this.areas map, e.g.
42
+ * `this.areas.get('myarea').push(new Component(inheritedData, this.path + '/myarea/inherit1', this))`
40
43
  */
41
44
  async fetch(editMode) {
42
45
  return undefined;
package/dist/render.d.ts CHANGED
@@ -1,5 +1,83 @@
1
- import { ContextBase } from './component.js';
1
+ import { ContextBase, DataData, PageData } from './component.js';
2
+ import { AssetLink, DataFolderLink, DataLink, LinkDefinition } from './links.js';
2
3
  export declare function printHeader(ctx: ContextBase, content: string): string;
3
4
  export declare function advanceHeader(ctx: ContextBase, content?: string): {
4
5
  headerLevel: number;
5
6
  };
7
+ export interface PictureResize {
8
+ /** the width of this particular resize */
9
+ width: number;
10
+ /** the URL to this particular resize, relative or absolute depends on options used */
11
+ src: string;
12
+ }
13
+ export interface PictureAttributes {
14
+ /** string appropriate for the src attribute of the default <img> tag */
15
+ src: string;
16
+ /** string appropriate for the srcset attribute of the default <img> tag, or use widths array to reconstruct */
17
+ srcset: string;
18
+ /** a list of available widths in case you want to filter some out and recreate the srcset */
19
+ widths: PictureResize[];
20
+ /** alternative text stored with the image in its asset repository, may be overridden by local alt text */
21
+ alt?: string;
22
+ /** the original intrinsic width of the image uploaded by the editor */
23
+ width: number;
24
+ /** the original intrinsic height of the image uploaded by the editor */
25
+ height: number;
26
+ /** a list of alternate formats like AVIF or WEBP and their resizes, useful for creating <source> tags */
27
+ alternates: {
28
+ /** the mime type of this alternate source, useful for the type attribute on a <source> tag */
29
+ mime: string;
30
+ /** the full srcset for the <source> tag, or use widths array to reconstruct */
31
+ srcset: string;
32
+ /** a list of available widths in case you want to filter some out and recreate the srcset */
33
+ widths: PictureResize[];
34
+ }[];
35
+ }
36
+ export interface APIClient {
37
+ /**
38
+ * Run any query against the API.
39
+ *
40
+ * Will be authenticated as appropriate - anonymous during published renders, as the editor
41
+ * during preview renders.
42
+ */
43
+ query: <T = any>(query: string, variables?: any) => Promise<T>;
44
+ /**
45
+ * This function will be provided by the rendering server and should be used inside your fetch,
46
+ * method to convert a link, as input by a user, into a URL suitable for an href, or optionally
47
+ * an absolute URL suitable for a backend http request or non-HTML document like an RSS feed.
48
+ */
49
+ resolveLink: (link: string | LinkDefinition, absolute?: boolean) => Promise<string>;
50
+ /**
51
+ * This function will be provided by the rendering server and should be used inside your fetch
52
+ * method to prepare editor-provided HTML for rendering. It will do things like find and resolve
53
+ * link definitions in the internal dosgato format and clean up tags that were accidentally left
54
+ * open to protect overall page integrity.
55
+ */
56
+ processRich: (text: string) => Promise<string>;
57
+ /**
58
+ * This function will retrieve information about an image to help you construct responsive HTML
59
+ * for a <picture> element including the <img> and all <source> tags.
60
+ *
61
+ * The alt text it returns will be the default alternative text from the asset repository. Alt
62
+ * text gathered from a template's dialog should generally take precedence (though the dialog may
63
+ * preload the alt text field with the asset repository default).
64
+ */
65
+ getImgAttributes: (link: string | AssetLink, absolute?: boolean) => Promise<PictureAttributes>;
66
+ /** Get the data for a specific page. Will be dataloaded. */
67
+ getPageData: ({ id, path }: {
68
+ id?: string;
69
+ path?: string;
70
+ }) => Promise<PageData>;
71
+ /**
72
+ * Get data items
73
+ *
74
+ * Returns an array in case link is a DataFolderLink. If link is a DataLink, will return an
75
+ * array with length <= 1.
76
+ */
77
+ getDataByLink: (link: string | DataLink | DataFolderLink) => Promise<DataData[]>;
78
+ /**
79
+ * Get data by full path including site. Use '/global' for global data. If path refers
80
+ * to a specific data item, will return an array with length <= 1.
81
+ */
82
+ getDataByPath: (templateKey: string, path: string) => Promise<DataData[]>;
83
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dosgato/templating",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "description": "A library to support building templates for dosgato CMS.",
5
5
  "type": "module",
6
6
  "exports": {