Skip to content

Commit

Permalink
feat: node active
Browse files Browse the repository at this point in the history
  • Loading branch information
mekery committed Apr 14, 2020
1 parent 6d33bbc commit 7a1e0fb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/lib/components/buttons/OAlignDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</template>

<script>
import { isAlignmentActive } from '../../utils/alignment'
import { isNodeActive } from '../../utils/node'
export default {
name: 'o-align-dropdown',
data () {
Expand All @@ -39,7 +39,7 @@ export default {
},
methods: {
isActive (alignment) {
return isAlignmentActive(this.editor.state, alignment)
return isNodeActive(this.editor.state, 'textAlign', alignment)
}
},
computed: {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/buttons/OHeadingDropdown.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<q-btn-dropdown icon="mdi-format-header-pound" menu-anchor="bottom left" menu-self="top left" class="o-heading-dropdown" content-class="o-menu o-heading-dropdown-menu" flat>
<o-heading-list :commands="commands" :is-active="isActive" />
<o-heading-list :editor="editor" :commands="commands" />
</q-btn-dropdown>
</template>

Expand All @@ -13,10 +13,10 @@ export default {
}
},
props: {
commands: {
editor: {
type: Object
},
isActive: {
commands: {
type: Object
}
},
Expand Down
20 changes: 12 additions & 8 deletions src/lib/components/buttons/OHeadingList.vue
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
<template>
<q-list class="o-heading-list">
<q-item :class="{ 'is-active': isActive.paragraph() }" clickable v-close-popup @click.native="commands.paragraph">
<q-item clickable v-close-popup @click.native="commands.paragraph">
<q-item-section side><q-icon name="mdi-format-pilcrow" /></q-item-section>
<q-item-section>正文</q-item-section>
</q-item>
<q-separator />
<q-item :class="{ 'is-active': isActive.heading({ level: 1 }) }" clickable v-close-popup @click.native="commands.heading({ level: 1 })">
<q-item :class="{ 'is-active': isActive(1) }" clickable v-close-popup @click.native="commands.heading({ level: 1 })">
<q-item-section side>H1</q-item-section>
<q-item-section>H1 标题</q-item-section>
</q-item>
<q-item :class="{ 'is-active': isActive.heading({ level: 2 }) }" clickable v-close-popup @click.native="commands.heading({ level: 2 })">
<q-item :class="{ 'is-active': isActive(2) }" clickable v-close-popup @click.native="commands.heading({ level: 2 })">
<q-item-section side>H2</q-item-section>
<q-item-section>H2 标题</q-item-section>
</q-item>
<q-item :class="{ 'is-active': isActive.heading({ level: 3 }) }" clickable v-close-popup @click.native="commands.heading({ level: 3 })">
<q-item :class="{ 'is-active': isActive(3) }" clickable v-close-popup @click.native="commands.heading({ level: 3 })">
<q-item-section side>H3</q-item-section>
<q-item-section>H3 标题</q-item-section>
</q-item>
<q-item :class="{ 'is-active': isActive.heading({ level: 4 }) }" clickable v-close-popup @click.native="commands.heading({ level: 4 })">
<q-item :class="{ 'is-active': isActive(4) }" clickable v-close-popup @click.native="commands.heading({ level: 4 })">
<q-item-section side>H4</q-item-section>
<q-item-section>H4 标题</q-item-section>
</q-item>
<q-item :class="{ 'is-active': isActive.heading({ level: 5 }) }" clickable v-close-popup @click.native="commands.heading({ level: 5 })">
<q-item :class="{ 'is-active': isActive(5) }" clickable v-close-popup @click.native="commands.heading({ level: 5 })">
<q-item-section side>H5</q-item-section>
<q-item-section>H5 标题</q-item-section>
</q-item>
</q-list>
</template>

<script>
import { isNodeActive } from '../../utils/node'
export default {
name: 'o-heading-list',
data () {
return {
}
},
props: {
commands: {
editor: {
type: Object
},
isActive: {
commands: {
type: Object
}
},
methods: {
isActive (level) {
return isNodeActive(this.editor.state, 'level', level)
}
},
computed: {
}
Expand Down
9 changes: 8 additions & 1 deletion src/lib/components/buttons/OLineHeightDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<q-list>
<template v-for="(item, index) of types">
<q-separator :key="index" v-if="item.separator" />
<q-item :key="index" clickable v-close-popup
<q-item :class="{ 'is-active': isActive(item.value) }" :key="index" clickable v-close-popup
@click.native="commands.lineHeight({lineHeight: item.value})" v-else>
<q-item-section>{{item.label}}</q-item-section>
</q-item>
Expand All @@ -13,6 +13,7 @@
</template>

<script>
import { isNodeActive } from '../../utils/node'
export default {
name: 'o-line-height-dropdown',
data () {
Expand All @@ -29,11 +30,17 @@ export default {
}
},
props: {
editor: {
type: Object
},
commands: {
type: Object
}
},
methods: {
isActive (lineHeight) {
return isNodeActive(this.editor.state, 'lineHeight', lineHeight)
}
},
computed: {
}
Expand Down
18 changes: 0 additions & 18 deletions src/lib/utils/alignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,6 @@ const ALLOWED_NODE_TYPES = [
'todo_item',
]

export function isAlignmentActive (state, alignment) {
const { selection, doc } = state
const { from, to } = selection

let keepLooking = true
let active = false

doc.nodesBetween(from, to, (node) => {
if (keepLooking && node.attrs.textAlign === alignment) {
keepLooking = false
active = true
}
return keepLooking
})

return active
}

export function setAlignment (tr, alignment) {
const { selection, doc } = tr

Expand Down
24 changes: 24 additions & 0 deletions src/lib/utils/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Utils: node
* @author Micle, [email protected]
*/

function isNodeActive (state, name, value) {
const { selection, doc } = state
const { from, to } = selection

let keepLooking = true
let active = false

doc.nodesBetween(from, to, (node) => {
if (keepLooking && node.attrs[name] === value) {
keepLooking = false
active = true
}
return keepLooking
})

return active
}

export { isNodeActive }

0 comments on commit 7a1e0fb

Please sign in to comment.