This repository has been archived by the owner on Apr 26, 2019. It is now read-only.
forked from LasaleFamine/ace-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathace-editor.es6.js
142 lines (127 loc) · 3.32 KB
/
ace-editor.es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
const Polymer = window.Polymer
const CustomEvent = window.CustomEvent
class aceEditor {
// Define behaviors with a getter.
get behaviors () {
return []
}
// Element setup goes in beforeRegister instead of createdCallback.
beforeRegister () {
this.is = 'ace-editor'
// Define the properties object in beforeRegister.
this.properties = {
/** Ace istance **/
Ace: {
type: Function
},
uniqueId: {
type: String,
value: 'aceEditorPolymerLibLoader'
},
version: {
type: String,
value: '1.2.5'
},
mode: {
type: String,
value: 'html'
},
theme: {
type: String,
value: 'monokai'
},
_isAceInit: {
type: Boolean,
value: false
}
}
}
// onReady fill <lib-loader> component to load Ace.js from CDN
ready () {
this._computeLibLink()
this._computeUniqueId()
}
/**
* {function} setTheme set the choosen theme
* {string} theme Name of the theme to set
**/
setTheme (theme) {
this.Ace.setTheme('ace/theme/' + this.theme)
this.set('theme', theme)
}
/**
* {function} setMode set the choosen mode
* {string} mode Name of the mode to set
**/
setMode (mode) {
this.Ace.getSession().setMode('ace/mode/' + this.mode)
this.set('mode', mode)
}
/**
*
* {function} setContent set the passed content
* {string} content Content that will override the current content
**/
setContent (content) {
this.Ace.setValue(content, 1)
}
/**
*
* {function} appendContent append the passed content
* {string} content Content that will be appended to the after the current content
**/
appendContent (content) {
this.Ace.setValue(this.Ace.getValue() + ' ' + content, 1)
}
/**
* {function} getContent get all the content
* {string} return content getted from the editor
**/
getContent () {
return this.Ace.getValue()
}
/** ===============
* Private methods
**/
_computeLibLink () {
this.$.loaderAce.set('lib', `https://cdnjs.cloudflare.com/ajax/libs/ace/${this.version}/ace.js`)
}
_computeUniqueId () {
this.$.loaderAce.set('libUniqueId', this.uniqueId)
}
_initAce () {
this.Ace = window.ace.edit(this.$.editor)
this.Ace.$blockScrolling = Infinity
this._initListeners()
this.Ace.setTheme('ace/theme/' + this.theme)
this.Ace.getSession().setMode('ace/mode/' + this.mode)
this._isAceInit = true
this.dispatchEvent(new CustomEvent('ace-ready'))
}
_initListeners () {
this.Ace.on('change', (e) => {
this.dispatchEvent(new CustomEvent('change'))
})
this.Ace.on('blur', (e) => {
this.dispatchEvent(new CustomEvent('blur'))
})
this.Ace.on('changeSelectionStyle', (e) => {
this.dispatchEvent(new CustomEvent('change-selection-style'))
})
this.Ace.on('changeSession', (e) => {
this.dispatchEvent(new CustomEvent('change-session'))
})
this.Ace.on('copy', (e) => {
this.dispatchEvent(new CustomEvent('copy'))
})
this.Ace.on('focus', (e) => {
this.dispatchEvent(new CustomEvent('focus'))
})
this.Ace.on('paste', (e) => {
this.dispatchEvent(new CustomEvent('paste'))
})
}
}
// Register the element using Polymer's constructor.
Polymer(aceEditor)