1 Star 0 Fork 34

sky/outline.js

forked from Yaohaixiao/outline.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_paintChapters.js 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
import createElement from './utils/dom/createElement'
import timeSlice from './utils/lang/timeSlice'
const _paintChapters = ($list, chapters, showCode = false) => {
const LIMIT = 400
const count = chapters.length
const clones = [...chapters]
const paint = (parts) => {
const byId = (id) => $list.querySelector(`#${id}`)
parts.forEach((chapter) => {
const pid = chapter.pid
const id = chapter.id
const rel = chapter.rel
const children = []
const $text = createElement(
'span',
{
className: 'outline-chapters__text'
},
chapter.text
)
let $link
let $code
let $li
let $subject
let $chapter
if (showCode) {
$code = createElement(
'span',
{
className: 'outline-chapters__code',
'data-id': id
},
chapter.code
)
children.push($code)
}
children.push($text)
$link = createElement(
'a',
{
id: `chapter__anchor-${id}`,
className: 'outline-chapters__anchor',
href: '#' + rel,
rel: rel,
'data-id': id
},
children
)
$li = createElement(
'li',
{
id: `chapter-${id}`,
className: 'outline-chapters__item',
'data-id': id
},
$link
)
if (pid === -1) {
$list.appendChild($li)
} else {
$chapter = byId(`chapter-${pid}`)
$subject = byId(`subject-${pid}`)
if (!$subject) {
$subject = createElement(
'ul',
{
id: 'subject-' + pid,
className: 'outline-chapters__subject'
},
$li
)
$chapter.appendChild($subject)
} else {
$subject.appendChild($li)
}
}
})
}
if (count > LIMIT) {
// 同步绘制
paint(clones.splice(0, LIMIT))
// 剩余的采用 timeSlice 机制绘制
while (clones.length > 0) {
const once = clones.splice(0, LIMIT)
timeSlice(() => {
paint(once)
})
}
} else {
paint(clones)
}
}
export default _paintChapters
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/scrsxx/outline.js.git
git@gitee.com:scrsxx/outline.js.git
scrsxx
outline.js
outline.js
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385