1 Star 0 Fork 34

durandal/outline.js

forked from Yaohaixiao/outline.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
print.js 2.24 KB
一键复制 编辑 原始数据 按行查看 历史
import isString from './utils/types/isString'
import isElement from './utils/types/isElement'
import createElement from './utils/dom/createElement'
import addClass from './utils/dom/addClass'
import later from './utils/lang/later'
import icon from './utils/icons/icon'
const _updateSiblingElements = (siblingElement, isPrev) => {
let tagName
let $sibling = siblingElement
while ($sibling) {
tagName = $sibling.tagName.toLowerCase()
if (tagName !== 'script' && tagName !== 'style') {
addClass($sibling, 'outline-print_sibling')
}
if (isPrev) {
$sibling = $sibling.previousElementSibling
} else {
$sibling = $sibling.nextElementSibling
}
}
}
const paintPrint = (el, title) => {
const $fragment = document.createDocumentFragment()
let text = title
let $el
let $wrapper
let $article
let $title
let $sibling
let $icon
let $children
if (isString(el)) {
$el = document.querySelector(el)
} else if (isElement(el)) {
$el = el
}
if (!$el) {
return false
}
$icon = icon('close', {
iconSet: 'outline',
size: 20,
attrs: {
className: 'outline-print__close'
}
})
$title = $el.querySelector('h1')
if (isElement(title)) {
$title = title
}
if (isElement($title)) {
text = $title.innerText
}
$title = createElement(
'h1',
{
className: 'outline-print__title'
},
text
)
$article = createElement('article', {
id: 'outline-print__article',
className: 'outline-print__article'
})
$wrapper = createElement(
'section',
{
id: 'outline-print',
className: 'outline-print'
},
[$icon, $title, $article]
)
document.body.appendChild($wrapper)
later(() => {
// 设置邻居节点的打印样式
$sibling = $wrapper.previousElementSibling
_updateSiblingElements($sibling, true)
$sibling = $wrapper.nextElementSibling
_updateSiblingElements($sibling)
// 克隆文章内容
$children = [...$el.cloneNode(true).children]
$children.forEach(($child) => {
$fragment.appendChild($child)
})
$article.appendChild($fragment)
})
}
export default paintPrint
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/ddal/outline.js.git
git@gitee.com:ddal/outline.js.git
ddal
outline.js
outline.js
master

搜索帮助