2020-12-20 16:45:51 +01:00
|
|
|
var ace: any;
|
|
|
|
namespace OS {
|
|
|
|
export namespace application {
|
|
|
|
|
|
|
|
|
2020-12-20 17:51:51 +01:00
|
|
|
/**
|
|
|
|
* Wrapper model for the ACE text editor
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @class CodePadACEModel
|
|
|
|
* @extends {CodePadBaseEditorModel}
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
export class CodePadACEModel extends CodePadBaseEditorModel {
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to ACE language modes
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @type {GenericObject<any>}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
private modes: GenericObject<any>;
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of CodePadACEModel.
|
|
|
|
* @param {CodePad} app CodePad instance
|
|
|
|
* @param {GUI.tag.TabBarTag} tabbar tabbar element
|
|
|
|
* @param {HTMLElement} editorarea main editor container element
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
constructor(app: CodePad, tabbar: GUI.tag.TabBarTag, editorarea: HTMLElement) {
|
|
|
|
ace.config.set("basePath", "scripts/ace");
|
|
|
|
ace.require("ace/ext/language_tools");
|
2020-12-20 17:51:51 +01:00
|
|
|
super(app, tabbar, editorarea);
|
2020-12-20 16:45:51 +01:00
|
|
|
this.modes = ace.require("ace/ext/modelist");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-20 17:51:51 +01:00
|
|
|
/**
|
|
|
|
* Get language modes
|
|
|
|
*
|
|
|
|
* @return {*} {GenericObject<any>[]}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
getModes(): GenericObject<any>[] {
|
|
|
|
const list = [];
|
|
|
|
let v: GenericObject<any>;
|
|
|
|
for (v of Array.from(this.modes.modes)) {
|
|
|
|
list.push({ text: v.caption, mode: v.mode });
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the editor theme
|
|
|
|
*
|
|
|
|
* @param {string} theme theme name
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
setTheme(theme: string): void {
|
|
|
|
this.editor.setTheme(theme);
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the editor undo manager
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @param {GenericObject<any>} um
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
protected setUndoManager(um: GenericObject<any>): void {
|
|
|
|
this.editor.getSession().setUndoManager(um);
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the editor cursor
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @param {GenericObject<any>} c cursor option
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
protected setCursor(c: GenericObject<any>): void {
|
|
|
|
this.editor.renderer.scrollCursorIntoView(
|
|
|
|
{
|
|
|
|
row: c.row,
|
|
|
|
column: c.column,
|
|
|
|
},
|
|
|
|
0.5
|
|
|
|
);
|
|
|
|
this.editor.selection.moveTo(
|
|
|
|
c.row,
|
|
|
|
c.column
|
|
|
|
);
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set editor language mode
|
|
|
|
*
|
|
|
|
* The mode object should be in the following format:
|
|
|
|
* ```ts
|
|
|
|
* {
|
|
|
|
* text: string,
|
|
|
|
* mode: string
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @param {GenericObject<any>} m language mode object
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
setMode(m: GenericObject<any>): void {
|
|
|
|
this.currfile.langmode = m;
|
|
|
|
this.editor.getSession().setMode(m.mode);
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get current editor cursor position
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @return {*} {GenericObject<any>}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
protected getCursor(): GenericObject<any> {
|
|
|
|
return this.editor.getCursorPosition();
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create a new UndoManage instance
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @return {*} {GenericObject<any>}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
protected newUndoManager(): GenericObject<any> {
|
|
|
|
return new ace.UndoManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to the editor instance
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @type {GenericObject<any>}
|
|
|
|
* @memberof CodePad
|
|
|
|
*/
|
|
|
|
protected editor: GenericObject<any>;
|
|
|
|
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the editor
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @param {HTMLElement} el editor container DOM
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
protected editorSetup(el: HTMLElement): void {
|
|
|
|
this.editor = ace.edit(el);
|
|
|
|
this.editor.setOptions({
|
|
|
|
enableBasicAutocompletion: true,
|
|
|
|
enableSnippets: true,
|
|
|
|
enableLiveAutocompletion: true,
|
|
|
|
highlightActiveLine: true,
|
|
|
|
highlightSelectedWord: true,
|
|
|
|
behavioursEnabled: true,
|
|
|
|
wrap: true,
|
|
|
|
fontSize: "10pt",
|
|
|
|
showInvisibles: true,
|
|
|
|
});
|
|
|
|
this.editor.setTheme("ace/theme/monokai");
|
|
|
|
this.editor.completers.push({
|
|
|
|
getCompletions(
|
|
|
|
editor: any,
|
|
|
|
session: any,
|
|
|
|
pos: any,
|
|
|
|
prefix: any,
|
|
|
|
callback: any
|
|
|
|
) { },
|
|
|
|
});
|
|
|
|
this.editor.getSession().setUseWrapMode(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register to editor event
|
|
|
|
*
|
|
|
|
* @param {string} evt_str event name
|
|
|
|
* @param {() => void} callback callback function
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
on(evt_str: string, callback: () => void): void {
|
|
|
|
switch (evt_str) {
|
|
|
|
case "input":
|
|
|
|
case "focus":
|
|
|
|
this.editor.on(evt_str, callback);
|
|
|
|
break;
|
|
|
|
case "changeCursor":
|
|
|
|
this.editor
|
|
|
|
.getSession()
|
|
|
|
.selection.on(evt_str, callback);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resize the editor
|
|
|
|
*
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
resize(): void {
|
|
|
|
this.editor.resize();
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focus on the editor
|
|
|
|
*
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
focus(): void {
|
|
|
|
this.editor.focus();
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get language mode from path
|
|
|
|
*
|
|
|
|
* @protected
|
|
|
|
* @param {string} path
|
|
|
|
* @return {*} {GenericObject<any>}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
protected getModeForPath(path: string): GenericObject<any> {
|
|
|
|
const m = this.modes.getModeForPath(path);
|
|
|
|
return {
|
|
|
|
text: m.caption,
|
|
|
|
mode: m.mode
|
|
|
|
}
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the editor status
|
|
|
|
*
|
|
|
|
* @return {*} {GenericObject<any>}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
getEditorStatus(): GenericObject<any> {
|
|
|
|
const c = this.editor.session.selection.getCursor();
|
|
|
|
const l = this.editor.session.getLength();
|
|
|
|
return {
|
|
|
|
row: c.row,
|
|
|
|
column: c.column,
|
|
|
|
line: l,
|
|
|
|
langmode: this.currfile.langmode,
|
|
|
|
file: this.currfile.path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get editor value
|
|
|
|
*
|
|
|
|
* @return {*} {string}
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
getValue(): string {
|
|
|
|
return this.editor.getValue();
|
|
|
|
}
|
2020-12-20 17:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set editor value
|
|
|
|
*
|
|
|
|
* @param {string} value
|
|
|
|
* @memberof CodePadACEModel
|
|
|
|
*/
|
2020-12-20 16:45:51 +01:00
|
|
|
setValue(value: string): void {
|
|
|
|
this.editor.setValue(value, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|