Vue

[Vue3] Code Mirror 적용하기

jjineei 2024. 5. 29. 09:26

vue3에선 아래 npm모듈로 편리하게 v-model을 활용하면서 양방향 데이터 전달이 가능하다

 

vue-codemirror

CodeMirror code editor component for Vue. Latest version: 6.1.1, last published: 2 years ago. Start using vue-codemirror in your project by running `npm i vue-codemirror`. There are 543 other projects in the npm registry using vue-codemirror.

www.npmjs.com

1. npm install vue-codemirror

2. main.js에 import

import VueCodemirror from 'vue-codemirror';

const app = createApp(App);
app.use(VueCodemirror, {
	// optional default global options
	autofocus: true,
	disabled: false,
	indentWithTab: true,
	tabSize: 2,
	extensions: [basicSetup],
	// ...
});​

 

3. 컴포넌트 사용

 

- template 영역

<codemirror
    v-model="query"
    autoDestroy
    ref="editor"
    placeholder="Query goes here..."
    :autofocus="true"
    :indent-with-tab="true"
    :tab-size="2"
    :extensions="extensions"
    @ready="handleReady"
/>

 

- script영역

const extensions = [sql(), ayuLight, autocompletion({ override: [customCompletion] })];

const handleReady = payload => {
	const view = shallowRef();
	view.value = payload.view;
};

 

- 자동완성 (auto completion)

const sqlCommands = [
	'select',
	'from',
	'where',
	'insert into',
	'update',
	'delete from',
	'group by',
	'order by',
	'join',
	'inner join',
	'left join',
	'right join',
	'full outer join',
	'union',
	'union all',
	'having',
	'like',
	'distinct',
	'limit',
];
const sqlCommandObj = sqlCommands.map(command => {
	return { label: command, type: 'keyword' };
});
const completions = sqlCommandObj;

function customCompletion(context) {
	let before = context.matchBefore(/\w+/);
	if (!context.explicit && !before) return null;
	return {
		from: before ? before.from : context.pos,
		options: completions.value,
		validFor: /^\w*$/,
	};
}

 

** 추가. code mirror 테마 적용

1. npm install thememirror

https://www.npmjs.com/package/thememirror

 

thememirror

Beautiful themes for CodeMirror. Latest version: 2.0.1, last published: 2 years ago. Start using thememirror in your project by running `npm i thememirror`. There are 17 other projects in the npm registry using thememirror.

www.npmjs.com

2. 아래 사이트에서 사용하고싶은 테마명 import 

 

ThemeMirror

Free and open source themes for CodeMirror

thememirror.net

import { ayuLight } from 'thememirror';

const extensions = [sql(), ayuLight, autocompletion({ override: [customCompletion] })];

 

 

'Vue' 카테고리의 다른 글

[Vue3] Quasar Framework Boot File  (0) 2023.03.29
[Vue] ESLint error : multi-word-component-name  (0) 2023.03.06
SPA와 MPA  (0) 2023.02.15
[Vue] Jest 환경설정  (0) 2023.02.14
[Vue] Service port 변경  (0) 2023.02.14