Skip to content

Commit

Permalink
更新版本为 0.0.3
Browse files Browse the repository at this point in the history
文档树更多菜单可以切换二级文档列表。
  • Loading branch information
Misuzu2027 committed Sep 21, 2024
1 parent 8d689fc commit 2d45847
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ jobs:
artifactErrorsFailBuild: true
artifacts: "package.zip"
token: ${{ secrets.GITHUB_TOKEN }}
prerelease: true
prerelease: false
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

### 功能介绍
1. 点击文档树中的的笔记本或文档会切换(刷新)文档列表。
2. 双击文档列表的文档,会在文档树中定位该文档。
2. 双击文档列表的文档,会在文档树中定位该文档;如果该文档存在子文档,同时会切换路径。
3. 文档列表中的文档支持拖拽到文档树进行移动,同时支持按紧 Ctrl + 单击 实现多选后拖拽。(不支持文档树拖拽到文档列表)

#### 何时会切换路径
1. 点击笔记本名称或文档名称。
Expand All @@ -13,7 +14,6 @@




### 注意事项

该插件使用两种搜索方式,一种是“数据库查询”,另一种是“根据路径获取文档接口”;
Expand Down
5 changes: 3 additions & 2 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

### 功能介绍
1. 点击文档树中的的笔记本或文档会切换(刷新)文档列表。
2. 双击文档列表的文档,会在文档树中定位该文档。
2. 双击文档列表的文档,会在文档树中定位该文档;如果该文档存在子文档,同时会切换路径。
3. 文档列表中的文档支持拖拽到文档树进行移动,同时支持按紧 Ctrl + 单击 实现多选后拖拽。(不支持文档树拖拽到文档列表)
4. 可以在文档树的更多菜单,切换二级文档列表的显示。

#### 何时会切换路径
1. 点击笔记本名称或文档名称。
Expand All @@ -13,7 +15,6 @@




### 注意事项

该插件使用两种搜索方式,一种是“数据库查询”,另一种是“根据路径获取文档接口”;
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "syplugin-dual-doc-list",
"author": "Misuzu2027",
"url": "https://github.com/Misuzu2027/syplugin-dual-doc-list",
"version": "0.0.2",
"version": "0.0.3",
"minAppVersion": "3.0.12",
"backends": [
"all"
Expand Down
94 changes: 87 additions & 7 deletions src/components/doc-list/DocListManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import DocList from "@/components/doc-list/doc-list.svelte";
import { SettingConfig } from "@/models/setting-model";
import { SettingService } from "@/service/setting/SettingService";
import { findParentElementWithAttribute, getAttributeRecursively } from "@/utils/html-util";
import { convertTextToFirstElement, findParentElementWithAttribute, getAttributeRecursively } from "@/utils/html-util";
import Instance from "@/utils/Instance";
import { isElementHidden } from "@/utils/siyuan-util";
import { isStrNotBlank } from "@/utils/string-util";

const EmbedDualDocListElementAttrName = "data-misuzu2027-embed-dualDocList";
export class DocListManager {
Expand All @@ -19,13 +22,15 @@ export class DocListManager {
init() {
this.initInterval();
this.initElementEventListener();
addObserveCommonMenuElement();
}


destroy() {
this.destroyInterval();
this.destroyElementEventListener();
this.destroyEmbedDualDocList();
destroyObserveCommonMenuElement();
}

initElementEventListener() {
Expand All @@ -38,7 +43,7 @@ export class DocListManager {

initInterval() {
this.destroyInterval();
this.checkEmbedIntervalId = setInterval(() => this.intervalCheckEmbedDualDocTree(), 800)
this.checkEmbedIntervalId = setInterval(() => this.intervalCheckEmbedDualDocList(), 800)
}

destroyInterval() {
Expand All @@ -48,7 +53,7 @@ export class DocListManager {
}
}

intervalCheckEmbedDualDocTree() {
intervalCheckEmbedDualDocList() {
let showEmbedDualDocList = SettingService.ins.SettingConfig.showEmbedDualDocList;
if (showEmbedDualDocList) {
this.createEmbedDualDocList();
Expand Down Expand Up @@ -97,7 +102,6 @@ export class DocListManager {
docListElement.setAttribute("data-id", docTreeId);
docListElement.setAttribute(EmbedDualDocListElementAttrName, "1");
docListElement.classList.add("fn__flex-1");

docListElement.style.flex = listViewFlex;

docListElement.addEventListener("click", (event) => {
Expand All @@ -109,6 +113,9 @@ export class DocListManager {
} else {
fileTreeDocElement.before(docListElement);
}
if (isElementHidden(fileTreeDocElement)) {
docListElement.classList.add("fn__none");
}


this.embedDocListSvelte = new DocList({
Expand Down Expand Up @@ -160,16 +167,19 @@ export class DocListManager {
}
// 如果是笔记本,判断一下是否启用双击切换文档折叠。
if (targetLiElementType == "navigation-root") {
this.handleNotebookDoubleClick(event);
if (this.handleNotebookDoubleClick(event)) {
return;
}
}

this.handleSelectDoc(targetLiElement)
}

private handleNotebookDoubleClick(event: MouseEvent): void {
// return : 是否双击
private handleNotebookDoubleClick(event: MouseEvent): boolean {
let settingConfig = SettingService.ins.SettingConfig;
if (!settingConfig || !settingConfig.doubleClickToggleNotebook) {
return;
return false;
}
this.clickCount++;
let doubleClickTimeout = settingConfig.doubleClickTimeout;
Expand All @@ -179,7 +189,9 @@ export class DocListManager {
setTimeout(() => {
this.clickCount = 0;
}, doubleClickTimeout);
return false;
}
return true;
}

handleSelectDoc(targetLiElement: HTMLElement) {
Expand All @@ -204,3 +216,71 @@ export class DocListManager {


}

let observerCommonMenuElement: MutationObserver;

function addObserveCommonMenuElement() {
let protyleUtilElement = document.querySelector("#commonMenu > div.b3-menu__items");
if (protyleUtilElement.getAttribute("data-misuzu2027-observed") == "1") {
return;
}
if (observerCommonMenuElement) {
observerCommonMenuElement.disconnect;
}

// 创建一个 MutationObserver 实例,并传入回调函数
observerCommonMenuElement = new MutationObserver((mutationsList, observer) => {

let childNodes = protyleUtilElement.childNodes;
if (childNodes.length == 3
&& childNodes[0].childNodes[1].textContent == "新建笔记本") {
createSwitchEmbedDualDocListButtonEle();
}

return;
});

// 配置 MutationObserver 监听的类型
const config = { childList: true, };
protyleUtilElement.setAttribute("data-misuzu2027-observed", "1")
// 开始观察目标节点
observerCommonMenuElement.observe(protyleUtilElement, config);
}

function destroyObserveCommonMenuElement() {
observerCommonMenuElement.disconnect();
}


function createSwitchEmbedDualDocListButtonEle() {
let menuElement = document.querySelector("#commonMenu > div.b3-menu__items");
if (!menuElement) {
return;
}
let showEmbedDualDocList = SettingService.ins.SettingConfig.showEmbedDualDocList;

let switchDocListButtonEle = document.createElement("button");
switchDocListButtonEle.classList.add("b3-menu__item");
let svgElement = document.createElement("svg");
svgElement.classList.add("b3-menu__icon");
let spanElement = document.createElement("span");
spanElement.classList.add("b3-menu__label");
spanElement.textContent = "二级文档列表";
if (showEmbedDualDocList) {
svgElement.innerHTML = (`<use xlink:href="#iconSelect"></use>`);
}

switchDocListButtonEle.append(svgElement, spanElement);
switchDocListButtonEle.addEventListener("click", (event) => {
event.stopPropagation();
event.preventDefault();
SettingService.ins.updateSettingCofnigValue("showEmbedDualDocList", !showEmbedDualDocList)
DocListManager.ins.intervalCheckEmbedDualDocList();
window.siyuan.menus.menu.remove();
})

menuElement.append(switchDocListButtonEle);

}


36 changes: 30 additions & 6 deletions src/components/doc-list/doc-list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
isStrNotBlank,
splitKeywordStringToArray,
} from "@/utils/string-util";
import { getBlockByID } from "@/utils/api";
import { getBlockByID, getDocInfo } from "@/utils/api";
import { SiyuanConstants } from "@/models/siyuan-constant";
import { convertNumberToSordMode } from "@/utils/siyuan-util";
import { isTouchDevice } from "@/libs/siyuan/functions";
Expand Down Expand Up @@ -229,6 +229,8 @@
curPathNotebookId = null;
curPathDocId = null;
curPathDocPath = null;
curPathSortMethod =
SettingService.ins.SettingConfig.defaultDbQuerySortOrder;
updateDocList(
curPathNotebookId,
curPathDocId,
Expand Down Expand Up @@ -282,18 +284,20 @@
function handleClickLogic(event: MouseEvent, blockId: string) {
clearItemFocus();
clickCount++;
let doubleClickTimeout =
SettingService.ins.SettingConfig.doubleClickTimeout;
if (clickCount === 1) {
const tabPosition = determineTabPosition(event);
openBlockTab(blockId, tabPosition);
clickTimeoutId = setTimeout(() => {
clickCount = 0; // 重置计数
}, 300);
}, doubleClickTimeout);
} else {
clickCount = 0;
clearTimeout(clickTimeoutId);
performDoubleClickAction();
performDoubleClickAction(blockId);
}
}
Expand All @@ -309,14 +313,34 @@
return null;
}
function performDoubleClickAction() {
async function performDoubleClickAction(blockId: string) {
const focusSpanElement = document.querySelector(
`#layouts div.file-tree.sy__file > div.block__icons > span[data-type="focus"]`,
) as HTMLElement;
if (focusSpanElement) {
focusSpanElement.click();
}
// 实现双击进入这个路径
let docItemInfo: DocumentTreeItemInfo;
for (const docItem of documentItems) {
if (docItem.fileBlock.id == blockId) {
docItemInfo = docItem;
break;
}
}
if (docItemInfo) {
let subFileCount = docItemInfo.fileBlock.subFileCount;
if (subFileCount === undefined || subFileCount === null) {
let docInfo = await getDocInfo(blockId);
subFileCount = docInfo.subFileCount;
}
if (subFileCount && subFileCount > 0) {
let fileBlock = docItemInfo.fileBlock;
searchInputKey = "";
switchPath(fileBlock.box, blockId, fileBlock.path);
}
}
}
function clearItemFocus() {
Expand Down Expand Up @@ -604,7 +628,7 @@
}
function handleSearchInputChange(event) {
console.log("handleSearchInputChange searchInputKey ", searchInputKey);
// console.log("handleSearchInputChange searchInputKey ", searchInputKey);
// if (event.isComposing) {
// return;
// }
Expand All @@ -631,7 +655,7 @@
async function afterOpenDocTab(docTabPromise: Promise<ITab>) {
let docTab = await docTabPromise;
console.log("afterOpenDocTab");
// console.log("afterOpenDocTab");
let lastDocumentContentElement = docTab.panelElement
.children[1] as HTMLElement;
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ export default class PluginSample extends Plugin {

}

async onunload() { }
async onunload() {
DocListManager.ins.destroy();
}

uninstall() { }
uninstall() {
DocListManager.ins.destroy();
}

openSetting(): void {
openSettingsDialog();
Expand Down
2 changes: 0 additions & 2 deletions src/models/document-model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

export class DocumentTreeItemInfo {
fileBlock: FileBlock;
fileName: string;
filePath: string;
ariaLabel: string;
icon: string;
boxName: string;
Expand Down
4 changes: 3 additions & 1 deletion src/models/setting-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export class SettingConfig {
defaultDbQuerySortOrder: DocumentSortMode;
// 显示所有文档时的数量限制
allDocsQueryLimit: number;

// 记忆文档的排序方式。

// 清空所有文档记忆的排序方式。



Expand Down
2 changes: 1 addition & 1 deletion src/service/setting/SettingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Instance from "@/utils/Instance";
import { setReplacer } from "@/utils/json-util";
import { mergeObjects } from "@/utils/object-util";

const SettingFileName = 'backlink-panel-setting.json';
const SettingFileName = 'dual-doc-list-setting.json';

export class SettingService {

Expand Down
7 changes: 7 additions & 0 deletions src/utils/html-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,10 @@ export function findParentElementWithAttribute(element: HTMLElement, types: stri
}
return null;
}


export function convertTextToFirstElement(htmlString: string): HTMLElement {
const template = document.createElement('template');
template.innerHTML = htmlString.trim(); // 去除可能的空白符
return template.content.firstElementChild as HTMLElement;
}
15 changes: 14 additions & 1 deletion src/utils/siyuan-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,17 @@ export function convertNumberToSordMode(sortCode: number): DocumentSortMode {
}
return sortMode;

}
}

export function isElementHidden(element: Element) {
if (!element || element.tagName === "BODY") {
return false;
}

if (element.classList.contains("fn__none")) {
return true;
}

return isElementHidden(element.parentElement);
}

0 comments on commit 2d45847

Please sign in to comment.