operation of classList

This commit is contained in:
Jocs 2017-11-16 12:01:54 +08:00
parent 6d790ce42c
commit 010ee4d8df
2 changed files with 12 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import {
isAganippeEditorElement, isAganippeEditorElement,
findNearestParagraph, findNearestParagraph,
markedText2Html, markedText2Html,
operateClassName,
insertAfter // eslint-disable-line no-unused-vars insertAfter // eslint-disable-line no-unused-vars
} from './utils.js' } from './utils.js'
@ -192,12 +193,8 @@ class Aganippe {
subscribeParagraphChange (newParagraph, oldParagraph) { subscribeParagraphChange (newParagraph, oldParagraph) {
console.log(newParagraph.id, oldParagraph.id) console.log(newParagraph.id, oldParagraph.id)
if (oldParagraph.classList.contains(activeClassName)) { operateClassName(oldParagraph, 'remove', activeClassName)
oldParagraph.classList.remove(activeClassName) operateClassName(newParagraph, 'add', activeClassName)
}
if (!newParagraph.classList.contains(activeClassName)) {
newParagraph.classList.add(activeClassName)
}
} }
// TODO: refactor // TODO: refactor
handleKeyDown () { handleKeyDown () {

View File

@ -211,14 +211,6 @@ export const updateBlock = (origin, tagName) => {
replaceElement(origin, html2element(html)) replaceElement(origin, html2element(html))
} }
/**
* translate paragraph to ohter block
*/
/**
* viewModel2Html
*/
export const createEmptyElement = (ids, tagName, attrs) => { export const createEmptyElement = (ids, tagName, attrs) => {
const id = getUniqueId(ids) const id = getUniqueId(ids)
const element = document.createElement(tagName) const element = document.createElement(tagName)
@ -233,6 +225,15 @@ export const createEmptyElement = (ids, tagName, attrs) => {
return element return element
} }
/**
* [description `add` or `remove` className of element
*/
export const operateClassName = (element, ctrl, className) => {
const containClassName = element.classList.contains(className)
const needOperation = ctrl === 'add' ? !containClassName : containClassName
return needOperation && element.classList[ctrl](className)
}
export const findNearestParagraph = node => { export const findNearestParagraph = node => {
do { do {
if (isAganippeParagraph(node)) return node if (isAganippeParagraph(node)) return node