@aquera/nile-elements 0.0.42-1 → 0.0.42-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Webcomponent nile-elements following open-wc recommendations",
4
4
  "license": "MIT",
5
5
  "author": "nile-elements",
6
- "version": "0.0.42-1",
6
+ "version": "0.0.42-2",
7
7
  "main": "dist/src/index.js",
8
8
  "type": "module",
9
9
  "module": "dist/src/index.js",
@@ -104,4 +104,4 @@
104
104
  "prettier --write"
105
105
  ]
106
106
  }
107
- }
107
+ }
@@ -18,6 +18,7 @@ import { styles } from './nile-code-editor.css';
18
18
  import { EditorView } from 'codemirror';
19
19
  import { ViewUpdate } from '@codemirror/view';
20
20
  import { EditorState, Compartment } from '@codemirror/state';
21
+ import {lineNumbers} from '@codemirror/view';
21
22
  import {
22
23
  javascript,
23
24
  javascriptLanguage,
@@ -80,49 +81,76 @@ export class NileCodeEditor extends NileElement {
80
81
  return code.replace(/\s+/g, ' ').trim();
81
82
  }
82
83
  lineNumbersComp = new Compartment();
84
+ restrictSingleLineComp = new Compartment();
85
+ readOnlyComp = new Compartment();
86
+
83
87
 
84
88
  @watch(['value'], { waitUntilFirstUpdate: true })
85
- handleValueChange() {}
89
+ handleValueChange() { }
86
90
 
87
- updated(changedProperties: PropertyValues) {
88
- super.updated(changedProperties);
89
- if (changedProperties.has('value')) {
90
- const customAutoCompletions = javascriptLanguage.data.of({
91
- autocomplete: scopeCompletionSource(this.customAutoCompletions),
92
- });
93
- if (!this.view) {
94
- let startState = EditorState.create({
95
- doc: !this.multiline
96
- ? this.convertToSingleLine(this.value)
97
- : this.value,
98
- extensions: [
99
- basicSetup({
100
- lineNumbers: !!this.multiline,
91
+ getLineNumbersExension() {
92
+ return this.multiline ? lineNumbers() : [];
93
+ }
94
+
95
+ getReadOnlyExtension() {
96
+ return EditorState.readOnly.of(this.readonly);
97
+ }
98
+
99
+ getSingleLineExtension(){
100
+ return !this.multiline ? EditorState.transactionFilter.of(tr =>
101
+ tr.newDoc.lines > 1 ? [] : tr
102
+ ) : [];
103
+ }
104
+
105
+ firstUpdated() {
106
+ const lineNumbersExtension = this.lineNumbersComp.of(this.getLineNumbersExension());
107
+ const readOnlyExtension = this.readOnlyComp.of(this.getReadOnlyExtension());
108
+ const restrictSingleLineExtension = this.restrictSingleLineComp.of(this.getSingleLineExtension())
109
+ const customAutoCompletions = javascriptLanguage.data.of({
110
+ autocomplete: scopeCompletionSource(this.customAutoCompletions),
111
+ });
112
+ let startState = EditorState.create({
113
+ doc: !this.multiline
114
+ ? this.convertToSingleLine(this.value)
115
+ : this.value,
116
+ extensions: [
117
+ basicSetup({
118
+ lineNumbers: false,
101
119
  highlightActiveLine: false,
102
- foldGutter: !!this.multiline,
103
- }),
104
- EditorState.readOnly.of(this.readonly),
105
- customAutoCompletions,
106
- autocompletion(),
107
- javascript(),
108
- this.setTheme(),
109
- !this.multiline ? this.restrictSingleLine() : [],
110
- EditorView.updateListener.of((v: ViewUpdate) => {
111
- if (v.docChanged) {
112
- this.emitValues(this.view.state.doc.toString());
113
- }
120
+ foldGutter: false,
114
121
  }),
115
- ],
116
- });
117
-
118
- this.view = new EditorView({
119
- state: startState,
120
- parent: this.codeEditor,
121
- });
122
- } else {
123
- if (this.updateValue) {
124
- // Editor has already been initialized, update its state
125
- this.view.dispatch({
122
+ lineNumbersExtension,
123
+ readOnlyExtension,
124
+ restrictSingleLineExtension,
125
+ customAutoCompletions,
126
+ autocompletion(),
127
+ javascript(),
128
+ this.setTheme(),
129
+ EditorView.updateListener.of((v: ViewUpdate) => {
130
+ if (v.docChanged) {
131
+ this.emitValues(this.view.state.doc.toString());
132
+ }
133
+ }),
134
+
135
+ ],
136
+ });
137
+
138
+ this.view = new EditorView({
139
+ state: startState,
140
+ parent: this.codeEditor,
141
+ });
142
+ this.addOpenListeners();
143
+ }
144
+
145
+
146
+ addOpenListeners() {
147
+ this.view.contentDOM.addEventListener('blur', () => this.toggleFocusAndBlur('blur'));
148
+ this.view.contentDOM.addEventListener('focus', () => this.toggleFocusAndBlur('focus'));
149
+ }
150
+
151
+
152
+ singleLineMultiLineToggle() {
153
+ this.view.dispatch({
126
154
  changes: {
127
155
  from: 0,
128
156
  to: this.view.state.doc.length,
@@ -131,11 +159,35 @@ export class NileCodeEditor extends NileElement {
131
159
  : this.value,
132
160
  },
133
161
  });
162
+ }
163
+
164
+ updated(changedProperties: PropertyValues) {
165
+ super.updated(changedProperties);
166
+ if (changedProperties.has('value') && this.updateValue) {
167
+ // Editor has already been initialized, update its state
168
+ this.singleLineMultiLineToggle();
134
169
  }
135
- }
170
+ if (changedProperties.has('multiline')) {
171
+ this.view.dispatch({
172
+ effects: [this.lineNumbersComp.reconfigure(this.getLineNumbersExension()),
173
+ this.restrictSingleLineComp.reconfigure(this.getSingleLineExtension())],
174
+ })
175
+ this.singleLineMultiLineToggle();
176
+ }
177
+ if (changedProperties.has('readonly')) {
178
+ this.view.dispatch({ effects: this.readOnlyComp.reconfigure(this.getReadOnlyExtension()) })
136
179
  }
137
180
  }
138
181
 
182
+ toggleFocusAndBlur(action: 'blur' | 'focus') {
183
+ if(action === 'blur'){
184
+ this.emit('nile-blur', { onBlur : true });
185
+ }
186
+ if (action === 'focus') {
187
+ this.emit('nile-focus', { onFocus : true });
188
+ }
189
+ }
190
+
139
191
  setTheme() {
140
192
  return EditorView.theme(Theme);
141
193
  }