Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: phrases prop #166

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 120 additions & 73 deletions dev/App.vue
Original file line number Diff line number Diff line change
@@ -1,50 +1,88 @@
<script setup lang="ts">
import { reactive, shallowRef, computed, onMounted } from 'vue'
import { javascript } from '@codemirror/lang-javascript'
import { html } from '@codemirror/lang-html'
import { json } from '@codemirror/lang-json'
import { markdown } from '@codemirror/lang-markdown'
import { oneDark } from '@codemirror/theme-one-dark'
import { Codemirror } from '../src'
import { reactive, shallowRef, computed, onMounted } from 'vue'
import { javascript } from '@codemirror/lang-javascript'
import { html } from '@codemirror/lang-html'
import { json } from '@codemirror/lang-json'
import { markdown } from '@codemirror/lang-markdown'
import { oneDark } from '@codemirror/theme-one-dark'
import { Codemirror } from '../src'

const themes: any = { oneDark }
const languages: any = {
javascript: javascript(),
html: html(),
json: json(),
markdown: markdown()
}
const themes: any = { oneDark }
const languages: any = {
javascript: javascript(),
html: html(),
json: json(),
markdown: markdown()
}

const consoleLog = console.log
const code = shallowRef(`console.log('Hello World')`)
const view = shallowRef()
const state = reactive({
disabled: false,
indentWithTab: true,
tabSize: 4,
autofocus: true,
placeholder: 'input...',
backgroundColor: 'red',
language: 'javascript',
theme: 'oneDark',
phrases: 'en-us'
})

const consoleLog = console.log
const code = shallowRef(`console.log('Hello World')`)
const view = shallowRef()
const state = reactive({
disabled: false,
indentWithTab: true,
tabSize: 4,
autofocus: true,
placeholder: 'input...',
backgroundColor: 'red',
language: 'javascript',
theme: 'oneDark'
})
const handleReady = (payload: any) => {
console.log('handleReady payload:', payload)
}

const handleReady = (payload: any) => {
console.log('handleReady payload:', payload)
const extensions = computed(() => {
const result = []
result.push(languages[state.language])
if (themes[state.theme]) {
result.push(themes[state.theme])
}
return result
})

const extensions = computed(() => {
const result = []
result.push(languages[state.language])
if (themes[state.theme]) {
result.push(themes[state.theme])
}
return result
})
onMounted(() => {
console.log('mounted view:', view)
})

onMounted(() => {
console.log('mounted view:', view)
})
const germanPhrases = {
// @codemirror/view
'Control character': 'Steuerzeichen',
// @codemirror/commands
'Selection deleted': 'Auswahl gelöscht',
// @codemirror/language
'Folded lines': 'Eingeklappte Zeilen',
'Unfolded lines': 'Ausgeklappte Zeilen',
to: 'bis',
'folded code': 'eingeklappter Code',
unfold: 'ausklappen',
'Fold line': 'Zeile einklappen',
'Unfold line': 'Zeile ausklappen',
// @codemirror/search
'Go to line': 'Springe zu Zeile',
go: 'OK',
Find: 'Suchen',
Replace: 'Ersetzen',
next: 'nächste',
previous: 'vorherige',
all: 'alle',
'match case': 'groß/klein beachten',
'by word': 'ganze Wörter',
replace: 'ersetzen',
'replace all': 'alle ersetzen',
close: 'schließen',
'current match': 'aktueller Treffer',
'replaced $ matches': '$ Treffer ersetzt',
'replaced match on line $': 'Treffer on Zeile $ ersetzt',
'on line': 'auf Zeile',
// @codemirror/autocomplete
Completions: 'Vervollständigungen',
// @codemirror/lint
Diagnostics: 'Diagnosen',
'No diagnostics': 'Keine Diagnosen'
}
</script>

<template>
Expand Down Expand Up @@ -97,6 +135,14 @@
</option>
</select>
</p>
<p>
<label for="phrases">phrases:</label>
<select name="phrases" id="phrases" v-model="state.phrases">
<option :value="option" :key="option" v-for="option in ['en-us', 'de-de']">
{{ option }}
</option>
</select>
</p>
</div>
</div>
<div class="content">
Expand All @@ -111,6 +157,7 @@
:disabled="state.disabled"
:style="{ backgroundColor: state.backgroundColor }"
:extensions="extensions"
:phrases="state.phrases === 'en-us' ? {} : germanPhrases"
v-model="code"
@ready="handleReady"
@change="consoleLog('change', $event)"
Expand All @@ -122,47 +169,47 @@
</template>

<style lang="scss">
body {
margin: 0;
}
body {
margin: 0;
}

.example {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;

.example {
width: 100vw;
height: 100vh;
.toolbar {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-bottom: 1rem;

.toolbar {
display: flex;
align-items: center;
margin-bottom: 1rem;

.state {
margin: 2rem 0;
margin-right: 2rem;
padding: 2em;
border: 1px solid #ccc;
}
.state {
margin: 2rem 0;
margin-right: 2rem;
padding: 2em;
border: 1px solid #ccc;
}
}

.content {
display: flex;
width: 100%;
justify-content: center;
.content {
display: flex;
width: 100%;
justify-content: center;

.code {
overflow: scroll;
}
.code {
overflow: scroll;
}

.code,
.codemirror .cm-editor {
width: 30vw;
height: 50vh;
margin: 0 1rem;
border: 1px solid #ddd;
}
.code,
.codemirror .cm-editor {
width: 30vw;
height: 50vh;
margin: 0 1rem;
border: 1px solid #ddd;
}
}
}
</style>
8 changes: 8 additions & 0 deletions src/codemirror.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ export const getEditorTools = (view: EditorView) => {
reTabSize([EditorState.tabSize.of(tabSize), indentUnit.of(' '.repeat(tabSize))])
}

// phrases
// https://codemirror.net/examples/translate/
const { run: rePhrases } = createEditorCompartment(view)
const setPhrases = (phrases: Record<string, string>) => {
rePhrases([EditorState.phrases.of(phrases)])
}

// set editor's placeholder
const { run: rePlaceholder } = createEditorCompartment(view)
const setPlaceholder = (value: string) => {
Expand All @@ -117,6 +124,7 @@ export const getEditorTools = (view: EditorView) => {
toggleDisabled,
toggleIndentWithTab,
setTabSize,
setPhrases,
setPlaceholder,
setStyle
}
Expand Down
7 changes: 7 additions & 0 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export default defineComponent({
{ immediate: true }
)

// watch prop.phrases
watch(
() => config.value.phrases,
(phrases) => editorTools.setPhrases(phrases!),
{ immediate: true }
)

// watch prop.placeholder
watch(
() => config.value.placeholder,
Expand Down
3 changes: 2 additions & 1 deletion src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export const configProps = {
// codemirror options
root: Object as PropType<ShadowRoot | Document>,
extensions: Array as PropType<EditorStateConfig['extensions']>,
selection: Object as PropType<EditorStateConfig['selection']>
selection: Object as PropType<EditorStateConfig['selection']>,
phrases: Object
}

export const modelValueProp = {
Expand Down