First draft of js-options

This commit is contained in:
Eric van der Vlist 2021-04-28 16:48:01 +02:00
parent b66831cc1d
commit 51ecfd78cd
5 changed files with 72 additions and 48 deletions

View File

@ -1,53 +1,9 @@
# Alltogether: opening a JavaScript debugger within Oxygen [JS Operations](https://www.oxygenxml.com/doc/versions/23.1/ug-editor/topics/dg-default-author-operations.html#dg-default-author-operations__jsoperation) through inclusion
This rambling shows how a [debugger](../js-debugger) can be launched using an [inclusion](../js-include).
# Options
This rambling shows how to add functionalities to the [OptionStorage](https://www.oxygenxml.com/InstData/Editor/SDK/javadoc/ro/sync/ecss/extensions/api/OptionsStorage.html) mechanism.
## Installation
To do so:
### 1) add the include() function to your commons.js file:
```javascript
include = function (filepath, isAbsolute) {
if (isAbsolute === undefined || isAbsolute == false) {
filepath = Packages.ro.sync.ecss.extensions.commons.operations.CommonsOperationsUtil.expandAndResolvePath(authorAccess, filepath);
}
var text = new java.lang.String(java.nio.file.Files.readAllBytes(java.nio.file.Paths. get (java.net.URI(filepath))));
text = String(text);
eval(text);
}
```
### 2) create a debugger.js file:
```javascript
startDebugger = function () {
var runnable = {
run: function () {
main.dispose();
}
}
var context = Packages.org.mozilla.javascript.Context.getCurrentContext();
// Within the current context...
var contextFactory = context.getFactory();
var scope = Packages.org.mozilla.javascript.tools.shell.Environment(runnable.__parent__);
// and the scope of the runnable variable's parent ...
var main = Packages.org.mozilla.javascript.tools. debugger.Main.mainEmbedded(contextFactory, scope, 'Debugger');
// start a debugging session ...
main.setExitAction(java.lang.Runnable(runnable));
// , clean the resources at exit time...
main.setVisible(true);
// and make it visible/
}
```
### 3) Use these functions in your doOperation() method:
```javascript
doOperation = function () {
include('debugger.js');
startDebugger();
}
```
## See also
More details on these hacks:
* [debugger](../js-debugger)

View File

@ -1,3 +1,11 @@
hello = function () {
Packages.java.lang.System.out.println("Hello world.");
include('options.js');
var options = new Options(authorAccess, 'hello');
options.setOption('Hello', 'World');
options.setOption('See you later', 'aligator');
var myOptions = options.getOptions();
for (var key in myOptions) {
var value = myOptions[key];
Packages.java.lang.System.out.println(key + ' ' + value);
}
}

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="22.0">
<meta>
<filters directoryPatterns="" filePatterns="\Qjs-debugger.xpr\E" positiveFilePatterns="" showHiddenFiles="false"/>
<options>
<serialized version="22.0" xml:space="preserve">
<serializableOrderedMap>
<entry>
<String>additional.frameworks.directories</String>
<String-array>
<String>/home/vdv/projects/pro/Remix/oxygen/frameworks/http___oxygen.remix.rece.hachette_livre.fr_oxygenUpdate.xml</String>
<String>/home/vdv/projects/tea/dyomedea/ramblings/oxygen/js-options</String>
</String-array>
</entry>
<entry>
<String>key.editor.document.type.custom.locations.option.pane</String>
<Boolean>true</Boolean>
</entry>
</serializableOrderedMap>
</serialized>
</options>
</meta>
<projectTree name="js-options.xpr">
<folder path="."/>
</projectTree>
</project>

View File

@ -0,0 +1,32 @@
Options = function (authorAccess, namespace) {
this.namespace = namespace;
this.authorAccess = authorAccess;
this.optionsStorage = this.authorAccess.getOptionsStorage();
}
Options.SYSTEM = '__system__';
Options.prototype.getOption = function (key, defaultValue) {
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
return this.optionsStorage.getOption(key, defaultValue);
}
Options.prototype.setOption = function (key, value) {
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
options[key] = (new Date()).toISOString();
this.optionsStorage.setOption(this.namespace, JSON.stringify(options));
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
this.optionsStorage.setOption(key, value);
}
Options.prototype.getOptions = function (key) {
var results = {
};
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
for (var key in options) {
results[key] = this.getOption(key, '');
}
return results;
}

View File

@ -0,0 +1,2 @@
include('hello.js');
hello();