Commit ea7dc847 authored by Jérome Perrin's avatar Jérome Perrin

monaco_editor: Add incomplete typescript declarations for Renderjs & RSVP

So that javascript editor provides hints for code using these libraries.

Some pointers for type scripts definitions:
https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
https://definitelytyped.org/
parent 704fac1c
......@@ -70,6 +70,7 @@
})
.onStateChange(function(modification_dict) {
var queue = new RSVP.Queue();
if (modification_dict.hasOwnProperty('value')) {
// Do not notify the UI when initializing the value
this.state.ignoredChangeDuringInitialization = true;
......@@ -87,7 +88,28 @@
monaco.languages.html.htmlDefaults.options.format.tabSize = 2;
monaco.languages.html.htmlDefaults.options.format.insertSpaces = true;
}
if (this.state.model_language === 'javascript') {
// Type mapping for Nexedi libraries
function addExtraLibrary(script_name, lib_name) {
return fetch(script_name)
.then(function(resp) {
return resp.text();
})
.then(function(script_code) {
monaco.languages.typescript.javascriptDefaults.addExtraLib(
script_code,
lib_name
);
});
}
queue
.push(addExtraLibrary('./monaco-rsvp.d.ts', 'rsvp'))
.push(addExtraLibrary('./monaco-renderjs.d.ts', 'renderjs'))
.push(addExtraLibrary('./monaco-jio.d.ts', 'jio'));
}
}
return queue;
})
.declareMethod('getContent', function() {
......
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>monaco-jio.d.ts</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/typescript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
// Type definitions for renderjs 0.20.0
// Project: https://lab.nexedi.com/nexedi/renderjs
// Definitions by: Jerome Perrin <https://github.com/perrinjerome>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped (not yet)
// TypeScript Version: 2.4
// XXX needs nexedi RSVP patch loaded.
/**
* A gadget state dict.
*/
interface GadgetState {
[key: string]: any;
}
/**
* Options to declared methods
*/
interface MethodOptions {
/**
* controls mutual exclusions of method calls.
*/
mutex?: string;
}
/**
* Options to declare a gadget.
*/
interface GadgetDeclarationOptions {
/**
* The local name of the gadget in declarer namespace
*/
scope: string;
/**
* The sandbox type, can be iframe to render in an iframe or public to render in the
* same window context.
*/
sandbox: "public" | "iframe";
/**
* The element where to bind the gadget.
*/
element: HTMLElement;
}
/**
* RenderJS gadget instance
*/
declare class Gadget {
/**
* The current state of the gadget.
* To mutate the state, use `changeState`.
*/
public state: GadgetState;
/**
* Because declared methods will be added to the gadget class, the type declaration is loose.
*/
[key: string]: any;
/**
* `declareGadget`: Declare a child gadget
*
* The parameters exactly correspond to those when declaring the gadget in HTML, with the addition of element, to specify an element to wrap the gadget in, rather than the default auto-generated <div>.
*
* @param gadgetURL the URL of the HTML page of the gadget.
* @param options the gadget options
*/
declareGadget(
gadgetURL: string,
options: GadgetDeclarationOptions
): RSVP.Queue<Gadget>;
/**
* `dropGadget`: Drop a child gadget.
*
* @param gadgetScope the scope of the previously declared gadget
*/
dropGadget(gadgetScope: string): RSVP.Queue<Gadget>;
/**
* `getDeclaredGadget`: Get a previously declared child gadget
*
* Returns a child gadget instance, previously declared with `declareGadget`.
*
* @param gadgetScope the scope of the previously declared gadget
*/
getDeclaredGadget(gadgetScope: string): RSVP.Queue<Gadget>;
/**
* `setState`: Set Initial State
*
* The gadget's state should be set once when initialising the gadget. The state should contain key/value pairs, but the state is just an ordinary JavaScript object with no hard restrictions.
*
* @param initialState the initial state.
*/
setState(initialState: GadgetState): RSVP.Queue<void>;
/**
* `changeState`: Change State
*
* Change the state by passing in a new key-value pair, which only overwrites the keys provided in the changeState call, and only if the current and new values are different. All other keys remain unchanged.
*
* @param newState the changes made to the state.
*/
changeState(newState: GadgetState): RSVP.Queue<void>;
}
/**
* RenderJs gadget class.
*/
interface GadgetKlass {
/**
* `setState`: Set Initial State
*
* The gadget's state should be set once when initialising the gadget. The state should contain key/value pairs, but the state is just an ordinary JavaScript object with no hard restrictions.
*
* @param initialState the initial state.
*/
setState(initialState: GadgetState): GadgetKlass;
/**
* `onStateChange`: Change State Callback.
*
* @param handler function implementing the service logic.
*/
onStateChange(
handler: (
this: Gadget,
modification_dict: GadgetState
) => RSVP.Queue<any> | void
): GadgetKlass;
/**
* `ready`: define a function to be called when gadget is ready
*
* The ready handler is triggered automatically when all gadget dependencies have loaded.
*
* @param f function to call when gadget is ready.
*/
ready(
f: (this: Gadget, gadget?: Gadget) => RSVP.Queue<any> | void
): GadgetKlass;
/**
* `declareMethod`: Declare Method
*
* The ready handler is triggered automatically when all gadget dependencies have loaded.
*
* @param methodName name of the method
* @param method function implementing the method logic.
* @param methodOptions the options for this methods.
*/
declareMethod(
methodName: string,
method: (this: Gadget, ...args: any[]) => any,
methodOptions?: MethodOptions
): GadgetKlass;
/**
* `declareService`: Declare a service.
*
* Services automatically trigger as soon as the gadget is loaded into the DOM, and are usually used for event binding. There can be multiple declareService handlers, which all trigger simultaneously.
*
* @param service function implementing the service logic.
*/
declareService(
service: (this: Gadget) => RSVP.Queue<any> | Promise<any>
): GadgetKlass;
/**
* `declareJob`: Declare a job.
*
* Jobs manually trigger by being called, like an ordinary RenderJS method. However, calling a job cancels the last call of the job if it hasn't finished. *
*
* @param jobName name of the job
* @param service function implementing the job logic.
*/
declareJob(
jobName: string,
job: (this: Gadget, ...args: any[]) => RSVP.Queue<any> | Promise<any>
): GadgetKlass;
/**
* `onEvent`: Bind Event.
*
* Jobs manually trigger by being called, like an ordinary RenderJS method. However, calling a job cancels the last call of the job if it hasn't finished. *
*
* @param eventName name of the Event
* @param eventHandler function implementing the logic.
* @param useCapture same semantics as EventTarget.addEventListener
* @param preventDefault XXX ?
*/
onEvent(
eventName: string,
eventHandler: (this: Gadget, event?: Event) => RSVP.Queue<any>,
useCapture?: boolean,
preventDefault?: boolean | Promise<any> | void
): GadgetKlass;
/**
* `onLoop`: Loop.
*
* When the gadget is displayed, loop on the callback method in a service.
* A delay can be configured between each loop execution.
*
* @param loopFunction the function to execute in the loop.
* @param delay the delay between call, in seconds.
*/
onLoop(loopFunction: (this: Gadget) => void, delay: number): GadgetKlass;
/**
* `allowPublicAcquisition`: Publish Method.
*
* Publish a method to allow children to acquire it.
* Only methods passed into allowPublicAcquisition in a parent gadget can be acquired using declareAcquiredMethod in a child gadget.
*
* @param methodName name of the method
* @param method function implementing the method logic.
*/
allowPublicAcquisition(
methodName: string,
method:
/**
* @param argument_list The arguments which the method is called.
* @param scope The caller gadget scope.
*/
(this: Gadget, argument_list: any[], scope: string) => any
): GadgetKlass;
/**
* `allowPublicAcquisition`: Publish Method.
*
* Acquire a method from a parent gadget, by passing the name of the published method as the first parameter and the name to call it locally as the second parameter.
*
* @param localMethodName name of the method as seen from this gadget instance.
* @param parentMethodName name of the method on the parent gadget instance.
*/
declareAcquiredMethod(
localMethodName: string,
parentMethodName: string
): GadgetKlass;
}
declare function rJS(window: Window): GadgetKlass;
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>monaco-renderjs.d.ts</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/typescript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="File" module="OFS.Image"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_Cacheable__manager_id</string> </key>
<value> <string>http_cache</string> </value>
</item>
<item>
<key> <string>__name__</string> </key>
<value> <string>monaco-rsvp.d.ts</string> </value>
</item>
<item>
<key> <string>content_type</string> </key>
<value> <string>text/typescript</string> </value>
</item>
<item>
<key> <string>precondition</string> </key>
<value> <string></string> </value>
</item>
<item>
<key> <string>title</string> </key>
<value> <string></string> </value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
......@@ -115,6 +115,25 @@ $script.onload = function() {
monaco.languages.html.htmlDefaults.options.format.insertSpaces = true;
}
// Type mapping for Nexedi libraries
function addExtraLibrary(script_name, lib_name) {
return fetch(script_name)
.then(function(resp) {
return resp.text();
})
.then(function(script_code) {
monaco.languages.typescript.javascriptDefaults.addExtraLib(
script_code,
lib_name
);
});
}
Promise.all([
addExtraLibrary('../monaco-rsvp.d.ts', 'rsvp'),
addExtraLibrary('../monaco-renderjs.d.ts', 'renderjs'),
addExtraLibrary('../monaco-jio.d.ts', 'jio')]);
var timeout = null;
// minimal pollyfil for AbortController
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment