2014-01-30 13:40:36 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/widgets/keyboard.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: widget
|
|
|
|
|
|
|
|
Keyboard shortcut widget
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Widget = require("$:/core/modules/widgets/widget.js").widget;
|
|
|
|
|
|
|
|
var KeyboardWidget = function(parseTreeNode,options) {
|
|
|
|
this.initialise(parseTreeNode,options);
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Inherit from the base widget class
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype = new Widget();
|
|
|
|
|
|
|
|
/*
|
|
|
|
Render this widget into the DOM
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype.render = function(parent,nextSibling) {
|
|
|
|
var self = this;
|
|
|
|
// Remember parent
|
|
|
|
this.parentDomNode = parent;
|
|
|
|
// Compute attributes and execute state
|
|
|
|
this.computeAttributes();
|
|
|
|
this.execute();
|
2017-01-13 18:17:19 +00:00
|
|
|
var tag = this.parseTreeNode.isBlock ? "div" : "span";
|
|
|
|
if(this.tag && $tw.config.htmlUnsafeElements.indexOf(this.tag) === -1) {
|
|
|
|
tag = this.tag;
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
// Create element
|
2017-01-13 18:17:19 +00:00
|
|
|
var domNode = this.document.createElement(tag);
|
2014-01-30 13:40:36 +00:00
|
|
|
// Assign classes
|
2021-06-06 10:03:08 +00:00
|
|
|
this.domNode = domNode;
|
|
|
|
this.assignDomNodeClasses();
|
2014-01-30 13:40:36 +00:00
|
|
|
// Add a keyboard event handler
|
2021-05-25 21:18:37 +00:00
|
|
|
$tw.utils.addEventListeners(domNode,[
|
|
|
|
{name: "keydown", handlerObject: this, handlerMethod: "handleChangeEvent"}
|
|
|
|
]);
|
2014-01-30 13:40:36 +00:00
|
|
|
// Insert element
|
|
|
|
parent.insertBefore(domNode,nextSibling);
|
|
|
|
this.renderChildren(domNode,null);
|
|
|
|
this.domNodes.push(domNode);
|
|
|
|
};
|
|
|
|
|
2021-05-25 21:18:37 +00:00
|
|
|
KeyboardWidget.prototype.handleChangeEvent = function(event) {
|
2022-10-07 16:31:04 +00:00
|
|
|
if ($tw.keyboardManager.handleKeydownEvent(event, {onlyPriority: true})) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-14 11:03:59 +00:00
|
|
|
var keyInfo = $tw.keyboardManager.getMatchingKeyDescriptor(event,this.keyInfoArray);
|
|
|
|
if(keyInfo) {
|
2021-05-25 21:18:37 +00:00
|
|
|
var handled = this.invokeActions(this,event);
|
|
|
|
if(this.actions) {
|
|
|
|
var variables = {
|
|
|
|
"event-key": event.key,
|
|
|
|
"event-code": event.code,
|
|
|
|
"modifier": $tw.keyboardManager.getEventModifierKeyDescriptor(event)
|
|
|
|
};
|
2021-06-14 11:03:59 +00:00
|
|
|
if(keyInfo.keyDescriptor) {
|
|
|
|
variables["event-key-descriptor"] = keyInfo.keyDescriptor;
|
|
|
|
}
|
2021-05-25 21:18:37 +00:00
|
|
|
this.invokeActionString(this.actions,this,event,variables);
|
|
|
|
}
|
|
|
|
this.dispatchMessage(event);
|
|
|
|
if(handled || this.actions || this.message) {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-30 13:40:36 +00:00
|
|
|
KeyboardWidget.prototype.dispatchMessage = function(event) {
|
|
|
|
this.dispatchEvent({type: this.message, param: this.param, tiddlerTitle: this.getVariable("currentTiddler")});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Compute the internal state of the widget
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype.execute = function() {
|
2018-11-15 21:40:27 +00:00
|
|
|
var self = this;
|
2014-01-30 13:40:36 +00:00
|
|
|
// Get attributes
|
2019-07-30 12:47:56 +00:00
|
|
|
this.actions = this.getAttribute("actions","");
|
|
|
|
this.message = this.getAttribute("message","");
|
|
|
|
this.param = this.getAttribute("param","");
|
|
|
|
this.key = this.getAttribute("key","");
|
|
|
|
this.tag = this.getAttribute("tag","");
|
2024-08-29 16:25:30 +00:00
|
|
|
if($tw.keyboardManager) {
|
|
|
|
this.keyInfoArray = $tw.keyboardManager.parseKeyDescriptors(this.key);
|
|
|
|
if(this.key.substr(0,2) === "((" && this.key.substr(-2,2) === "))") {
|
|
|
|
this.shortcutTiddlers = [];
|
|
|
|
var name = this.key.substring(2,this.key.length -2);
|
|
|
|
$tw.utils.each($tw.keyboardManager.lookupNames,function(platformDescriptor) {
|
|
|
|
self.shortcutTiddlers.push("$:/config/" + platformDescriptor + "/" + name);
|
|
|
|
});
|
|
|
|
}
|
2018-11-15 16:50:23 +00:00
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
// Make child widgets
|
|
|
|
this.makeChildWidgets();
|
|
|
|
};
|
|
|
|
|
2021-06-06 10:03:08 +00:00
|
|
|
KeyboardWidget.prototype.assignDomNodeClasses = function() {
|
|
|
|
var classes = this.getAttribute("class","").split(" ");
|
|
|
|
classes.push("tc-keyboard");
|
|
|
|
this.domNode.className = classes.join(" ");
|
|
|
|
};
|
|
|
|
|
2014-01-30 13:40:36 +00:00
|
|
|
/*
|
|
|
|
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
|
|
|
|
*/
|
|
|
|
KeyboardWidget.prototype.refresh = function(changedTiddlers) {
|
|
|
|
var changedAttributes = this.computeAttributes();
|
2021-06-06 10:03:08 +00:00
|
|
|
if(changedAttributes.message || changedAttributes.param || changedAttributes.key || changedAttributes.tag) {
|
2014-01-30 13:40:36 +00:00
|
|
|
this.refreshSelf();
|
|
|
|
return true;
|
2021-06-06 10:03:08 +00:00
|
|
|
} else if(changedAttributes["class"]) {
|
|
|
|
this.assignDomNodeClasses();
|
2014-01-30 13:40:36 +00:00
|
|
|
}
|
2018-11-15 16:50:23 +00:00
|
|
|
// Update the keyInfoArray if one of its shortcut-config-tiddlers has changed
|
2024-08-29 16:25:30 +00:00
|
|
|
if(this.shortcutTiddlers && $tw.utils.hopArray(changedTiddlers,this.shortcutTiddlers) && $tw.keyboardManager) {
|
2018-11-15 16:50:23 +00:00
|
|
|
this.keyInfoArray = $tw.keyboardManager.parseKeyDescriptors(this.key);
|
|
|
|
}
|
2014-01-30 13:40:36 +00:00
|
|
|
return this.refreshChildren(changedTiddlers);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.keyboard = KeyboardWidget;
|
|
|
|
|
|
|
|
})();
|