diff --git a/oxygen/js-options/README.md b/oxygen/js-options/README.md index 509a306..abecc55 100644 --- a/oxygen/js-options/README.md +++ b/oxygen/js-options/README.md @@ -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) diff --git a/oxygen/js-options/hello.js b/oxygen/js-options/hello.js index e083f67..fe69e43 100644 --- a/oxygen/js-options/hello.js +++ b/oxygen/js-options/hello.js @@ -1,3 +1,11 @@ -hello = function (){ - Packages.java.lang.System.out.println("Hello world."); +hello = function () { + 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); + } } \ No newline at end of file diff --git a/oxygen/js-options/js-options.xpr b/oxygen/js-options/js-options.xpr new file mode 100644 index 0000000..14ffd85 --- /dev/null +++ b/oxygen/js-options/js-options.xpr @@ -0,0 +1,26 @@ + + + + + + + + + additional.frameworks.directories + + /home/vdv/projects/pro/Remix/oxygen/frameworks/http___oxygen.remix.rece.hachette_livre.fr_oxygenUpdate.xml + /home/vdv/projects/tea/dyomedea/ramblings/oxygen/js-options + + + + key.editor.document.type.custom.locations.option.pane + true + + + + + + + + + \ No newline at end of file diff --git a/oxygen/js-options/options.js b/oxygen/js-options/options.js new file mode 100644 index 0000000..8da158f --- /dev/null +++ b/oxygen/js-options/options.js @@ -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; +} diff --git a/oxygen/js-options/test.js b/oxygen/js-options/test.js new file mode 100644 index 0000000..6590582 --- /dev/null +++ b/oxygen/js-options/test.js @@ -0,0 +1,2 @@ +include('hello.js'); +hello();