Skip to content

Commit

Permalink
修改版本为 0.0.8
Browse files Browse the repository at this point in the history
支持新建同级文档
  • Loading branch information
Misuzu2027 committed Oct 14, 2024
1 parent 2681ec7 commit c5f8efa
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 47 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"typescript": "^5.1.3",
"vite": "^5.0.0",
"vite-plugin-static-copy": "^1.0.2",
"vite-plugin-zip-pack": "^1.0.5"
"vite-plugin-zip-pack": "^1.0.5",
"path-browserify": "^1.0.1"
}
}
}
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.7",
"version": "0.0.8",
"minAppVersion": "3.0.12",
"backends": [
"all"
Expand Down
138 changes: 96 additions & 42 deletions src/components/doc-list/doc-list.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
isStrNotBlank,
splitKeywordStringToArray,
} from "@/utils/string-util";
import { getBlockByID, getDocInfo } from "@/utils/api";
import { createDoc, getBlockByID, getDocInfo } from "@/utils/api";
import { SiyuanConstants } from "@/models/siyuan-constant";
import { convertNumberToSordMode } from "@/utils/siyuan-util";
import {
convertNumberToSordMode,
getParentPath,
} from "@/utils/siyuan-util";
import { isTouchDevice } from "@/libs/siyuan/functions";
import { hasClosestByTag } from "@/libs/siyuan/hasClosest";
import { delayedTwiceRefresh } from "@/utils/timing-util";
import { SettingService } from "@/service/setting/SettingService";
import { PathHistory } from "@/models/PathHistory";
import { getDisplayName, pathPosix } from "@/libs/siyuan/pathName";
let rootElement: HTMLElement;
let lastSelectDocItemIndex: number = -1;
Expand All @@ -62,6 +66,7 @@
let curPathSortMethod: DocumentSortMode;
let curNotebookSortMethod: DocumentSortMode;
let waitRefreshByDatabase: boolean = false;
let waitRefreshSelectDocId: string = null;
onMount(async () => {
showSubDocOfSubDoc =
Expand Down Expand Up @@ -199,24 +204,12 @@
break;
case "databaseIndexCommit":
if (waitRefreshByDatabase) {
console.log("databaseIndexCommit");
refreshDocList();
}
break;
}
if (waitRefreshByDatabase) {
let showSubDocuments = false;
let fullTextSearch = false;
if (
isQueryDocByPathApi(
showSubDocuments,
curPathNotebookId,
curPathDocPath,
lastKeywords,
fullTextSearch,
)
) {
if (localIsQueryDocByPathApi()) {
refreshDocList();
}
}
Expand Down Expand Up @@ -312,6 +305,53 @@
switchPath(pathObj.notebookId, pathObj.docId, pathObj.docPath, false);
}
async function createSiblingDocClick(
event: MouseEvent,
item: DocumentTreeItemInfo,
) {
if (!event) return;
event.stopPropagation();
event.preventDefault();
if (!item) {
return;
}
let notebook = item.fileBlock.box;
let path = item.fileBlock.path;
if (isStrBlank(notebook) || isStrBlank(path)) {
return;
}
let parentPath = getParentPath(path);
let title = window.siyuan.languages.untitled;
const id = Lute.NewNodeID();
const newPath = pathPosix().join(
getDisplayName(parentPath, false, true),
id + ".sy",
);
let docId = (await createDoc(notebook, newPath, title, "", null)).id;
if (isStrBlank(docId)) {
return;
}
let actions: TProtyleAction[] = [
Constants.CB_GET_CONTEXT,
Constants.CB_GET_OPENNEW,
];
await openBlockTab(docId, null, actions);
if (localIsQueryDocByPathApi()) {
await refreshDocList();
docListSelectDocById(docId);
} else {
waitRefreshByDatabase = true;
waitRefreshSelectDocId = docId;
}
}
let clickTimeoutId: NodeJS.Timeout | undefined;
let clickCount: number = 0;
Expand Down Expand Up @@ -437,11 +477,11 @@
async function openBlockTab(
blockId: string,
tabPosition: "right" | "bottom",
actions?: TProtyleAction[],
) {
let actions: TProtyleAction[] = [
Constants.CB_GET_FOCUS,
Constants.CB_GET_SCROLL,
];
if (isArrayEmpty(actions)) {
actions = [Constants.CB_GET_FOCUS, Constants.CB_GET_SCROLL];
}
if (EnvConfig.ins.isMobile) {
openMobileFileById(EnvConfig.ins.app, blockId, actions);
Expand Down Expand Up @@ -560,7 +600,7 @@
return _getInstanceById(layout, id);
};
async function selectCurDoc() {
async function docListSelectCurDoc() {
const element =
document.querySelector(
".layout__wnd--active > .fn__flex > .layout-tab-bar > .item--focus",
Expand All @@ -586,9 +626,16 @@
let parentDocId = getDocIdByPath(parentDocPath);
await switchPath(notebookId, parentDocId, parentDocPath);
docListSelectDocById(docId);
}
function docListSelectDocById(docId: string) {
const docLiElement = rootElement.querySelector(
`li[data-node-id="${docId}"]`,
) as HTMLElement;
if (!docLiElement) {
return;
}
docLiElement.classList.add("b3-list-item--focus");
let docListElement = docLiElement.parentElement.parentElement;
Expand All @@ -608,26 +655,6 @@
}
}
function getParentPath(path: string): string {
if (isStrBlank(path)) {
return null;
}
// 将路径按斜杠分割
const parts = path.split("/");
if (parts.length <= 1) {
return "/"; // 如果没有多余的路径部分,返回根路径
}
// 获取倒数第二部分并保留其后缀
const secondLast = parts[parts.length - 2];
const last = parts[parts.length - 1];
// 提取后缀,合并成新的路径
const suffix = last.split(".").pop();
return `/${parts.slice(1, -2).join("/")}/${secondLast}.${suffix}`;
}
function getDocIdByPath(path: string) {
if (isStrBlank(path)) {
return null;
Expand Down Expand Up @@ -676,6 +703,10 @@
searchInputKey,
curPathSortMethod,
);
if (isStrBlank(waitRefreshSelectDocId)) {
docListSelectDocById(waitRefreshSelectDocId);
waitRefreshSelectDocId = null;
}
}
async function updateDocList(
Expand Down Expand Up @@ -964,6 +995,19 @@
item.remove();
});
}
function localIsQueryDocByPathApi(): boolean {
let showSubDocuments = showSubDocOfSubDoc;
let fullTextSearch = SettingService.ins.SettingConfig.fullTextSearch;
return isQueryDocByPathApi(
showSubDocuments,
curPathNotebookId,
curPathDocPath,
lastKeywords,
fullTextSearch,
);
}
</script>

<!-- svelte-ignore a11y-no-static-element-interactions -->
Expand All @@ -988,7 +1032,7 @@
class="block__icon ariaLabel"
aria-label="定位打开的文档 "
style="opacity: 1;"
on:click={selectCurDoc}
on:click={docListSelectCurDoc}
on:keydown={handleKeyDownDefault}
><svg><use xlink:href="#iconFocus"></use></svg></span
>
Expand Down Expand Up @@ -1141,7 +1185,7 @@
data-count={item.fileBlock.subFileCount}
data-type="navigation-file"
style="--file-toggle-width:40px;height:32px;padding:2px;"
class="b3-list-item"
class="b3-list-item b3-list-item--hide-action"
draggable="true"
data-path={item.fileBlock.path}
on:click={docItemClick}
Expand All @@ -1167,6 +1211,16 @@
{@html item.fileBlock.content}
</span>

<span
class="b3-list-item__action b3-tooltips b3-tooltips__nw"
aria-label="新建同级文档"
on:click={(event) => {
createSiblingDocClick(event, item);
}}
on:keydown={handleKeyDownDefault}
>
<svg><use xlink:href="#iconAdd"></use></svg>
</span>
{#if item.fileBlock.refCount}
<span
class="popover__block counter b3-tooltips b3-tooltips__nw"
Expand Down
21 changes: 21 additions & 0 deletions src/libs/siyuan/pathName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as path from "path-browserify";



export const pathPosix = () => {
if (path.posix) {
return path.posix;
}
return path;
};

export const getDisplayName = (filePath: string, basename = true, removeSY = false) => {
let name = filePath;
if (basename) {
name = pathPosix().basename(filePath);
}
if (removeSY && name.endsWith(".sy")) {
name = name.substr(0, name.length - 3);
}
return name;
};
Loading

0 comments on commit c5f8efa

Please sign in to comment.