bullet_train-fields 1.19.2 → 1.20.0
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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2656a1e9663726891c41452518e8ca12907980b07f1e085597c2591a454cb361
|
4
|
+
data.tar.gz: 212e25792745b3fda7da34b55a235526aa0979d75832d7294651173388920263
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ff7f8a7f606e0d1811e93412a27a604332e350355c696fa9df72e00b203367bfa54887b7eb38ab8fd3a2a701171c72ea7da543d9de9e9a94ff2aa68011893ce
|
7
|
+
data.tar.gz: 1e77d47ca39501d6c2545877f0a303416d4005382989531e9d90dda7aadb6c00e2c88b6a034d75adefb60f32ed7ef896f4bef9cc0ac72ea5acb0d049976ba239
|
@@ -0,0 +1,182 @@
|
|
1
|
+
import { Controller } from '@hotwired/stimulus'
|
2
|
+
import { version as MONACO_VERSION } from 'monaco-editor/package.json'
|
3
|
+
|
4
|
+
const MONACO_LOADED_EVENT = 'monaco:loaded'
|
5
|
+
const MONACO_ERROR_EVENT = 'monaco:error'
|
6
|
+
export default class extends Controller {
|
7
|
+
static targets = ['plainField', 'codeField']
|
8
|
+
|
9
|
+
static values = {
|
10
|
+
language: {
|
11
|
+
type: String,
|
12
|
+
default: 'javascript' // for all supported languages, see https://github.com/microsoft/monaco-editor/tree/main/src/basic-languages
|
13
|
+
},
|
14
|
+
themeLight: {
|
15
|
+
type: String,
|
16
|
+
default: 'vs'
|
17
|
+
},
|
18
|
+
themeDark: {
|
19
|
+
type: String,
|
20
|
+
default: 'vs-dark'
|
21
|
+
},
|
22
|
+
monacoConfig: {
|
23
|
+
type: Object,
|
24
|
+
default: {}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
connect() {
|
29
|
+
this.loadEditorFramework()
|
30
|
+
this.watchColorScheme()
|
31
|
+
}
|
32
|
+
|
33
|
+
disconnect() {
|
34
|
+
this.teardownCodeEditor()
|
35
|
+
this.unwatchColorScheme()
|
36
|
+
}
|
37
|
+
|
38
|
+
initCodeEditor() {
|
39
|
+
if (!this.hasPlainFieldTarget || !this.hasCodeFieldTarget) return
|
40
|
+
if (!this.monacoInstance) return
|
41
|
+
|
42
|
+
this.codeEditor = this.monacoInstance.editor.create(this.codeFieldTarget, {
|
43
|
+
value: this.plainFieldTarget.value,
|
44
|
+
language: this.languageValue,
|
45
|
+
...this.editorConfig
|
46
|
+
})
|
47
|
+
|
48
|
+
this.codeEditor.onDidChangeModelContent(() => {
|
49
|
+
this.updatePlainField()
|
50
|
+
}) // teardownCodeEditor should properly unregister this event handler
|
51
|
+
}
|
52
|
+
|
53
|
+
updatePlainField() {
|
54
|
+
this.plainFieldTarget.value = this.codeEditor.getValue()
|
55
|
+
this.plainFieldTarget.dispatchEvent(new Event('input', { bubbles: true }))
|
56
|
+
}
|
57
|
+
|
58
|
+
get editorConfig() {
|
59
|
+
return {
|
60
|
+
automaticLayout: true,
|
61
|
+
theme: this.theme,
|
62
|
+
minimap: { enabled: false },
|
63
|
+
scrollBeyondLastLine: false,
|
64
|
+
...this.monacoConfigValue
|
65
|
+
}
|
66
|
+
}
|
67
|
+
|
68
|
+
showPlainField() {
|
69
|
+
this.plainFieldTarget.classList.remove('hidden')
|
70
|
+
this.codeFieldTarget.classList.add('hidden')
|
71
|
+
}
|
72
|
+
|
73
|
+
teardownCodeEditor() {
|
74
|
+
if (this.codeEditor) {
|
75
|
+
this.codeEditor.dispose()
|
76
|
+
this.codeEditor = null
|
77
|
+
}
|
78
|
+
document.removeEventListener(MONACO_LOADED_EVENT, this.initCodeEditor)
|
79
|
+
document.removeEventListener(MONACO_ERROR_EVENT, this.showPlainField)
|
80
|
+
}
|
81
|
+
|
82
|
+
loadEditorFramework() {
|
83
|
+
if (!this.hasPlainFieldTarget || !this.hasCodeFieldTarget) return
|
84
|
+
|
85
|
+
// Listen for Monaco load event
|
86
|
+
this.initCodeEditor = this.initCodeEditor.bind(this)
|
87
|
+
this.showPlainField = this.showPlainField.bind(this)
|
88
|
+
document.addEventListener(MONACO_LOADED_EVENT, this.initCodeEditor)
|
89
|
+
document.addEventListener(MONACO_ERROR_EVENT, this.showPlainField)
|
90
|
+
try {
|
91
|
+
if (this.monacoInstance) {
|
92
|
+
this.initCodeEditor()
|
93
|
+
return
|
94
|
+
}
|
95
|
+
|
96
|
+
this.ensureMonacoLoaded()
|
97
|
+
} catch (error) {
|
98
|
+
console.error('Monaco failed to load:', error)
|
99
|
+
this.showPlainField()
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
// if you want to include the monaco editor directly, overload this method in the subclass
|
104
|
+
// and monacoInstance() getter
|
105
|
+
ensureMonacoLoaded() {
|
106
|
+
// Check for existing script
|
107
|
+
const existingScript = document.querySelector('script[data-monaco-loader]')
|
108
|
+
if (!existingScript) {
|
109
|
+
const script = document.createElement('script')
|
110
|
+
script.src = `https://cdn.jsdelivr.net/npm/monaco-editor@${MONACO_VERSION}/min/vs/loader.js`
|
111
|
+
script.dataset.monacoLoader = ''
|
112
|
+
|
113
|
+
script.onload = () => {
|
114
|
+
require.config({
|
115
|
+
paths: {
|
116
|
+
vs: `https://cdn.jsdelivr.net/npm/monaco-editor@${MONACO_VERSION}/min/vs`
|
117
|
+
}
|
118
|
+
})
|
119
|
+
|
120
|
+
require(['vs/editor/editor.main'], (monaco) => {
|
121
|
+
window.monacoInstance = monaco
|
122
|
+
document.dispatchEvent(new Event(MONACO_LOADED_EVENT))
|
123
|
+
})
|
124
|
+
}
|
125
|
+
|
126
|
+
script.onerror = () => {
|
127
|
+
try {
|
128
|
+
document.dispatchEvent(new Event(MONACO_ERROR_EVENT))
|
129
|
+
} catch (error) {
|
130
|
+
console.warn(
|
131
|
+
'script failed to load and code editor is no longer available',
|
132
|
+
error
|
133
|
+
)
|
134
|
+
}
|
135
|
+
}
|
136
|
+
|
137
|
+
document.head.appendChild(script)
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
// if you want to include the monaco editor directly, overload this method in the subclass
|
142
|
+
// and ensureMonacoLoaded() above
|
143
|
+
get monacoInstance() {
|
144
|
+
return window.monacoInstance
|
145
|
+
}
|
146
|
+
|
147
|
+
watchColorScheme() {
|
148
|
+
this.userPrefersDarkSchemeQuery = window.matchMedia(
|
149
|
+
'(prefers-color-scheme: dark)'
|
150
|
+
)
|
151
|
+
this.userPrefersDarkSchemeQuery.addEventListener(
|
152
|
+
'change',
|
153
|
+
this.updateTheme.bind(this)
|
154
|
+
)
|
155
|
+
}
|
156
|
+
|
157
|
+
unwatchColorScheme() {
|
158
|
+
if (this.userPrefersDarkSchemeQuery === undefined) return
|
159
|
+
this.userPrefersDarkSchemeQuery.removeEventListener(
|
160
|
+
'change',
|
161
|
+
this.updateTheme.bind(this)
|
162
|
+
)
|
163
|
+
this.userPrefersDarkSchemeQuery = undefined
|
164
|
+
}
|
165
|
+
|
166
|
+
updateTheme() {
|
167
|
+
this.codeEditor?.updateOptions({ theme: this.theme })
|
168
|
+
}
|
169
|
+
|
170
|
+
get theme() {
|
171
|
+
return this.userPrefersDarkScheme
|
172
|
+
? this.themeDarkValue
|
173
|
+
: this.themeLightValue
|
174
|
+
}
|
175
|
+
|
176
|
+
get userPrefersDarkScheme() {
|
177
|
+
if (this.userPrefersDarkSchemeQuery === undefined) {
|
178
|
+
return false
|
179
|
+
}
|
180
|
+
return this.userPrefersDarkSchemeQuery.matches
|
181
|
+
}
|
182
|
+
}
|
@@ -13,6 +13,7 @@ import PhoneController from './fields/phone_controller'
|
|
13
13
|
import SuperSelectController from './fields/super_select_controller'
|
14
14
|
import DependableController from './dependable_controller'
|
15
15
|
import DependentFieldsFrameController from './dependent_fields_frame_controller'
|
16
|
+
import CodeEditorController from './fields/code_editor_controller'
|
16
17
|
|
17
18
|
export const controllerDefinitions = [
|
18
19
|
[FieldController, 'fields/field_controller.js'],
|
@@ -28,6 +29,7 @@ export const controllerDefinitions = [
|
|
28
29
|
[SuperSelectController, 'fields/super_select_controller.js'],
|
29
30
|
[DependableController, 'dependable_controller.js'],
|
30
31
|
[DependentFieldsFrameController, 'dependent_fields_frame_controller.js'],
|
32
|
+
[CodeEditorController, 'fields/code_editor_controller.js'],
|
31
33
|
].map(function(d) {
|
32
34
|
const key = d[1]
|
33
35
|
const controller = d[0]
|
@@ -51,4 +53,5 @@ export {
|
|
51
53
|
SuperSelectController,
|
52
54
|
DependableController,
|
53
55
|
DependentFieldsFrameController,
|
56
|
+
CodeEditorController,
|
54
57
|
}
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bullet_train-fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.20.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Culver
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: standard
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- app/javascript/controllers/dependent_fields_frame_controller.js
|
127
127
|
- app/javascript/controllers/fields/button_toggle_controller.js
|
128
128
|
- app/javascript/controllers/fields/cloudinary_image_controller.js
|
129
|
+
- app/javascript/controllers/fields/code_editor_controller.js
|
129
130
|
- app/javascript/controllers/fields/color_picker_controller.js
|
130
131
|
- app/javascript/controllers/fields/date_controller.js
|
131
132
|
- app/javascript/controllers/fields/emoji_picker_controller.js
|
@@ -163,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
164
|
- !ruby/object:Gem::Version
|
164
165
|
version: '0'
|
165
166
|
requirements: []
|
166
|
-
rubygems_version: 3.6.
|
167
|
+
rubygems_version: 3.6.7
|
167
168
|
specification_version: 4
|
168
169
|
summary: Bullet Train Fields
|
169
170
|
test_files: []
|