feat: half done of task list item

This commit is contained in:
Jocs 2018-01-03 00:51:31 +08:00
parent 166dc2e271
commit b424b9d49e
4 changed files with 56 additions and 4 deletions

View File

@ -70,7 +70,8 @@ export const CLASS_OR_ID = genUpper2LowerKeyHash([
'AG_IMAGE_FAIL', 'AG_IMAGE_FAIL',
'AG_REMOVE', 'AG_REMOVE',
'AG_EMOJI_MARKER', 'AG_EMOJI_MARKER',
'AG_NOTEXT_LINK' 'AG_NOTEXT_LINK',
'AG_TASK_LIST_ITEM'
]) ])
export const codeMirrorConfig = { export const codeMirrorConfig = {

View File

@ -2,7 +2,7 @@ import selection from '../selection'
import { tokenizer } from '../parser/parse' import { tokenizer } from '../parser/parse'
import { conflict } from '../utils' import { conflict } from '../utils'
const INLINE_UPDATE_REG = /^([*+-]\s(\[\s\]\s)?)|^(\d+\.\s)|^(#{1,6})[^#]+|^(>).+|^(\*{3,}|-{3,}|_{3,})/ const INLINE_UPDATE_REG = /^([*+-]\s)|^(\[[x\s]{1}\]\s)|^(\d+\.\s)|^(#{1,6})[^#]+|^(>).+|^(\*{3,}|-{3,}|_{3,})/
const updateCtrl = ContentState => { const updateCtrl = ContentState => {
ContentState.prototype.checkNeedRender = function (block) { ContentState.prototype.checkNeedRender = function (block) {
@ -30,6 +30,7 @@ const updateCtrl = ContentState => {
ContentState.prototype.checkInlineUpdate = function (block) { ContentState.prototype.checkInlineUpdate = function (block) {
const { text } = block const { text } = block
const parent = this.getParent(block)
const [match, disorder, tasklist, order, header, blockquote, hr] = text.match(INLINE_UPDATE_REG) || [] const [match, disorder, tasklist, order, header, blockquote, hr] = text.match(INLINE_UPDATE_REG) || []
let newType let newType
@ -42,8 +43,8 @@ const updateCtrl = ContentState => {
this.updateList(block, 'disorder', disorder) this.updateList(block, 'disorder', disorder)
return true return true
case !!tasklist: case !!tasklist && parent && parent.type === 'li':
this.updateList(block, 'tasklist', disorder) // tasklist is one type of disorder. this.updateTaskListItem(block, 'tasklist', tasklist)
return true return true
case !!order: case !!order:
@ -75,7 +76,29 @@ const updateCtrl = ContentState => {
return false return false
} }
ContentState.prototype.updateTaskListItem = function (block, type, marker) {
const parent = this.getParent(block)
const checked = /\[x\]\s/.test(marker)
const checkbox = this.createBlock('input')
const { start, end } = this.cursor
checkbox.checked = checked
this.insertBefore(checkbox, block)
block.text = block.text.substring(marker.length)
parent.isTask = true
this.cursor = {
start: {
key: start.key,
offset: Math.max(0, start.offset - marker.length)
},
end: {
key: end.key,
offset: Math.max(0, end.offset - marker.length)
}
}
}
ContentState.prototype.updateList = function (block, type, marker) { ContentState.prototype.updateList = function (block, type, marker) {
console.log(type, marker)
const parent = this.getParent(block) const parent = this.getParent(block)
const preSibling = this.getPreSibling(block) const preSibling = this.getPreSibling(block)
const nextSibling = this.getNextSibling(block) const nextSibling = this.getNextSibling(block)

View File

@ -28,6 +28,21 @@ li > h1, li > h2, li > h3, li > h4, li > h5, li > h6 {
display: initial; display: initial;
} }
li.ag-task-list-item {
list-style-type: none;
position: relative;
}
li.ag-task-list-item > input[type=checkbox] {
position: absolute;
cursor: pointer;
width: inherit;
height: inherit;
margin: 4px 0px 0px;
top: 3px;
left: -20px;
}
li p .ag-hide:first-child { li p .ag-hide:first-child {
display: none; display: none;
} }

View File

@ -68,6 +68,9 @@ class StateRender {
if (block.type === 'ol') { if (block.type === 'ol') {
Object.assign(data.props, { start: block.start }) Object.assign(data.props, { start: block.start })
} }
if (block.type === 'li' && block.isTask) {
blockSelector += `.${CLASS_OR_ID['AG_TASK_LIST_ITEM']}`
}
return h(blockSelector, data, block.children.map(child => renderBlock(child))) return h(blockSelector, data, block.children.map(child => renderBlock(child)))
} else { } else {
let children = block.text let children = block.text
@ -91,6 +94,16 @@ class StateRender {
children = '' children = ''
} }
if (block.type === 'input') {
const { checked, type, key } = block
Object.assign(data.props, { type: 'checkbox' })
if (checked) {
Object.assign(data.props, { checked: true })
}
blockSelector = `${type}#${key}`
children = ''
}
if (block.temp) { if (block.temp) {
blockSelector += `.${CLASS_OR_ID['AG_TEMP']}` blockSelector += `.${CLASS_OR_ID['AG_TEMP']}`
} }