Skip to content

Commit d9b35f2

Browse files
authored
Merge pull request #166 from Zhaocl1997/phrases
feat: phrases prop
2 parents 333f178 + b649daa commit d9b35f2

File tree

4 files changed

+137
-74
lines changed

4 files changed

+137
-74
lines changed

dev/App.vue

Lines changed: 120 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,88 @@
11
<script setup lang="ts">
2-
import { reactive, shallowRef, computed, onMounted } from 'vue'
3-
import { javascript } from '@codemirror/lang-javascript'
4-
import { html } from '@codemirror/lang-html'
5-
import { json } from '@codemirror/lang-json'
6-
import { markdown } from '@codemirror/lang-markdown'
7-
import { oneDark } from '@codemirror/theme-one-dark'
8-
import { Codemirror } from '../src'
2+
import { reactive, shallowRef, computed, onMounted } from 'vue'
3+
import { javascript } from '@codemirror/lang-javascript'
4+
import { html } from '@codemirror/lang-html'
5+
import { json } from '@codemirror/lang-json'
6+
import { markdown } from '@codemirror/lang-markdown'
7+
import { oneDark } from '@codemirror/theme-one-dark'
8+
import { Codemirror } from '../src'
99
10-
const themes: any = { oneDark }
11-
const languages: any = {
12-
javascript: javascript(),
13-
html: html(),
14-
json: json(),
15-
markdown: markdown()
16-
}
10+
const themes: any = { oneDark }
11+
const languages: any = {
12+
javascript: javascript(),
13+
html: html(),
14+
json: json(),
15+
markdown: markdown()
16+
}
17+
18+
const consoleLog = console.log
19+
const code = shallowRef(`console.log('Hello World')`)
20+
const view = shallowRef()
21+
const state = reactive({
22+
disabled: false,
23+
indentWithTab: true,
24+
tabSize: 4,
25+
autofocus: true,
26+
placeholder: 'input...',
27+
backgroundColor: 'red',
28+
language: 'javascript',
29+
theme: 'oneDark',
30+
phrases: 'en-us'
31+
})
1732
18-
const consoleLog = console.log
19-
const code = shallowRef(`console.log('Hello World')`)
20-
const view = shallowRef()
21-
const state = reactive({
22-
disabled: false,
23-
indentWithTab: true,
24-
tabSize: 4,
25-
autofocus: true,
26-
placeholder: 'input...',
27-
backgroundColor: 'red',
28-
language: 'javascript',
29-
theme: 'oneDark'
30-
})
33+
const handleReady = (payload: any) => {
34+
console.log('handleReady payload:', payload)
35+
}
3136
32-
const handleReady = (payload: any) => {
33-
console.log('handleReady payload:', payload)
37+
const extensions = computed(() => {
38+
const result = []
39+
result.push(languages[state.language])
40+
if (themes[state.theme]) {
41+
result.push(themes[state.theme])
3442
}
43+
return result
44+
})
3545
36-
const extensions = computed(() => {
37-
const result = []
38-
result.push(languages[state.language])
39-
if (themes[state.theme]) {
40-
result.push(themes[state.theme])
41-
}
42-
return result
43-
})
46+
onMounted(() => {
47+
console.log('mounted view:', view)
48+
})
4449
45-
onMounted(() => {
46-
console.log('mounted view:', view)
47-
})
50+
const germanPhrases = {
51+
// @codemirror/view
52+
'Control character': 'Steuerzeichen',
53+
// @codemirror/commands
54+
'Selection deleted': 'Auswahl gelöscht',
55+
// @codemirror/language
56+
'Folded lines': 'Eingeklappte Zeilen',
57+
'Unfolded lines': 'Ausgeklappte Zeilen',
58+
to: 'bis',
59+
'folded code': 'eingeklappter Code',
60+
unfold: 'ausklappen',
61+
'Fold line': 'Zeile einklappen',
62+
'Unfold line': 'Zeile ausklappen',
63+
// @codemirror/search
64+
'Go to line': 'Springe zu Zeile',
65+
go: 'OK',
66+
Find: 'Suchen',
67+
Replace: 'Ersetzen',
68+
next: 'nächste',
69+
previous: 'vorherige',
70+
all: 'alle',
71+
'match case': 'groß/klein beachten',
72+
'by word': 'ganze Wörter',
73+
replace: 'ersetzen',
74+
'replace all': 'alle ersetzen',
75+
close: 'schließen',
76+
'current match': 'aktueller Treffer',
77+
'replaced $ matches': '$ Treffer ersetzt',
78+
'replaced match on line $': 'Treffer on Zeile $ ersetzt',
79+
'on line': 'auf Zeile',
80+
// @codemirror/autocomplete
81+
Completions: 'Vervollständigungen',
82+
// @codemirror/lint
83+
Diagnostics: 'Diagnosen',
84+
'No diagnostics': 'Keine Diagnosen'
85+
}
4886
</script>
4987

5088
<template>
@@ -97,6 +135,14 @@
97135
</option>
98136
</select>
99137
</p>
138+
<p>
139+
<label for="phrases">phrases:</label>
140+
<select name="phrases" id="phrases" v-model="state.phrases">
141+
<option :value="option" :key="option" v-for="option in ['en-us', 'de-de']">
142+
{{ option }}
143+
</option>
144+
</select>
145+
</p>
100146
</div>
101147
</div>
102148
<div class="content">
@@ -111,6 +157,7 @@
111157
:disabled="state.disabled"
112158
:style="{ backgroundColor: state.backgroundColor }"
113159
:extensions="extensions"
160+
:phrases="state.phrases === 'en-us' ? {} : germanPhrases"
114161
v-model="code"
115162
@ready="handleReady"
116163
@change="consoleLog('change', $event)"
@@ -122,47 +169,47 @@
122169
</template>
123170

124171
<style lang="scss">
125-
body {
126-
margin: 0;
127-
}
172+
body {
173+
margin: 0;
174+
}
175+
176+
.example {
177+
width: 100vw;
178+
height: 100vh;
179+
display: flex;
180+
flex-direction: column;
181+
justify-content: center;
182+
align-items: center;
128183
129-
.example {
130-
width: 100vw;
131-
height: 100vh;
184+
.toolbar {
132185
display: flex;
133-
flex-direction: column;
134-
justify-content: center;
135186
align-items: center;
187+
margin-bottom: 1rem;
136188
137-
.toolbar {
138-
display: flex;
139-
align-items: center;
140-
margin-bottom: 1rem;
141-
142-
.state {
143-
margin: 2rem 0;
144-
margin-right: 2rem;
145-
padding: 2em;
146-
border: 1px solid #ccc;
147-
}
189+
.state {
190+
margin: 2rem 0;
191+
margin-right: 2rem;
192+
padding: 2em;
193+
border: 1px solid #ccc;
148194
}
195+
}
149196
150-
.content {
151-
display: flex;
152-
width: 100%;
153-
justify-content: center;
197+
.content {
198+
display: flex;
199+
width: 100%;
200+
justify-content: center;
154201
155-
.code {
156-
overflow: scroll;
157-
}
202+
.code {
203+
overflow: scroll;
204+
}
158205
159-
.code,
160-
.codemirror .cm-editor {
161-
width: 30vw;
162-
height: 50vh;
163-
margin: 0 1rem;
164-
border: 1px solid #ddd;
165-
}
206+
.code,
207+
.codemirror .cm-editor {
208+
width: 30vw;
209+
height: 50vh;
210+
margin: 0 1rem;
211+
border: 1px solid #ddd;
166212
}
167213
}
214+
}
168215
</style>

src/codemirror.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ export const getEditorTools = (view: EditorView) => {
9696
reTabSize([EditorState.tabSize.of(tabSize), indentUnit.of(' '.repeat(tabSize))])
9797
}
9898

99+
// phrases
100+
// https://codemirror.net/examples/translate/
101+
const { run: rePhrases } = createEditorCompartment(view)
102+
const setPhrases = (phrases: Record<string, string>) => {
103+
rePhrases([EditorState.phrases.of(phrases)])
104+
}
105+
99106
// set editor's placeholder
100107
const { run: rePlaceholder } = createEditorCompartment(view)
101108
const setPlaceholder = (value: string) => {
@@ -117,6 +124,7 @@ export const getEditorTools = (view: EditorView) => {
117124
toggleDisabled,
118125
toggleIndentWithTab,
119126
setTabSize,
127+
setPhrases,
120128
setPlaceholder,
121129
setStyle
122130
}

src/component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ export default defineComponent({
9797
{ immediate: true }
9898
)
9999

100+
// watch prop.phrases
101+
watch(
102+
() => config.value.phrases,
103+
(phrases) => editorTools.setPhrases(phrases!),
104+
{ immediate: true }
105+
)
106+
100107
// watch prop.placeholder
101108
watch(
102109
() => config.value.placeholder,

src/props.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const configProps = {
1919
// codemirror options
2020
root: Object as PropType<ShadowRoot | Document>,
2121
extensions: Array as PropType<EditorStateConfig['extensions']>,
22-
selection: Object as PropType<EditorStateConfig['selection']>
22+
selection: Object as PropType<EditorStateConfig['selection']>,
23+
phrases: Object
2324
}
2425

2526
export const modelValueProp = {

0 commit comments

Comments
 (0)