@@ -10,44 +10,39 @@ import {focusByRange} from "../protyle/util/selection";
1010import { hasClosestByClassName } from "../protyle/util/hasClosest" ;
1111import { hideElements } from "../protyle/ui/hideElements" ;
1212
13- const getHTML = async ( data : {
13+ const renderRecentDocsContent = async ( data : {
1414 rootID : string ,
1515 icon : string ,
1616 title : string ,
1717 viewedAt ?: number ,
1818 closedAt ?: number ,
1919 openAt ?: number ,
20- updated ?: number
21- } [ ] , element : Element , key ?: string , sortBy : TRecentDocsSort = "viewedAt" ) => {
20+ } [ ] , element : Element , key ?: string ) => {
2221 let tabHtml = "" ;
2322 let index = 0 ;
2423
25- // 根据排序字段对数据进行排序
26- const sortedData = [ ...data ] . sort ( ( a , b ) => {
27- const aValue = a [ sortBy ] || 0 ;
28- const bValue = b [ sortBy ] || 0 ;
29- return bValue - aValue ; // 降序排序
30- } ) ;
31-
32- sortedData . forEach ( ( item ) => {
33- if ( ! key || item . title . toLowerCase ( ) . includes ( key . toLowerCase ( ) ) ) {
34- tabHtml += `<li data-index="${ index } " data-node-id="${ item . rootID } " class="b3-list-item${ index === 0 ? " b3-list-item--focus" : "" } ">
35- ${ unicode2Emoji ( item . icon || window . siyuan . storage [ Constants . LOCAL_IMAGES ] . file , "b3-list-item__graphic" , true ) }
36- <span class="b3-list-item__text">${ escapeHtml ( item . title ) } </span>
24+ if ( key ) {
25+ data = data . filter ( ( item ) => {
26+ return item . title . toLowerCase ( ) . includes ( key . toLowerCase ( ) ) ;
27+ } ) ;
28+ }
29+ data . forEach ( ( item ) => {
30+ tabHtml += `<li data-index="${ index } " data-node-id="${ item . rootID } " class="b3-list-item${ index === 0 ? " b3-list-item--focus" : "" } ">
31+ ${ unicode2Emoji ( item . icon || window . siyuan . storage [ Constants . LOCAL_IMAGES ] . file , "b3-list-item__graphic" , true ) }
32+ <span class="b3-list-item__text">${ escapeHtml ( item . title ) } </span>
3733</li>` ;
38- index ++ ;
39- }
34+ index ++ ;
4035 } ) ;
4136 let switchPath = "" ;
4237 if ( tabHtml ) {
4338 const pathResponse = await fetchSyncPost ( "/api/filetree/getFullHPathByID" , {
44- id : data [ 0 ] . rootID
39+ id : data [ 0 ] . rootID // 过滤后的第一个文档 ID
4540 } ) ;
4641 switchPath = escapeHtml ( pathResponse . data ) ;
4742 }
4843 let dockHtml = "" ;
4944 if ( ! isWindow ( ) ) {
50- dockHtml = '<ul class="b3-list b3-list--background" style="overflow: auto;width: 200px;">' ;
45+ let docIndex = 0 ;
5146 if ( ! key || window . siyuan . languages . riffCard . toLowerCase ( ) . includes ( key . toLowerCase ( ) ) ) {
5247 dockHtml += `<li data-type="riffCard" data-index="0" class="b3-list-item${ ! switchPath ? " b3-list-item--focus" : "" } ">
5348 <svg class="b3-list-item__graphic"><use xlink:href="#iconRiffCard"></use></svg>
@@ -57,30 +52,30 @@ ${unicode2Emoji(item.icon || window.siyuan.storage[Constants.LOCAL_IMAGES].file,
5752 if ( ! switchPath ) {
5853 switchPath = window . siyuan . languages . riffCard ;
5954 }
55+ docIndex ++ ;
6056 }
61- let docIndex = 1 ;
6257 getAllDocks ( ) . forEach ( ( item ) => {
6358 if ( ! key || item . title . toLowerCase ( ) . includes ( key . toLowerCase ( ) ) ) {
6459 dockHtml += `<li data-type="${ item . type } " data-index="${ docIndex } " class="b3-list-item${ ! switchPath ? " b3-list-item--focus" : "" } ">
6560 <svg class="b3-list-item__graphic"><use xlink:href="#${ item . icon } "></use></svg>
6661 <span class="b3-list-item__text">${ item . title } </span>
67- <span class="b3-list-item__meta">${ updateHotkeyTip ( item . hotkey || "" ) } </span>
62+ <span class="b3-list-item__meta">${ updateHotkeyTip ( item . hotkey ) } </span>
6863</li>` ;
69- docIndex ++ ;
7064 if ( ! switchPath ) {
71- switchPath = window . siyuan . languages . riffCard ;
65+ switchPath = item . title ;
7266 }
67+ docIndex ++ ;
7368 }
7469 } ) ;
75- dockHtml = dockHtml + "</ul>" ;
70+ dockHtml = '<ul class="b3-list b3-list--background" style="overflow: auto;width: 200px;">' + dockHtml + "</ul>" ;
7671 }
7772
7873 const pathElement = element . querySelector ( ".switch-doc__path" ) ;
7974 pathElement . innerHTML = switchPath ;
80- pathElement . previousElementSibling . innerHTML = `<div class="fn__flex fn__flex-1" style="overflow:auto;">
81- ${ dockHtml }
82- <ul style="${ isWindow ( ) ? "border-left:0;" : "" } min-width:360px;" class="b3-list b3-list--background fn__flex-1">${ tabHtml } </ul>
83- </div>`;
75+ pathElement . previousElementSibling . innerHTML = `<div class="fn__flex fn__flex-1" style="overflow: auto;">
76+ ${ dockHtml }
77+ <ul style="${ isWindow ( ) ? "border-left: 0;" : "" } min-width: 360px;" class="b3-list b3-list--background fn__flex-1">${ tabHtml } </ul>
78+ </div>` ;
8479} ;
8580
8681export const openRecentDocs = ( ) => {
@@ -93,7 +88,8 @@ export const openRecentDocs = () => {
9388 hideElements ( [ "dialog" ] ) ;
9489 return ;
9590 }
96- fetchPost ( "/api/storage/getRecentDocs" , { sortBy : "viewedAt" } , ( response ) => {
91+ const sortBy = window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] . type ;
92+ fetchPost ( "/api/storage/getRecentDocs" , { sortBy} , ( response ) => {
9793 let range : Range ;
9894 if ( getSelection ( ) . rangeCount > 0 ) {
9995 range = getSelection ( ) . getRangeAt ( 0 ) ;
@@ -110,15 +106,15 @@ export const openRecentDocs = () => {
110106<span class="fn__space"></span>
111107<div class="fn__flex-center">
112108 <select class="b3-select" id="recentDocsSort">
113- <option value="viewedAt"${ window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] . type === "viewedAt" ? " selected" : "" } >${ window . siyuan . languages . recentViewed } </option>
114- <option value="updated"${ window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] . type === "updated" ? " selected" : "" } >${ window . siyuan . languages . recentModified } </option>
115- <option value="openAt"${ window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] . type === "openAt" ? " selected" : "" } >${ window . siyuan . languages . recentOpened } </option>
116- <option value="closedAt"${ window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] . type === "closedAt" ? " selected" : "" } >${ window . siyuan . languages . recentClosed } </option>
109+ <option value="viewedAt">${ window . siyuan . languages . recentViewed } </option>
110+ <option value="updated">${ window . siyuan . languages . recentModified } </option>
111+ <option value="openAt">${ window . siyuan . languages . recentOpened } </option>
112+ <option value="closedAt">${ window . siyuan . languages . recentClosed } </option>
117113 </select>
118114</div>
119115</div>` ,
120116 content : `<div class="fn__flex-column switch-doc">
121- <div class="fn__flex fn__flex-1" style="overflow:auto;"></div>
117+ <div class="fn__flex fn__flex-1" style="overflow: auto;"></div>
122118 <div class="switch-doc__path"></div>
123119</div>` ,
124120 height : "80vh" ,
@@ -128,16 +124,18 @@ export const openRecentDocs = () => {
128124 }
129125 }
130126 } ) ;
127+ const sortSelect = dialog . element . querySelector ( "#recentDocsSort" ) as HTMLSelectElement ;
128+ sortSelect . value = sortBy ;
131129 const searchElement = dialog . element . querySelector ( "input" ) ;
132130 searchElement . focus ( ) ;
133131 searchElement . addEventListener ( "compositionend" , ( ) => {
134- getHTML ( response . data , dialog . element , searchElement . value , sortSelect . value as TRecentDocsSort ) ;
132+ renderRecentDocsContent ( response . data , dialog . element , searchElement . value ) ;
135133 } ) ;
136134 searchElement . addEventListener ( "input" , ( event : InputEvent ) => {
137135 if ( event . isComposing ) {
138136 return ;
139137 }
140- getHTML ( response . data , dialog . element , searchElement . value , sortSelect . value as TRecentDocsSort ) ;
138+ renderRecentDocsContent ( response . data , dialog . element , searchElement . value ) ;
141139 } ) ;
142140 dialog . element . setAttribute ( "data-key" , Constants . DIALOG_RECENTDOCS ) ;
143141 dialog . element . addEventListener ( "click" , ( event ) => {
@@ -152,45 +150,16 @@ export const openRecentDocs = () => {
152150 } ) ;
153151
154152 // 添加排序下拉框事件监听
155- const sortSelect = dialog . element . querySelector ( "#recentDocsSort" ) as HTMLSelectElement ;
156153 sortSelect . addEventListener ( "change" , ( ) => {
157- // 重新调用API获取排序后的数据
158- if ( sortSelect . value === "updated" ) {
159- // 使用SQL查询获取最近修改的文档
160- const data = {
161- stmt : "SELECT * FROM blocks WHERE type = 'd' ORDER BY updated DESC LIMIT 33"
162- } ;
163- fetchSyncPost ( "/api/query/sql" , data ) . then ( ( sqlResponse ) => {
164- if ( sqlResponse . data && sqlResponse . data . length > 0 ) {
165- // 转换SQL查询结果格式
166- const recentModifiedDocs = sqlResponse . data . map ( ( block : any ) => {
167- // 从ial中解析icon
168- let icon = "" ;
169- if ( block . ial ) {
170- const iconMatch = block . ial . match ( / i c o n = " ( [ ^ " ] * ) " / ) ;
171- if ( iconMatch ) {
172- icon = iconMatch [ 1 ] ;
173- }
174- }
175- return {
176- rootID : block . id ,
177- icon,
178- title : block . content ,
179- updated : block . updated
180- } ;
181- } ) ;
182- getHTML ( recentModifiedDocs , dialog . element , searchElement . value , "updated" ) ;
183- }
184- } ) ;
185- } else {
186- fetchPost ( "/api/storage/getRecentDocs" , { sortBy : sortSelect . value } , ( newResponse ) => {
187- getHTML ( newResponse . data , dialog . element , searchElement . value , sortSelect . value as TRecentDocsSort ) ;
188- } ) ;
189- }
154+ // 重新调用 API 获取排序后的数据
155+ fetchPost ( "/api/storage/getRecentDocs" , { sortBy : sortSelect . value } , ( newResponse ) => {
156+ response = newResponse ;
157+ renderRecentDocsContent ( newResponse . data , dialog . element , searchElement . value ) ;
158+ } ) ;
190159 window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] . type = sortSelect . value ;
191160 setStorageVal ( Constants . LOCAL_RECENT_DOCS , window . siyuan . storage [ Constants . LOCAL_RECENT_DOCS ] ) ;
192161 } ) ;
193162
194- getHTML ( response . data , dialog . element ) ;
163+ renderRecentDocsContent ( response . data , dialog . element ) ;
195164 } ) ;
196165} ;
0 commit comments