Skip to content

Commit d8f9e41

Browse files
committed
feat: RoughNotationGroup component
1 parent 37d722f commit d8f9e41

File tree

5 files changed

+176
-36
lines changed

5 files changed

+176
-36
lines changed

README.md

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ A Vue wrapper for [RoughNotation](https://github.com/pshihn/rough-notation), a s
1515
- [Installation](#installation)
1616
- [Usage](#usage)
1717
- [Global options](#global-options)
18-
- [Props](#props)
19-
- [Events](#events)
20-
- [TODO](#todo)
18+
- [RoughNotation Component](#roughnotation-component)
19+
- [RoughNotationGroup Component](#roughnotationgroup-component)
2120
- [License](#license)
2221

2322
## Demo
@@ -48,17 +47,6 @@ import VueRoughNotation from 'vue-rough-notation';
4847
Vue.use(VueRoughNotation);
4948
```
5049

51-
template:
52-
53-
```html
54-
<RoughNotation
55-
:is-show="isShow"
56-
type="underline"
57-
>
58-
<span>Rough Notation is awesome</span>
59-
</RoughNotation>
60-
```
61-
6250
## Global options
6351

6452
The default global options are:
@@ -88,9 +76,22 @@ import VueRoughNotation from 'vue-rough-notation';
8876
Vue.use(VueRoughNotation, options);
8977
```
9078

91-
## Props
79+
## RoughNotation Component
80+
81+
### Usage
82+
83+
```html
84+
<RoughNotation
85+
:is-show="isShow"
86+
type="underline"
87+
>
88+
<span>Rough Notation is awesome</span>
89+
</RoughNotation>
90+
```
91+
92+
### Props
9293

93-
### type
94+
#### type
9495

9596
**Type**: `string`
9697

@@ -105,7 +106,7 @@ This is a mandatory field. It sets the annotation style. Following are the list
105106
- **strike-through**: This style draws a box around the element.
106107
- **crossed-off**: This style draws a box around the element.
107108

108-
### isShow
109+
#### isShow
109110

110111
**Type**: `boolean`
111112

@@ -115,7 +116,7 @@ This is a mandatory field. It sets the annotation style. Following are the list
115116

116117
Whether draws the annotation.
117118

118-
### animate
119+
#### animate
119120

120121
**Type**: `boolean`
121122

@@ -125,7 +126,7 @@ Whether draws the annotation.
125126

126127
Turn on/off animation when annotating.
127128

128-
### animationDuration
129+
#### animationDuration
129130

130131
**Type**: `number`
131132

@@ -135,7 +136,7 @@ Turn on/off animation when annotating.
135136

136137
Duration of the animation in milliseconds.
137138

138-
### animationDelay
139+
#### animationDelay
139140

140141
**Type**: `number`
141142

@@ -145,7 +146,7 @@ Duration of the animation in milliseconds.
145146

146147
Delay in animation in milliseconds.
147148

148-
### color
149+
#### color
149150

150151
**Type**: `string`
151152

@@ -155,7 +156,7 @@ Delay in animation in milliseconds.
155156

156157
Representing the color of the annotation sketch.
157158

158-
### strokeWidth
159+
#### strokeWidth
159160

160161
**Type**: `number`
161162

@@ -165,7 +166,7 @@ Representing the color of the annotation sketch.
165166

166167
Width of the annotation strokes.
167168

168-
### padding
169+
#### padding
169170

170171
**Type**: `number`
171172

@@ -175,27 +176,57 @@ Width of the annotation strokes.
175176

176177
Padding between the element and roughly where the annotation is drawn.
177178

178-
### tag
179+
#### tag
179180

180181
**Type**: `string`
181182

182183
**Required**: `false`
183184

184-
**Default**: `undefined'`
185+
**Default**: `'span'`
185186

186187
String HTML tag name; if falsy (for example `null` or `undefined`), the component will be renderless (the content won't be wrapped in a tag), in this case, only the first child will be rendered
187188

188-
## Events
189+
### Events
189190

190-
### init
191+
#### init
191192

192193
**Parameters**: [`Annotation Object`](https://github.com/pshihn/rough-notation#annotation-object)
193194

194195
Called when the component is initialized.
195196

196-
## TODO
197+
## RoughNotationGroup Component
197198

198-
- [ ] Annotation Group
199+
### Usage
200+
201+
```html
202+
<RoughNotationGroup :is-show="isShow">
203+
<RoughNotation type="underline">Rough Notation</RoughNotation>
204+
<RoughNotation type="box">is</RoughNotation>
205+
<RoughNotation type="highlight">awesome</RoughNotation>
206+
</RoughNotation>
207+
```
208+
209+
### Props
210+
211+
#### isShow
212+
213+
**Type**: `boolean`
214+
215+
**Required**: `false`
216+
217+
**Default**: `false`
218+
219+
Show/Hides the annotations
220+
221+
#### tag
222+
223+
**Type**: `string`
224+
225+
**Required**: `false`
226+
227+
**Default**: `'div'`
228+
229+
String HTML tag name; if falsy (for example `null` or `undefined`), the component will be renderless (the content won't be wrapped in a tag), in this case, only the first child will be rendered
199230

200231
## License
201232

src/component.js renamed to src/components/RoughNotation.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default (options) => ({
2323

2424
tag: {
2525
type: String,
26-
default: undefined,
26+
default: 'span',
2727
},
2828

2929
isShow: {
@@ -74,6 +74,7 @@ export default (options) => ({
7474
});
7575

7676
this.$emit('init', this.annotation);
77+
this.$_dispatchGroup('annotation:add');
7778

7879
this.$watch('isShow', (value) => {
7980
if (value) {
@@ -85,6 +86,7 @@ export default (options) => ({
8586
},
8687

8788
beforeDestroy () {
89+
this.$_dispatchGroup('annotation:remove');
8890
this.annotation && this.annotation.remove();
8991
},
9092

@@ -100,6 +102,23 @@ export default (options) => ({
100102
isShowing () {
101103
return !!(this.annotation && this.annotation.isShowing());
102104
},
105+
106+
$_dispatchGroup (event) {
107+
let parent = this.$parent || this.$root;
108+
let name = parent.$options.name;
109+
110+
while (parent && (!name || name !== 'RoughNotationGroup')) {
111+
parent = parent.$parent;
112+
113+
if (parent) {
114+
name = parent.$options.name;
115+
}
116+
}
117+
118+
if (parent) {
119+
parent.$emit.call(parent, event, this.annotation);
120+
}
121+
},
103122
},
104123

105124
render (h) {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { annotationGroup } from 'rough-notation';
2+
3+
export default {
4+
name: 'RoughNotationGroup',
5+
6+
props: {
7+
isShow: {
8+
type: Boolean,
9+
default: false,
10+
},
11+
12+
tag: {
13+
type: String,
14+
default: 'div',
15+
},
16+
},
17+
18+
data: () => ({
19+
annotations: [],
20+
}),
21+
22+
watch: {
23+
isShow (value) {
24+
if (value) {
25+
this.show();
26+
} else {
27+
this.hide();
28+
}
29+
},
30+
31+
annotations(annotations) {
32+
this.group = annotationGroup(annotations);
33+
},
34+
},
35+
36+
created () {
37+
this.$on('annotation:add', (annotation) => {
38+
this.$_add(annotation);
39+
});
40+
41+
this.$on('annotation:remove', (annotation) => {
42+
this.$_remove(annotation);
43+
});
44+
},
45+
46+
mounted () {
47+
if (this.isShow) {
48+
this.show();
49+
}
50+
},
51+
52+
methods: {
53+
show () {
54+
this.group && this.group.show();
55+
},
56+
57+
hide () {
58+
this.group && this.group.hide();
59+
},
60+
61+
$_add (annotation) {
62+
this.annotations.push(annotation);
63+
},
64+
65+
$_remove (annotation) {
66+
const index = this.annotations.indexOf(annotation);
67+
if (index > -1) {
68+
this.annotations.splice(index, 1);
69+
}
70+
},
71+
},
72+
73+
render(h) {
74+
const slot = this.$slots.default;
75+
76+
if (this.tag) {
77+
return h(this.tag, null, slot);
78+
}
79+
80+
return slot && slot[0];
81+
},
82+
};

src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { defaultOptions } from './options';
2-
import RoughNotation from './component';
2+
import RoughNotation from './components/RoughNotation';
3+
import RoughNotationGroup from './components/RoughNotationGroup';
34

45
const VueRoughNotationPlugin = {
56
/**
67
* install function
7-
* @param {Vue} Vue
8+
* @param {Vue} Vue
9+
* @param {Object} options RoughNotation options
810
*/
911
install (Vue, options = {}) {
1012
const finalOptions = {
@@ -16,6 +18,9 @@ const VueRoughNotationPlugin = {
1618

1719
Vue.component('rough-notation', RoughNotationComponent);
1820
Vue.component('RoughNotation', RoughNotationComponent);
21+
22+
Vue.component('rough-notation-group', RoughNotationGroup);
23+
Vue.component('RoughNotationGroup', RoughNotationGroup);
1924
},
2025
};
2126

test/index.spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@ describe('Vue Rough Notation Test Suite', () => {
77

88
expect(localVue.options.components.RoughNotation).toBeUndefined();
99
expect(localVue.options.components['rough-notation']).toBeUndefined();
10+
expect(localVue.options.components.RoughNotationGroup).toBeUndefined();
11+
expect(localVue.options.components['rough-notation-group']).toBeUndefined();
1012

1113
localVue.use(VueRoughNotation);
1214

1315
expect(localVue.options.components.RoughNotation).toBeDefined();
1416
expect(localVue.options.components['rough-notation']).toBeDefined();
17+
expect(localVue.options.components.RoughNotationGroup).toBeDefined();
18+
expect(localVue.options.components['rough-notation-group']).toBeDefined();
1519
});
1620

17-
describe('component', () => {
18-
// install plugin
21+
describe('RoughNotation component', () => {
1922
const localVue = createLocalVue();
2023
localVue.use(VueRoughNotation);
2124
const RoughNotationComp = localVue.options.components.RoughNotation;
@@ -46,8 +49,8 @@ describe('Vue Rough Notation Test Suite', () => {
4649
expect(annotation.isShowing()).toBe(false);
4750
});
4851

49-
test('is a renderless component by default', () => {
50-
expect(wrapper.html()).toEqual(domStr);
52+
test('wrapped by `span` by default', () => {
53+
expect(wrapper.html()).toEqual(`<span>${domStr}</span>`);
5154
});
5255
});
5356
});

0 commit comments

Comments
 (0)