check if mark has attributes
This commit is contained in:
@@ -1,6 +1,11 @@
|
|||||||
<template>
|
<template>
|
||||||
<div v-if="editor">
|
<div v-if="editor">
|
||||||
<button @click="editor.chain().focus().highlight().run()" :class="{ 'is-active': editor.isActive('highlight') }">
|
<button
|
||||||
|
@click="editor.chain().focus().highlight().run()"
|
||||||
|
:class="{ 'is-active': editor.isActive('highlight', {
|
||||||
|
color: ''
|
||||||
|
}) }"
|
||||||
|
>
|
||||||
highlight (default)
|
highlight (default)
|
||||||
</button>
|
</button>
|
||||||
<button @click="editor.chain().focus().highlight({ color: 'red' }).run()" :class="{ 'is-active': editor.isActive('highlight', { color: 'red' }) }">
|
<button @click="editor.chain().focus().highlight({ color: 'red' }).run()" :class="{ 'is-active': editor.isActive('highlight', { color: 'red' }) }">
|
||||||
@@ -53,7 +58,7 @@ export default {
|
|||||||
content: `
|
content: `
|
||||||
<p>This isn’t highlighted.</s></p>
|
<p>This isn’t highlighted.</s></p>
|
||||||
<p><mark>But that one is.</mark></p>
|
<p><mark>But that one is.</mark></p>
|
||||||
<p><mark style="background-color: pink;">And this is highlighted too, but in a different color.</mark></p>
|
<p><mark style="background-color: red;">And this is highlighted too, but in a different color.</mark></p>
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -351,7 +351,7 @@ export class Editor extends EventEmitter {
|
|||||||
if (schemaType === 'node') {
|
if (schemaType === 'node') {
|
||||||
return nodeIsActive(this.state, this.schema.nodes[name], attrs)
|
return nodeIsActive(this.state, this.schema.nodes[name], attrs)
|
||||||
} if (schemaType === 'mark') {
|
} if (schemaType === 'mark') {
|
||||||
return markIsActive(this.state, this.schema.marks[name])
|
return markIsActive(this.state, this.schema.marks[name], attrs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|||||||
9
packages/core/src/utils/markHasAttributes.ts
Normal file
9
packages/core/src/utils/markHasAttributes.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { EditorState } from 'prosemirror-state'
|
||||||
|
import { MarkType } from 'prosemirror-model'
|
||||||
|
import getMarkAttrs from './getMarkAttrs'
|
||||||
|
|
||||||
|
export default function markHasAttributes(state: EditorState, type: MarkType, attrs?: Object) {
|
||||||
|
return attrs === null || JSON.stringify(attrs) === JSON.stringify(
|
||||||
|
getMarkAttrs(state, type),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
import { EditorState } from 'prosemirror-state'
|
import { EditorState } from 'prosemirror-state'
|
||||||
import { MarkType } from 'prosemirror-model'
|
import { MarkType } from 'prosemirror-model'
|
||||||
|
import markHasAttributes from './markHasAttributes'
|
||||||
|
|
||||||
export default function markIsActive(state: EditorState, type: MarkType) {
|
export default function markIsActive(state: EditorState, type: MarkType, attrs?: {}) {
|
||||||
const {
|
const {
|
||||||
from,
|
from,
|
||||||
$from,
|
$from,
|
||||||
@@ -9,9 +10,11 @@ export default function markIsActive(state: EditorState, type: MarkType) {
|
|||||||
empty,
|
empty,
|
||||||
} = state.selection
|
} = state.selection
|
||||||
|
|
||||||
|
const hasAttributes = markHasAttributes(state, type, attrs)
|
||||||
|
|
||||||
if (empty) {
|
if (empty) {
|
||||||
return !!type.isInSet(state.storedMarks || $from.marks())
|
return (type.isInSet(state.storedMarks || $from.marks()) && hasAttributes)
|
||||||
}
|
}
|
||||||
|
|
||||||
return !!state.doc.rangeHasMark(from, to, type)
|
return (state.doc.rangeHasMark(from, to, type) && hasAttributes)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user