-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): move ace only related logic to separate module
- Loading branch information
Showing
5 changed files
with
130 additions
and
87 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import js_beautify from 'js-beautify'; | ||
import {Ace, edit} from 'ace-builds'; | ||
import 'ace-builds/src-min-noconflict/mode-html'; | ||
import 'ace-builds/src-min-noconflict/theme-chrome'; | ||
import 'ace-builds/src-min-noconflict/theme-tomorrow_night'; | ||
|
||
import {EditorConfig, Theme} from './utils'; | ||
|
||
/** {@link https://ace.c9.io/ Ace} editor wrapper. */ | ||
class AceEditor { | ||
/** Inner {@link https://ace.c9.io/ Ace} instance. */ | ||
private editor: Ace.Editor; | ||
/** Callback to run on editor's content changes. */ | ||
private changeCallback: Function; | ||
/** Default ace editor's options. */ | ||
private static defaultConfig: EditorConfig = { | ||
theme: Theme.Light, | ||
printMarginColumn: -1, | ||
wrap: true, | ||
}; | ||
|
||
/** | ||
* @param {HTMLElement} element - element to place editor inside. | ||
* @param {Function=} changeCallback - callback to run on editor's content changes. | ||
*/ | ||
constructor(element: HTMLElement, changeCallback: Function = () => {}) { | ||
this.editor = edit(element); | ||
this.changeCallback = changeCallback; | ||
this.editor.addEventListener('change', changeCallback); | ||
this.editor.setOption('useWorker', false); | ||
this.editor.setOption('mode', 'ace/mode/html'); | ||
} | ||
|
||
/** Update editor's options. | ||
* @param {Partial<EditorConfig>} config - new options to set. | ||
*/ | ||
public setConfig(config: Partial<EditorConfig>): void { | ||
this.editor.setOptions({ | ||
...AceEditor.defaultConfig, | ||
...config | ||
}); | ||
} | ||
|
||
/** Set editor's text content. | ||
* @param {string} value - text content to set. | ||
*/ | ||
public setValue(value: string): void { | ||
this.editor.removeEventListener('change', this.changeCallback); | ||
this.editor.setValue(js_beautify.html(value), -1); | ||
this.editor.addEventListener('change', this.changeCallback); | ||
} | ||
|
||
/** @returns Editor's text content. */ | ||
public getValue(): string { | ||
return this.editor.getValue(); | ||
} | ||
|
||
/** Manually cleanup internal event listeners. */ | ||
public destroy(): void { | ||
this.editor.removeEventListener('change', this.changeCallback); | ||
} | ||
} | ||
|
||
export type {AceEditor}; | ||
export const Editor = AceEditor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** Config interface to define inner ACE editor settings. */ | ||
export interface EditorConfig { | ||
/** Editor's appearance theme. */ | ||
theme: string; | ||
/** Position of the vertical line for wrapping. */ | ||
printMarginColumn: number; | ||
/** Limit of characters before wrapping. */ | ||
wrap: number | boolean; | ||
} | ||
|
||
/** Enum to match themes to {@link https://github.com/ajaxorg/ace/tree/master/src/theme Ace styles}. */ | ||
export enum Theme { | ||
/** Light editor theme. */ | ||
Light = 'ace/theme/chrome', | ||
/** Dark editor theme. */ | ||
Dark = 'ace/theme/tomorrow_night', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import {UIPHeader} from './header/header'; | ||
import {UIPOptions} from './header/options/options'; | ||
import {UIPSnippets} from './header/snippets/snippets'; | ||
import {UIPEditor} from './editor/editor'; | ||
import {UIPSettings} from './settings/settings'; | ||
import {registeredSettings} from '../registration'; | ||
import {registerAsyncPlugins} from './async-plugins'; | ||
|
||
export {UIPOptions, UIPSettings, UIPSnippets, UIPHeader}; | ||
export {UIPOptions, UIPEditor, UIPSettings, UIPSnippets, UIPHeader}; | ||
|
||
export const registerPlugins = () => { | ||
registerAsyncPlugins(); | ||
UIPHeader.register(); | ||
UIPOptions.register(); | ||
UIPSnippets.register(); | ||
UIPEditor.register(); | ||
registeredSettings().then(() => UIPSettings.register()); | ||
}; |