Merge pull request #285 from StickyKnow/fix-italic-rules
fix pasteRules and InputRules for italic mark
This commit is contained in:
@@ -4,22 +4,29 @@ export default function (regexp, markType, getAttrs) {
|
|||||||
return new InputRule(regexp, (state, match, start, end) => {
|
return new InputRule(regexp, (state, match, start, end) => {
|
||||||
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
|
const attrs = getAttrs instanceof Function ? getAttrs(match) : getAttrs
|
||||||
const { tr } = state
|
const { tr } = state
|
||||||
|
const m = match.length - 1
|
||||||
let markEnd = end
|
let markEnd = end
|
||||||
|
let markStart = start
|
||||||
|
|
||||||
if (match[1]) {
|
if (match[m]) {
|
||||||
const startSpaces = match[0].search(/\S/)
|
const matchStart = start + match[0].indexOf(match[m - 1])
|
||||||
const textStart = start + match[0].indexOf(match[1])
|
// matchEnd index is -1 because the last matching char is not yet member of transaction
|
||||||
const textEnd = textStart + match[1].length
|
// and actually never will be because it triggered the inputrule and vanishes ;)
|
||||||
if (textEnd < end) {
|
const matchEnd = matchStart + match[m - 1].length - 1
|
||||||
tr.delete(textEnd, end)
|
const textStart = matchStart + match[m - 1].lastIndexOf(match[m])
|
||||||
|
const textEnd = textStart + match[m].length
|
||||||
|
|
||||||
|
if (textEnd < matchEnd) {
|
||||||
|
tr.delete(textEnd, matchEnd)
|
||||||
}
|
}
|
||||||
if (textStart > start) {
|
if (textStart > matchStart) {
|
||||||
tr.delete(start + startSpaces, textStart)
|
tr.delete(matchStart, textStart)
|
||||||
}
|
}
|
||||||
markEnd = start + startSpaces + match[1].length
|
markStart = matchStart
|
||||||
|
markEnd = markStart + match[m].length
|
||||||
}
|
}
|
||||||
|
|
||||||
tr.addMark(start, markEnd, markType.create(attrs))
|
tr.addMark(markStart, markEnd, markType.create(attrs))
|
||||||
tr.removeStoredMark(markType) // Do not continue with mark.
|
tr.removeStoredMark(markType) // Do not continue with mark.
|
||||||
return tr
|
return tr
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -30,13 +30,15 @@ export default class Italic extends Mark {
|
|||||||
|
|
||||||
inputRules({ type }) {
|
inputRules({ type }) {
|
||||||
return [
|
return [
|
||||||
markInputRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)$/, type),
|
markInputRule(/(?:^|[^_])(_([^_]+)_)$/, type),
|
||||||
|
markInputRule(/(?:^|[^*])(\*([^*]+)\*)$/, type),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
pasteRules({ type }) {
|
pasteRules({ type }) {
|
||||||
return [
|
return [
|
||||||
markPasteRule(/(?:^|[^*_])(?:\*|_)([^*_]+)(?:\*|_)/g, type),
|
markPasteRule(/_([^_]+)_/g, type),
|
||||||
|
markPasteRule(/\*([^*]+)\*/g, type),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user