Skip to content

Commit

Permalink
feat: 功能扩展,(在配置打印样式后)有纯净的阅读视图(按ESC键可退出);
Browse files Browse the repository at this point in the history
  • Loading branch information
yaohaixiao committed Aug 24, 2023
1 parent d987e1f commit 966c113
Show file tree
Hide file tree
Showing 22 changed files with 247 additions and 110 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ AnchorJS 是 outline.js 的创作灵感来源。既然 AnchorJS 可创建标题
* 支持针对(github 项目的)API 文档的 tags 和 issues 等按钮的跳转;
* 支持自定义图标的自定义按钮,并且支持配置自定义按钮的触发事件和事件处理器;
- 自动为文章页面添加通用的打印样式;
- (在配置打印样式后)有纯净的阅读视图(按ESC键可退出);
- 可以作为 jQuery 插件使用;
- 界面简洁大方;
- 配置灵活,丰富,让你随心所欲掌控 outline.js;
Expand Down
2 changes: 1 addition & 1 deletion anchors.min.js.map

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions api/less/example.less
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
@import './colors';
@import '../../src/theme/shadow';
@import './scrollbar';
@import './section';
@import './case';
@import './pager';
@import '../../src/theme/icons';
@import '../../src/theme/drawer';
@import '../../src/theme/anchors';
@import '../../src/theme/chapters';
@import '../../src/theme/toolbar';
@import '../../src/theme/print';
@import '../../src/theme/outline';
2 changes: 1 addition & 1 deletion chapters.min.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions docs/css/docs.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/css/docs.min.css

Large diffs are not rendered by default.

162 changes: 109 additions & 53 deletions docs/css/example.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/css/example.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/outline.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/outline.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion drawer.min.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions outline.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions outline.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion outline.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion outline.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yaohaixiao/outline.js",
"version": "3.18.1",
"version": "3.19.0",
"description": "outline.js - 自动生成文章导读(Table of Contents)导航的 JavaScript 工具。",
"main": "outline.min.js",
"files": [
Expand Down
104 changes: 85 additions & 19 deletions src/outline.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import scrollTo from './utils/dom/scrollTo'
import _getScrollElement from './utils/dom/_getScrollElement'
import subscribe from './utils/observer/on'
import unsubscribe from './utils/observer/off'
import at from './utils/event/at'
import off from './utils/event/off'
import stop from './utils/event/stop'

import print from './print'

Expand Down Expand Up @@ -350,31 +353,64 @@ class Outline extends Base {
return this
}

doReading() {
enterReading() {
const READING = 'outline-reading'
const HIDDEN = `${READING}_hidden`
const $reading = document.querySelector('#outline-print')
const $siblings = document.querySelectorAll('.outline-print_sibling')

if (!this.reading) {
$siblings.forEach(($sibling) => {
addClass($sibling, HIDDEN)
})
addClass($reading, READING)
this.reading = true
} else {
removeClass($reading, READING)
$siblings.forEach(($sibling) => {
removeClass($sibling, HIDDEN)
})
this.reading = false
if (this.reading || !$reading) {
return this
}

$siblings.forEach(($sibling) => {
addClass($sibling, HIDDEN)
})
addClass($reading, READING)
this.reading = true

this.toolbar.toggle()

return this
}

exitReading() {
const READING = 'outline-reading'
const HIDDEN = `${READING}_hidden`
const $reading = document.querySelector('#outline-print')
const $siblings = document.querySelectorAll('.outline-print_sibling')

if (!this.reading || !$reading) {
return this
}

removeClass($reading, READING)
$siblings.forEach(($sibling) => {
removeClass($sibling, HIDDEN)
})
this.reading = false

this.toolbar.toggle()

return this
}

switchReading() {
const $print = document.querySelector('#outline-print')

if (!$print) {
return this
}

if (!this.reading) {
this.enterReading()
} else {
this.exitReading()
}

return this
}

toggle() {
const position = this.attr('position')
const toolbar = this.toolbar
Expand Down Expand Up @@ -411,9 +447,14 @@ class Outline extends Base {
let toolbar = this.toolbar
let isOutside = false
const count = this.count()
const $print = document.querySelector('#outline-print')

this.removeListeners()

if ($print) {
document.body.removeChild($print)
}

if (count > 0) {
isOutside = chapters.isOutside()

Expand Down Expand Up @@ -442,11 +483,6 @@ class Outline extends Base {
return this
}

onReading() {
this.doReading()
return this
}

onScrollTop() {
this.toTop()
return this
Expand All @@ -457,6 +493,22 @@ class Outline extends Base {
return this
}

onEnterReading() {
this.switchReading()
return this
}

onExitReading(evt) {
const keyCode = evt.keyCode

if (keyCode === 27 && this.reading) {
this.switchReading()
stop(evt)
}

return this
}

onToolbarUpdate({ top, min, max }) {
const toolbar = this.toolbar
const current = Math.ceil(top)
Expand All @@ -476,19 +528,33 @@ class Outline extends Base {
}

addListeners() {
const $print = document.querySelector('#outline-print')

subscribe('toolbar:update', this.onToolbarUpdate, this)
subscribe('toolbar:action:up', this.onScrollTop, this)
subscribe('toolbar:action:toggle', this.onToggle, this)
subscribe('toolbar:action:reading', this.onReading, this)
subscribe('toolbar:action:reading', this.onEnterReading, this)
subscribe('toolbar:action:down', this.onScrollBottom, this)

if ($print) {
at(document, 'keyup', this.onExitReading, this, true)
}

return this
}

removeListeners() {
const $print = document.querySelector('#outline-print')

unsubscribe('toolbar:update')
unsubscribe('toolbar:action:up')
unsubscribe('toolbar:action:toggle')
unsubscribe('toolbar:action:down')

if ($print) {
off(document, 'keyup', this.onExitReading)
}

return this
}
}
Expand Down
21 changes: 14 additions & 7 deletions src/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import later from './utils/lang/later'

const print = (origins, title) => {
let $origins
let $warpper
let $article
let $title
let $sibling
Expand Down Expand Up @@ -34,8 +35,8 @@ const print = (origins, title) => {
$article = createElement(
'article',
{
id: 'outline-print',
className: 'outline-print'
id: 'outline-print__article',
className: 'outline-print__article'
},
['']
)
Expand All @@ -49,12 +50,18 @@ const print = (origins, title) => {
[text]
)

$article.insertBefore($title, $article.firstChild)

document.body.appendChild($article)
$warpper = createElement(
'section',
{
id: 'outline-print',
className: 'outline-print'
},
[$title, $article]
)
document.body.appendChild($warpper)

later(() => {
$sibling = $article.previousElementSibling
$sibling = $warpper.previousElementSibling

while ($sibling) {
tagName = $sibling.tagName.toLowerCase()
Expand All @@ -64,7 +71,7 @@ const print = (origins, title) => {
$sibling = $sibling.previousElementSibling
}

$sibling = $article.nextElementSibling
$sibling = $warpper.nextElementSibling

while ($sibling) {
tagName = $sibling.tagName.toLowerCase()
Expand Down
4 changes: 2 additions & 2 deletions src/theme/print.less
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

&__title {
text-align: center;
font-size: 20pt;
font-family: 'Microsoft YaHei UI', Arial, sans-serif;
font-weight: normal;
overflow: hidden;
}
}

Expand All @@ -27,7 +27,7 @@
padding: 0 !important;
overflow: hidden !important;

&__title {
.outline-print__title {
font-size: 20pt !important;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/theme/reading.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
overflow: auto;

.outline-print__title {
line-height: 1.3em;
font-size: 3.5em;
}

&_hidden {
display: none;

&.outline-print_sibling {
display: none;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/event/off.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import _off from './_off'
* 如果没有设置 handler,则销毁 this.$options 绑定的所有符合 type 事件类型的事件绑定
* ========================================================================
* @method off
* @param {HTMLElement} el - (必须)取消事件绑定的 DOM 元素
* @param {HTMLElement|Object} el - (必须)取消事件绑定的 DOM 元素
* @param {String} type - (必须)事件类型
* @param {Function} [fn] - (可选)事件处理器回调函数
*/
Expand Down
2 changes: 1 addition & 1 deletion toolbar.min.js.map

Large diffs are not rendered by default.

0 comments on commit 966c113

Please sign in to comment.