From b69bf1f0c7bd2d7ab0efaf0a9ba50081366408ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?sunsonliu=28=E5=88=98=E9=98=B3=29?= Date: Mon, 25 Nov 2024 10:23:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#897=20=E5=A2=9E=E5=8A=A0=E5=88=87?= =?UTF-8?q?=E6=8D=A2toc=E6=82=AC=E6=B5=AE=E7=9B=AE=E5=BD=95=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=9A=84api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/api.html | 12 ++++++++++++ src/Cherry.js | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/examples/api.html b/examples/api.html index 492e6103..4bd1b62f 100644 --- a/examples/api.html +++ b/examples/api.html @@ -249,6 +249,18 @@

setCodeBlockTheme(theme:string)

+
+

toggleToc(focusModel:{'full'|'pure'|''})

+

修改右侧悬浮目录的状态

+
+ + 试一试 +
+
+

diff --git a/src/Cherry.js b/src/Cherry.js index 2993ee1d..c114bcca 100644 --- a/src/Cherry.js +++ b/src/Cherry.js @@ -1064,4 +1064,26 @@ export default class Cherry extends CherryStatic { this.resetToolbar('toolbar', this.options.toolbars.toolbar || []); return true; } + + /** + * 切换TOC的模式(极简 or 展开) + * @param {'full'|'pure'|''} focusModel 是否强制切换模式,如果为空,则根据当前模式切换 + */ + toggleToc(focusModel = '') { + if (!this.toc) { + return; + } + let targetModel = 'full'; + if (focusModel === '') { + // @ts-ignore + const { model } = this.toc; + targetModel = model === 'full' ? 'pure' : 'full'; + } else { + targetModel = focusModel; + } + // @ts-ignore + this.toc.$switchModel(targetModel); + // @ts-ignore + this.toc.setModelToLocalStorage(targetModel); + } }