Skip to content

Commit ca65e26

Browse files
committed
feat: Further QEditor work
1 parent 781326b commit ca65e26

File tree

9 files changed

+51
-21
lines changed

9 files changed

+51
-21
lines changed

dev/components/form/editor.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
['bold', 'italic', 'underline', 'strike', 'h1', 'paragraph'],
77
['subscript', 'superscript'],
88
['quote', 'bullet', 'number', 'outdent', 'indent'],
9-
[['paragraph', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], 'hr', 'removeFormat'],
9+
[['paragraph', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']], ['hr', 'removeFormat'],
1010
['undo', 'redo']
1111
]"
1212
>

src/components/btn/QBtn.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
:style="{width: width}"
1414
></div>
1515

16-
<span class="q-btn-inner row col flex-center">
16+
<span
17+
class="q-btn-inner row col flex-center"
18+
:class="{'no-wrap': noWrap}"
19+
>
1720
<slot v-if="loading" name="loading">
1821
<q-spinner></q-spinner>
1922
</slot>

src/components/btn/QBtnDropdown.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default {
6262
props: {
6363
disable: this.disable,
6464
noCaps: this.noCaps,
65+
noWrap: this.noWrap,
6566
icon: this.icon,
6667
label: this.label,
6768
iconRight: this.split ? this.iconRight : null,
@@ -102,7 +103,7 @@ export default {
102103
rounded: this.rounded,
103104
push: this.push
104105
},
105-
staticClass: 'q-btn-dropdown q-btn-dropdown-split'
106+
staticClass: 'q-btn-dropdown q-btn-dropdown-split no-wrap'
106107
},
107108
[
108109
getBtn(),

src/components/btn/QBtnToggle.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
:class="classes"
77
>
88
<div class="desktop-only q-focus-helper"></div>
9-
<span class="q-btn-inner row col flex-center">
9+
<span
10+
class="q-btn-inner row col flex-center"
11+
:class="{'no-wrap': noWrap}"
12+
>
1013
<q-icon v-if="icon" :name="icon" :class="{'on-left': label && !round}"></q-icon>
1114
<span v-if="label && !round">{{ label }}</span>
1215
<slot></slot>

src/components/btn/btn-group.mat.styl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
border-radius $button-border-radius
33
box-shadow $button-shadow
44
vertical-align middle
5+
> .q-btn-group
6+
box-shadow none
57
.q-btn
68
box-shadow none !important
79
.q-btn:not(:last-child)

src/components/btn/btn-mixin.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ export default {
1111
props: {
1212
disable: Boolean,
1313
label: String,
14-
noCaps: {
15-
type: Boolean,
16-
default: false
17-
},
14+
noCaps: Boolean,
15+
noWrap: Boolean,
1816
icon: String,
1917
iconRight: String,
2018
round: Boolean,

src/components/editor/QEditor.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@ export default {
5050
},
5151
buttons () {
5252
const getBtn = name => {
53-
const btn = this.definitions[name] || buttons[name]
54-
if (btn.type === 'toggle' || btn.disable) {
53+
const
54+
btn = this.definitions[name] || buttons[name],
55+
state = this.attrib[btn.test || btn.cmd]
56+
57+
if (state === void 0 && (btn.type === 'toggle' || btn.disable)) {
5558
this.attrib[btn.test || btn.cmd] = false
5659
}
5760
return btn
@@ -118,7 +121,7 @@ export default {
118121
const key = getEventKey(e)
119122
this.updateAttributes()
120123

121-
if (!e.ctrlKey || key === 17 || key === 65) {
124+
if (!e.ctrlKey || [17, 65, 67, 86].includes(key)) {
122125
return
123126
}
124127

@@ -140,21 +143,29 @@ export default {
140143
}
141144
},
142145
updateAttributes () {
143-
console.log(this.buttons)
144146
setTimeout(() => {
147+
let change = false
145148
Object.keys(this.attrib).forEach(cmd => {
146-
this.attrib[cmd] = this.caret.is(cmd)
149+
const state = this.caret.is(cmd)
150+
if (this.attrib[cmd] !== state) {
151+
this.attrib[cmd] = state
152+
change = true
153+
}
147154
})
148-
this.$forceUpdate()
155+
if (change) {
156+
console.log('forceUpdate')
157+
this.$forceUpdate()
158+
}
149159
}, 1)
150160
}
151161
},
152162
mounted () {
153163
this.$refs.content.innerHTML = this.value
154164
this.caret = new Caret(this.$refs.content)
155-
this.runCmd('defaultParagraphSeparator', 'p')
165+
document.execCommand('defaultParagraphSeparator', false, 'div')
156166
},
157167
render (h) {
168+
const attr = this.attrib
158169
return h(
159170
'div',
160171
{ staticClass: 'q-editor' },
@@ -173,10 +184,16 @@ export default {
173184
on: {
174185
input: this.onInput,
175186
keydown: this.onKeydown,
176-
click: this.updateAttributes
187+
click: this.updateAttributes,
188+
blur: () => {
189+
this.caret.save()
190+
}
177191
}
178192
}
179-
)
193+
),
194+
h('div', {style: {
195+
wordWrap: 'break-word'
196+
}}, [JSON.stringify(attr)])
180197
]
181198
)
182199
}

src/components/editor/editor-caret.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class Caret {
9595
sel.addRange(r)
9696
}
9797
else {
98-
sel.selectAllChildren(this.element)
98+
sel.selectAllChildren(this.el)
9999
sel.collapseToEnd()
100100
}
101101
}
@@ -119,7 +119,6 @@ export class Caret {
119119

120120
is (name) {
121121
if (['BLOCKQUOTE', 'DIV', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'A', 'IMG'].includes(name)) {
122-
console.log('HIIIT')
123122
return this.hasParent(name)
124123
}
125124
return document.queryCommandState(name)

src/components/editor/editor-utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ function getBtn (h, vm, btn) {
4141
}, child)
4242
}
4343
else if (Array.isArray(btn)) {
44+
let sel = ''
45+
4446
const Items = btn.map(item => {
45-
return h(QItem, [
47+
const active = vm.attrib[item.test || item.cmd]
48+
if (active) {
49+
sel = item.tip
50+
}
51+
return h(QItem, {props: {active}}, [
4652
h(QItemMain, {
4753
props: {
4854
label: item.tip
@@ -51,14 +57,15 @@ function getBtn (h, vm, btn) {
5157
click () {
5258
instance.componentInstance.close()
5359
vm.$refs.content.focus()
60+
vm.caret.restore()
5461
vm.runCmd(item.cmd, item.param)
5562
}
5663
}
5764
})
5865
])
5966
})
6067

61-
const instance = h(QBtnDropdown, { props: { split: true, label: 'Select' } }, [
68+
const instance = h(QBtnDropdown, { props: { noCaps: true, noWrap: true, label: sel } }, [
6269
h(QList, { props: { link: true, separator: true } }, [
6370
Items
6471
])

0 commit comments

Comments
 (0)