markdown-up 3.7.0 → 3.8.0

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.
package/lib/app.js CHANGED
@@ -106,6 +106,7 @@ export class MarkdownUp {
106
106
  this.menu = (options !== null ? options.menu : null) ?? true;
107
107
  this.systemPrefix = (options !== null ? options.systemPrefix : null) ?? 'https://craigahobbs.github.io/markdown-up/include/';
108
108
  this.url = (options !== null ? options.url : null) ?? 'README.md';
109
+ this.runtimeDocumentKeyDown = null;
109
110
  this.runtimeWindowResize = null;
110
111
  this.runtimeTimeoutId = null;
111
112
  this.runtimeCount = 0;
@@ -178,6 +179,12 @@ export class MarkdownUp {
178
179
  this.window.addEventListener('resize', result.resize);
179
180
  }
180
181
 
182
+ // Set the document keydown handler?
183
+ if ('keyDown' in result) {
184
+ this.runtimeDocumentKeyDown = result.keyDown;
185
+ this.window.document.addEventListener('keydown', result.keyDown);
186
+ }
187
+
181
188
  // Render the element model
182
189
  renderElements(this.window.document.body, result.elements);
183
190
 
@@ -246,6 +253,12 @@ export class MarkdownUp {
246
253
  this.window.removeEventListener('resize', this.runtimeWindowResize);
247
254
  this.runtimeWindowResize = null;
248
255
  }
256
+
257
+ // Clear the document keydown event handler
258
+ if (clearResize && this.runtimeDocumentKeyDown !== null) {
259
+ this.window.document.removeEventListener('keydown', this.runtimeDocumentKeyDown);
260
+ this.runtimeDocumentKeyDown = null;
261
+ }
249
262
  }
250
263
 
251
264
 
@@ -348,6 +361,9 @@ export class MarkdownUp {
348
361
  if (scriptOptions.runtime.documentFocus !== null) {
349
362
  result.focus = scriptOptions.runtime.documentFocus;
350
363
  }
364
+ if (scriptOptions.runtime.documentKeyDown !== null) {
365
+ result.keyDown = scriptOptions.runtime.documentKeyDown;
366
+ }
351
367
  if (scriptOptions.runtime.documentTitle !== null) {
352
368
  result.title = scriptOptions.runtime.documentTitle;
353
369
  }
package/lib/script.js CHANGED
@@ -113,6 +113,7 @@ const pixelsPerPoint = 4 / 3;
113
113
  * @property {Object} options - The [markdown-script options]{@link module:lib/script~MarkdownScriptOptions}
114
114
  * @property {?Object[]} elements - The runtime-generated element model
115
115
  * @property {?string} documentFocus - The runtime-set input-focus element ID
116
+ * @property {?function} documentKeyDown - The runtime-set document keydown callback
116
117
  * @property {?string} documentReset - The runtime-set document-reset element ID
117
118
  * @property {?string} documentTitle - The runtime-set document title
118
119
  * @property {?string} windowLocation - The runtime-set document location
@@ -129,6 +130,7 @@ export class MarkdownScriptRuntime {
129
130
  this.options = options;
130
131
  this.elements = null;
131
132
  this.documentFocus = null;
133
+ this.documentKeyDown = null;
132
134
  this.documentReset = null;
133
135
  this.documentTitle = null;
134
136
  this.windowLocation = null;
@@ -162,6 +164,7 @@ export class MarkdownScriptRuntime {
162
164
  // Reset the runtime state
163
165
  reset() {
164
166
  this.documentFocus = null;
167
+ this.documentKeyDown = null;
165
168
  this.documentReset = null;
166
169
  this.documentTitle = null;
167
170
  this.windowLocation = null;
@@ -7,6 +7,7 @@ import {lineChartElements, validateLineChart} from './lineChart.js';
7
7
  import {valueArgsModel, valueArgsValidate} from 'bare-script/lib/value.js';
8
8
  import {encodeQueryString} from 'schema-markdown/lib/encode.js';
9
9
  import {markdownHeaderId as markdownModelHeaderId} from 'markdown-model/lib/elements.js';
10
+ import {parseSchemaMarkdown} from 'schema-markdown/lib/parser.js';
10
11
  import {schemaMarkdownDoc} from 'schema-markdown-doc/lib/schemaMarkdownDoc.js';
11
12
  import {validateElements} from 'element-model/lib/elementModel.js';
12
13
 
@@ -115,6 +116,106 @@ const documentSetFocusArgs = valueArgsModel([
115
116
  ]);
116
117
 
117
118
 
119
+ // $function: documentSetKeyDown
120
+ // $group: Document
121
+ // $doc: Set the document keydown event handler. For example:
122
+ // $doc:
123
+ // $doc: ```barescript
124
+ // $doc: function myAppMain():
125
+ // $doc: myAppRender()
126
+ // $doc: documentSetKeyDown(myAppKeyDown)
127
+ // $doc: endfunction
128
+ // $doc:
129
+ // $doc: function myAppRender(key):
130
+ // $doc: markdownPrint( \
131
+ // $doc: '# KeyDown Test', \
132
+ // $doc: '', \
133
+ // $doc: if(key, '**Key pressed:** "' + key + '"', '*No key pressed yet.*') \
134
+ // $doc: )
135
+ // $doc: endfunction
136
+ // $doc:
137
+ // $doc: function myAppKeyDown(event):
138
+ // $doc: key = objectGet(event, 'key')
139
+ // $doc: myAppRender(key)
140
+ // $doc: endfunction
141
+ // $doc:
142
+ // $doc: myAppMain()
143
+ // $doc: ```
144
+ // $arg callback: The keydown event callback function that takes a single
145
+ // $arg callback: [key event object](model.html#var.vName='DocumentKeyEvent') argument, `event`.
146
+ function documentSetKeyDown(args, options) {
147
+ const [callback] = valueArgsValidate(documentSetKeyDownArgs, args);
148
+ const {runtime} = options;
149
+ runtime.documentKeyDown = async (event) => {
150
+ await runtime.eventHandle(async () => {
151
+ // Create the event callback object
152
+ const eventObj = {
153
+ 'key': event.key,
154
+ 'code': event.code,
155
+ 'keyCode': event.keyCode,
156
+ 'ctrlKey': event.ctrlKey,
157
+ 'altKey': event.altKey,
158
+ 'shiftKey': event.shiftKey,
159
+ 'metaKey': event.metaKey,
160
+ 'repeat': event.repeat,
161
+ 'location': event.location
162
+ };
163
+
164
+ // Call the event callback function
165
+ options.statementCount = 0;
166
+ try {
167
+ await callback([eventObj], options);
168
+ } catch ({message}) {
169
+ if (options.debug) {
170
+ options.logFn(`MarkdownUp: Error executing documentSetKeyDown callback: ${message}`);
171
+ }
172
+ }
173
+ options.runtimeUpdateFn();
174
+ });
175
+ };
176
+ }
177
+
178
+ const documentSetKeyDownArgs = valueArgsModel([
179
+ {'name': 'callback', 'type': 'function'}
180
+ ]);
181
+
182
+
183
+ export const documentKeyEventTypes = parseSchemaMarkdown(`\
184
+ group "Events"
185
+
186
+
187
+ # A document key event object
188
+ struct DocumentKeyEvent
189
+
190
+ # The key value (e.g., "a", "Enter", "ArrowUp")
191
+ string key
192
+
193
+ # The physical key code (e.g., "KeyA", "Enter")
194
+ string code
195
+
196
+ # The legacy numeric code (e.g., 65 for 'a')
197
+ string keyCode
198
+
199
+ # If true, the control key is pressed
200
+ bool ctrlKey
201
+
202
+ # If true, the alt key is pressed
203
+ bool altKey
204
+
205
+ # If true, the shift key is pressed
206
+ bool shiftKey
207
+
208
+ # If true, the cmd key is pressed
209
+ bool metaKey
210
+
211
+ # If true, the key is held down
212
+ bool repeat
213
+
214
+ # 0=standard, 1=left, 2=right
215
+ int location
216
+ `);
217
+
218
+
118
219
  // $function: documentSetReset
119
220
  // $group: Document
120
221
  // $doc: Set the document reset element
@@ -1093,6 +1194,7 @@ export const markdownScriptFunctions = {
1093
1194
  documentFontSize,
1094
1195
  documentInputValue,
1095
1196
  documentSetFocus,
1197
+ documentSetKeyDown,
1096
1198
  documentSetReset,
1097
1199
  documentSetTitle,
1098
1200
  documentURL,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "markdown-up",
4
- "version": "3.7.0",
4
+ "version": "3.8.0",
5
5
  "description": "MarkdownUp is a Markdown viewer",
6
6
  "keywords": [
7
7
  "markdown",