Skip to content

Commit 4735df8

Browse files
authored
add support for v-model and some tests (#10)
1 parent 83188c4 commit 4735df8

File tree

8 files changed

+1448
-89
lines changed

8 files changed

+1448
-89
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,24 @@ OR:
106106

107107
| Name | Type | Default | Options | Description |
108108
| -------------------- | --------- | ------- | ------------------------------------ | ----------------------------------------------------- |
109+
| v-model | `string` | - | - | for the `code` prop below |
109110
| code | `string` | `""` | - | the code |
110111
| language | `String` | `"js"` | `vue,html,md,ts` + Prismjs Languages | language of the code |
111112
| lineNumbers | `Boolean` | `false` | - | Whether to show line numbers or not |
112113
| readonly | `Boolean` | `false` | - | Indicates if the editor is read only or not. |
113-
| emitEvents | `Boolean` | `false` | - | Indicates if the editor should emit events. |
114+
| emitEvents | `Boolean` | `false` | - | Indicates if the editor should emit events. |
114115
| autoStyleLineNumbers | `Boolean` | `true` | - | Allow the line number to be styled by this component. |
115116

116117
## Events
117118

118-
Those events won't fire unless you set the `emitEvents` prop to `true`.
119+
| Name | Parameters | Description |
120+
| ------ | ---------- | ------------------------------- |
121+
| change | `(code)` | Fires when the code is changed. |
122+
123+
The events below won't be fired unless you set the `emitEvents` prop to `true`.
119124

120125
| Name | Parameters | Description |
121126
| ------------ | ---------- | --------------------------------------------------------------------------- |
122-
| change | `(event)` | Fires when the value is changed. |
123127
| keydown | `(event)` | This event is emitted when a keydown event happens in editor |
124128
| keyup | `(event)` | This event is emitted when a keyup event happens in editor |
125129
| editor-click | `(event)` | This event is emitted when clicking anywhere in the contenteditable editor |

jest.config.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
moduleFileExtensions: ["js", "jsx", "json", "vue"],
3+
transform: {
4+
"^.+\\.vue$": "vue-jest",
5+
".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$":
6+
"jest-transform-stub",
7+
"^.+\\.jsx?$": "babel-jest"
8+
},
9+
moduleNameMapper: {
10+
"^@/(.*)$": "<rootDir>/src/$1"
11+
},
12+
snapshotSerializers: ["jest-serializer-vue"],
13+
testMatch: [
14+
"**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"
15+
],
16+
testURL: "http://localhost/"
17+
};

package.json

+31-26
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,13 @@
11
{
22
"name": "vue-prism-editor",
3-
"version": "0.0.1",
4-
"description": "A dead simple code editor with syntax highlighting and line numbers.",
5-
"keywords": [
6-
"vue",
7-
"editor",
8-
"code editor",
9-
"prism"
10-
],
11-
"main": "dist/VuePrismEditor.umd.js",
12-
"module": "dist/VuePrismEditor.common.js",
13-
"unpkg": "dist/VuePrismEditor.umd.min.js",
3+
"version": "0.1.0",
144
"scripts": {
155
"serve": "vue-cli-service serve",
166
"build": "vue-cli-service build --dest site",
7+
"lint": "vue-cli-service lint",
178
"build:lib": "vue-cli-service build --target lib --name VuePrismEditor ./src/components/Editor.vue && rimraf dist/demo.html",
18-
"lint": "vue-cli-service lint"
9+
"test:unit": "vue-cli-service test:unit"
1910
},
20-
"author": {
21-
"name": "Mesut Koca",
22-
"email": "imesutkoca@gmail.com"
23-
},
24-
"repository": {
25-
"type": "git",
26-
"url": "git+https://github.com/koca/vue-prism-editor.git"
27-
},
28-
"bugs": {
29-
"url": "https://github.com/koca/vue-prism-editor/issues"
30-
},
31-
"homepage": "https://github.com/koca/vue-prism-editor#readme",
32-
"license": "MIT",
3311
"dependencies": {
3412
"dom-iterator": "^1.0.0",
3513
"escape-html": "^1.0.3",
@@ -39,8 +17,12 @@
3917
"@vue/cli-plugin-babel": "^3.0.0-beta.15",
4018
"@vue/cli-plugin-eslint": "^3.0.0-beta.15",
4119
"@vue/cli-plugin-pwa": "^3.0.0-beta.15",
20+
"@vue/cli-plugin-unit-jest": "^3.0.3",
4221
"@vue/cli-service": "^3.0.0-beta.15",
4322
"@vue/eslint-config-prettier": "^3.0.0-beta.15",
23+
"@vue/test-utils": "^1.0.0-beta.20",
24+
"babel-core": "7.0.0-bridge.0",
25+
"babel-jest": "^23.0.1",
4426
"prismjs": "^1.15.0",
4527
"register-service-worker": "^1.5.2",
4628
"rimraf": "^2.6.2",
@@ -52,5 +34,28 @@
5234
"> 1%",
5335
"last 2 versions",
5436
"not ie <= 8"
55-
]
37+
],
38+
"unpkg": "dist/VuePrismEditor.umd.min.js",
39+
"module": "dist/VuePrismEditor.common.js",
40+
"main": "dist/VuePrismEditor.umd.js",
41+
"repository": {
42+
"type": "git",
43+
"url": "git+https://github.com/koca/vue-prism-editor.git"
44+
},
45+
"bugs": {
46+
"url": "https://github.com/koca/vue-prism-editor/issues"
47+
},
48+
"homepage": "https://github.com/koca/vue-prism-editor#readme",
49+
"license": "MIT",
50+
"keywords": [
51+
"vue",
52+
"editor",
53+
"code editor",
54+
"prism"
55+
],
56+
"description": "A dead simple code editor with syntax highlighting and line numbers.",
57+
"author": {
58+
"name": "Mesut Koca",
59+
"email": "imesutkoca@gmail.com"
60+
}
5661
}

src/App.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<Editor
2626
class="my-editor"
2727
language="html"
28-
:code="code"
28+
v-model="code"
2929
:line-numbers="lineNumbers"
3030
:readonly="readonly"
3131
></Editor>

src/components/Editor.vue

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ import selectionRange from "../utils/selection-range.js";
3131
import { getIndent, getDeindentLevel } from "../utils/getIndent";
3232
3333
export default {
34+
model: {
35+
prop: "code",
36+
event: "change"
37+
},
3438
props: {
3539
emitEvents: {
3640
type: Boolean,
37-
default: true
41+
default: false
3842
},
3943
language: {
4044
type: String,
@@ -220,9 +224,7 @@ export default {
220224
this.$nextTick(() => {
221225
this.codeData = plain;
222226
});
223-
if (this.emitEvents) {
224-
this.$emit("change", plain);
225-
}
227+
this.$emit("change", plain);
226228
},
227229
restoreStackState(offset) {
228230
const { plain, selection } = this.undoStack[

tests/unit/.eslintrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
env: {
3+
jest: true
4+
}
5+
};

tests/unit/Editor.spec.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { mount } from "@vue/test-utils";
2+
import Editor from "@/components/Editor.vue";
3+
import "prismjs";
4+
import { compileToFunctions } from "vue-template-compiler";
5+
6+
document.createRange = jest.fn();
7+
global.getSelection = jest.fn();
8+
global.getSelection.mockReturnValue({
9+
rangeCount: 1,
10+
getRangeAt: jest.fn().mockReturnValue({
11+
cloneRange: jest.fn().mockReturnValue({
12+
selectNodeContents: jest.fn(),
13+
setEnd: jest.fn(),
14+
setStart: jest.fn()
15+
})
16+
})
17+
});
18+
19+
describe("Editor.vue", () => {
20+
test("renders", () => {
21+
let code = "initialCode";
22+
const wrapper = mount(Editor, {
23+
propsData: { code }
24+
});
25+
expect(wrapper.html()).toContain(code);
26+
});
27+
test("emits change event", () => {
28+
let code = "console.log('test')";
29+
const wrapper = mount(Editor);
30+
const $pre = wrapper.find(".prism-editor__code");
31+
$pre.element.innerHTML = code;
32+
$pre.trigger("keyup");
33+
expect(wrapper.emitted("change")[0]).toEqual([code]);
34+
});
35+
36+
test("v-model works", () => {
37+
const compiled = compileToFunctions(
38+
'<div><Editor class="foo" v-model="code" /></div>'
39+
);
40+
const wrapper = mount(compiled, {
41+
data: () => ({
42+
code: "test"
43+
}),
44+
stubs: {
45+
Editor
46+
}
47+
});
48+
const $pre = wrapper.find("pre");
49+
50+
expect(wrapper.vm.code).toEqual("test");
51+
$pre.element.innerHTML = "works";
52+
wrapper.vm.$children[0].$emit("change", "works");
53+
expect(wrapper.vm.code).toEqual("works");
54+
});
55+
});

0 commit comments

Comments
 (0)