@gramlot/gramlot 0.2.18 → 0.2.20

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,7 @@
1
1
  // Attribute rules shared by GramlotHtmlRenderer, GramlotSvgRenderer and the live GramlotRenderer.
2
2
  // Same table and same functions as src/gramlot/renderer/attributes.py (source plan §1 rule 10).
3
3
  import {sourceAttributeItems} from '@genrojs/builders';
4
+ import {applyMask} from '../format.js';
4
5
 
5
6
  export const XHTML_NS = 'http://www.w3.org/1999/xhtml';
6
7
  export const SVG_NS = 'http://www.w3.org/2000/svg';
@@ -27,7 +28,7 @@ const NATIVE_ATTRIBUTES = {
27
28
  const TEMPLATE_REFERENCE = /(?<!\\)\$\{([a-zA-Z_][a-zA-Z0-9_]*)\}/g;
28
29
 
29
30
  // Display directives of the node text: consumed by the renderer, never written as attributes.
30
- const DISPLAY_ATTRIBUTES = ['format', 'mask', 'locale', 'places', 'dtype'];
31
+ const DISPLAY_ATTRIBUTES = ['format', 'mask', 'locale', 'timeZone', 'places', 'dtype'];
31
32
 
32
33
  // Output attributes whose `javascript:` value the browser runs as code (`action` never reaches the output).
33
34
  const URL_ATTRIBUTES = new Set(['href', 'src', 'formaction', 'xlink:href', 'xlink_href']);
@@ -145,18 +146,29 @@ export function textValue(value) {
145
146
 
146
147
  /**
147
148
  * `[item, attrs]` with the display directives consumed (HTML only).
148
- * `format`, `places`, `locale` and `dtype` do not format the text in 0.2.0. `mask` follows the
149
- * legacy rule (gnrlang.js asText): `%s` replaced by the value, no mask when the value is empty.
149
+ * The live page supplies its formatter; string renderers retain unformatted values.
150
+ * Plain masks use the same {} composition as the formatter, skipping empty text.
150
151
  */
151
- export function displayItem(item, attrs) {
152
+ export function displayItem(item, attrs, format = null) {
152
153
  const rest = {...attrs};
153
- const mask = rest.mask;
154
+ const {mask, format: pattern, locale, timeZone, dtype, places} = rest;
154
155
  for (const name of DISPLAY_ATTRIBUTES) delete rest[name];
155
- if (Array.isArray(item)) return [item, rest];
156
- if (typeof item === 'boolean') item = textValue(item);
157
- if (mask == null) return [item, rest];
158
- const text = item == null ? '' : textValue(item);
159
- return [text === '' ? text : String(mask).replaceAll('%s', () => text), rest];
156
+ const display = value => {
157
+ if (format && pattern != null) {
158
+ if (places != null) throw new RangeError('places is not implemented; specify precision in format');
159
+ return format(value, {pattern, mask, ...(locale == null ? {} : {locale}),
160
+ ...(timeZone == null ? {} : {timeZone}), ...(dtype == null ? {} : {dtype})});
161
+ }
162
+ if (typeof value === 'boolean') value = textValue(value);
163
+ if (mask == null) return value;
164
+ const text = value == null ? '' : textValue(value);
165
+ return text === '' ? text : applyMask(text, mask);
166
+ };
167
+ if (Array.isArray(item)) {
168
+ if (Object.hasOwn(rest, '_text')) rest._text = display(rest._text);
169
+ return [item, rest];
170
+ }
171
+ return [display(item), rest];
160
172
  }
161
173
 
162
174
  /** The parameter names of the `${name}` templates of `value`, in order; none for a value that is not a string. */
@@ -125,7 +125,8 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
125
125
  let attrs = runtimeAttrs;
126
126
  item = this.expressionValue(node, item);
127
127
  if (this.getRender(node.builder) === this) {
128
- [item, attrs] = displayItem(item, this.restoreNativeAttributes(node, runtimeAttrs));
128
+ [item, attrs] = displayItem(item, this.restoreNativeAttributes(node, runtimeAttrs),
129
+ (value, options) => this._binding.gramlot.format(value, options));
129
130
  }
130
131
  const children = Array.isArray(item) ? item : [];
131
132
  const text = Array.isArray(item) ? attrs._text : item;
@@ -471,7 +472,8 @@ export class GramlotRenderer extends GramlotHtmlRenderer {
471
472
  let attrs = node._getMeta('subbuilder') ? resolvedAttrs : renderer.adaptAttrs(resolvedAttrs);
472
473
  const branch = node.value instanceof SourceBag;
473
474
  let text = branch ? [] : this.expressionValue(node, value);
474
- if (renderer === this) [text, attrs] = displayItem(text, this.restoreNativeAttributes(node, attrs));
475
+ if (renderer === this) [text, attrs] = displayItem(text, this.restoreNativeAttributes(node, attrs),
476
+ (value, options) => this._binding.gramlot.format(value, options));
475
477
  this.html.update(record, node, attrs, branch ? attrs._text : text, resolved.visible ?? null);
476
478
  this.#projectControl(record, node, attrs, change);
477
479
  record.control?.refresh();
@@ -62,6 +62,8 @@ export interface GramlotServerOptions {
62
62
 
63
63
  /** The result of `registerPage`. */
64
64
  export interface RegisteredPage {
65
+ locale?: string;
66
+ timeZone?: string;
65
67
  /** The id of the registered page. */
66
68
  pageId: string;
67
69
  /** The title of the page. */
@@ -126,20 +126,23 @@ export class GramlotServer {
126
126
  if (this.pages.size >= this.maxPages) throw new ServerCapacity('Page registry capacity reached');
127
127
  const pageId = crypto.randomUUID().replaceAll('-', '');
128
128
  this.pages.set(pageId, {PageClass, owner, expires: performance.now() + this.pageTtl * 1000});
129
- return {pageId, title: PageClass.title, resources};
129
+ return {pageId, title: PageClass.title, resources,
130
+ ...(PageClass.locale == null ? {} : {locale: PageClass.locale}),
131
+ ...(PageClass.timeZone == null ? {} : {timeZone: PageClass.timeZone})};
130
132
  }
131
133
 
132
134
  /** Register the page and return its bootstrap. prefix is the mount prefix chosen
133
135
  * by the adapter, added once to the root-relative bootstrap URLs. */
134
136
  async openPage(path, {owner = null, prefix = ''} = {}) {
135
137
  if (typeof prefix !== 'string') throw new TypeError('Mount prefix must be a string');
136
- const {pageId, title, resources} = await this.registerPage(path, {owner});
138
+ const {pageId, title, resources, locale, timeZone} = await this.registerPage(path, {owner});
137
139
  try {
138
140
  const nonce = createNonce();
139
141
  // The CSS links and the JS modules reach the page through PageBootstrap (§4.12, D9).
140
142
  const bootstrap = scriptJson({
141
143
  config: {pageId, rpcUrl: prefixed(prefix, this.rpcUrl), closeUrl: prefixed(prefix, this.closeUrl),
142
- rootId: this.rootId, capabilities: this.capabilities},
144
+ rootId: this.rootId, capabilities: this.capabilities,
145
+ ...(locale === undefined ? {} : {locale}), ...(timeZone === undefined ? {} : {timeZone})},
143
146
  resources: {css: resources.css.map(url => prefixed(prefix, url)),
144
147
  js: resources.js.map(({url, group}) => ({url: prefixed(prefix, url), group}))},
145
148
  });
@@ -48,6 +48,8 @@ function declare(PageClass, methodName, marker, auth) {
48
48
  /** Server-side page base; unrelated to browser view components. */
49
49
  export class Page {
50
50
  static title = 'Gramlot';
51
+ static locale = null;
52
+ static timeZone = null;
51
53
  static installable = false;
52
54
  static appIcon = null;
53
55
 
@@ -19,6 +19,8 @@ export function endpointMethod(page: Page, name: string): Function | null;
19
19
  export class Page {
20
20
  /** The title of the page document. */
21
21
  static title: string;
22
+ static locale: string | null;
23
+ static timeZone: string | null;
22
24
  static installable: boolean;
23
25
  static appIcon: string | null;
24
26
  manifest(options?: {baseUrl?: string}): Record<string, unknown> | Promise<Record<string, unknown>>;
@@ -15,11 +15,13 @@ import {XHTML_NS} from '../renderer/attributes.js';
15
15
  export class ButtonBinding {
16
16
  #binding;
17
17
  #record;
18
+ #cancelDelay = null;
19
+ #pendingCount = 0;
18
20
 
19
- /** The ButtonBinding of a built element, or null when the element is not a `<button>`. */
21
+ /** The binding of a Gramlot button; html_button remains native despite its DOM tag. */
20
22
  static for(binding, record) {
21
23
  const element = record.element;
22
- if (element?.namespaceURI !== XHTML_NS || element.localName !== 'button') return null;
24
+ if (binding.node.nodeTag !== 'button' || element?.namespaceURI !== XHTML_NS || element.localName !== 'button') return null;
23
25
  return new ButtonBinding(binding, record);
24
26
  }
25
27
 
@@ -79,6 +81,7 @@ export class ButtonBinding {
79
81
 
80
82
  detach() {
81
83
  this.element.removeEventListener('click', this);
84
+ this.#cancelDelay?.();
82
85
  }
83
86
 
84
87
  handleEvent(event) {
@@ -97,6 +100,38 @@ export class ButtonBinding {
97
100
  event.stopPropagation();
98
101
  if (this.element.disabled) return;
99
102
  const counter = this.binding.countClick();
103
+ // Legacy reads inherited _delay. An explicit zero overrides an ancestor's delay.
104
+ let owner = this.node;
105
+ while (owner && owner.getAttr('_delay') == null) owner = owner.parentNode;
106
+ const delay = owner ? owner.builder.runtimeValues(owner)[1]._delay : 0;
107
+ if (delay > 0) {
108
+ const count = this.#pendingCount + 1;
109
+ this.#cancelDelay?.();
110
+ this.#pendingCount = count;
111
+ const timer = setTimeout(() => {
112
+ release();
113
+ this.#cancelDelay = null;
114
+ this.#pendingCount = 0;
115
+ this.#execute(event, count);
116
+ }, delay);
117
+ const release = this.binding.track(() => this.#cancelDelay?.());
118
+ this.#cancelDelay = () => {
119
+ clearTimeout(timer);
120
+ release();
121
+ this.#cancelDelay = null;
122
+ this.#pendingCount = 0;
123
+ };
124
+ return;
125
+ }
126
+ this.#cancelDelay?.();
127
+ return this.#execute(event, counter);
128
+ }
129
+
130
+ /** Deliver the last event once; arguments are read when the mechanism executes. */
131
+ #execute(event, counter) {
132
+ if (this.binding.closed || this.element.disabled) return;
133
+ const mechanism = this.mechanism;
134
+ if (!mechanism) return;
100
135
  const node = this.node;
101
136
  const modifiers = eventToString(event);
102
137
  if (mechanism === 'controller') {