Skip to content

Commit 0ed46e2

Browse files
committed
msic
1 parent 8b5b6a7 commit 0ed46e2

File tree

10 files changed

+141
-24
lines changed

10 files changed

+141
-24
lines changed

example/exp-sql-executor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const query = async () => {
1818
dv.addele(button);
1919

2020
dv.addtable(searchResult(), {
21-
fullwidth: true,
21+
fullwidth: false,
22+
cols: null,
23+
renderer: (b, a) => b[a]
2224
});
2325

2426
button.onclick = async () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sy-query-view",
3-
"version": "1.0.0-beta6",
3+
"version": "1.0.0-beta7",
44
"type": "module",
55
"description": "This is a sample plugin based on vite and svelte for Siyuan (https://b3log.org/siyuan). Created with siyuan-plugin-cli v2.4.5.",
66
"repository": "https://github.com/frostime/sy-query-view",

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "sy-query-view",
33
"author": "frostime",
44
"url": "https://github.com/frostime/sy-query-view",
5-
"version": "1.0.0-beta6",
5+
"version": "1.0.0-beta7",
66
"minAppVersion": "3.1.14",
77
"backends": [
88
"windows",

public/types.d.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ declare const Query: {
126126
/**
127127
* Wraps blocks with additional functionality
128128
* @param blocks - Blocks to wrap
129+
* @param useWrapBlock - Whether to wrap blocks inside the WrappedList
129130
* @returns Wrapped block(s)
130131
*/
131-
wrapBlocks: (blocks: Block[] | Block) => IWrappedBlock | IWrappedList<IWrappedBlock>;
132+
wrapBlocks: (blocks: Block[] | Block, useWrapBlock?: boolean) => IWrappedBlock | IWrappedList<IWrappedBlock>;
132133
/**
133134
* SiYuan Kernel Request API
134135
* @example
@@ -193,6 +194,7 @@ declare const Query: {
193194
random: (limit?: number, type?: BlockType) => Promise<any[]>;
194195
/**
195196
* Gets the daily notes document
197+
* @param limit - Maximum number of results
196198
* @param notebook - Notebook ID, if not specified, all daily notes documents will be returned
197199
* @returns Array of daily notes document blocks
198200
*/
@@ -203,6 +205,7 @@ declare const Query: {
203205
* @returns Array of child document blocks
204206
*/
205207
childdoc: (b: BlockId | Block) => Promise<IWrappedList<IWrappedBlock>>;
208+
markdown: (block: Block) => Promise<any>;
206209
/**
207210
* Redirects first block IDs to their parent containers
208211
* @param inputs - Array of blocks or block IDs
@@ -215,6 +218,18 @@ declare const Query: {
215218
heading?: boolean;
216219
doc?: boolean;
217220
}) => Promise<IWrappedList<IWrappedBlock>>;
221+
/**
222+
* Send GPT request, use AI configuration in `siyuan.config.ai.openAI` by default
223+
* @param prompt - Prompt
224+
* @param options - Options
225+
* @param options.timeout - Request timeout
226+
* @param options.url - Custom API URL
227+
* @param options.model - Custom API model
228+
* @param options.apiKey - Custom API key
229+
* @param options.returnRaw - Whether to return raw response (default: false)
230+
* @param options.history - Chat history
231+
* @returns GPT response
232+
*/
218233
gpt: (prompt: string, options?: {
219234
timeout?: number;
220235
url?: string;
@@ -702,12 +717,12 @@ export interface IWrappedBlock extends Block {
702717
/** Original Block object */
703718
unwrapped: Block;
704719
/** Block's URI link in format: siyuan://blocks/xxx */
705-
asuri: string;
720+
asurl: string;
706721
/** Block's URI link in format: siyuan://blocks/xxx */
707-
touri: string;
708-
/** Block's Markdown format link */
722+
tourl: string;
723+
/** Block's Markdown format link [content](siyuan://blocks/xxx) */
709724
aslink: string;
710-
/** Block's Markdown format link */
725+
/** Block's Markdown format link [content](siyuan://blocks/xxx) */
711726
tolink: string;
712727
/** Block's SiYuan reference format text */
713728
asref: string;
@@ -717,6 +732,10 @@ export interface IWrappedBlock extends Block {
717732
* Returns a rendered SiYuan attribute
718733
* @param attr - Attribute name
719734
* @param renderer - Custom render function, uses default rendering when returns null
735+
* @returns {string} Rendered attribute value
736+
* @example
737+
* block.attr('box') // Returns the name of the notebook
738+
* block.attr('root_id') // Returns the block link of the document
720739
*/
721740
attr(attr: keyof Block, renderer?: (block: Block, attr: keyof Block) => string | null): string;
722741
/** Update date in YYYY-MM-DD format */

src/core/components.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@ import { i18n } from "@/index";
1313
import { setting } from "@/setting";
1414

1515

16+
interface BlockWithOthers extends Block {
17+
[key: string | number]: string | number;
18+
}
19+
1620
/**
1721
* Renders the value of a block attribute as markdown format
1822
* @param b - Block object
1923
* @param attr - Attribute name
2024
* @returns Rendered attribute value
2125
*/
22-
const renderAttr = (b: Block, attr: keyof Block, options?: {
26+
const renderAttr = (b: BlockWithOthers, attr: keyof BlockWithOthers, options?: {
2327
onlyDate?: boolean;
2428
onlyTime?: boolean;
2529
}): string => {
@@ -88,6 +92,21 @@ const renderAttr = (b: Block, attr: keyof Block, options?: {
8892
v = JSON.stringify(ialObj);
8993
break;
9094

95+
/**
96+
* 兼容 refs 表中的字段
97+
*/
98+
case 'block_id':
99+
case 'def_block_id':
100+
case 'def_block_parent_id':
101+
case 'def_block_root_id':
102+
if (b[attr]) {
103+
//@ts-ignore
104+
v = link(b[attr], b[attr]);
105+
} else {
106+
v = b[attr]
107+
}
108+
break;
109+
91110
default:
92111
v = b[attr];
93112
break;

src/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @Author : frostime
44
* @Date : 2024-05-08 15:00:37
55
* @FilePath : /src/core/index.ts
6-
* @LastEditTime : 2024-12-06 22:19:56
6+
* @LastEditTime : 2024-12-08 21:34:19
77
* @Description :
88
* - Fork from project https://github.com/zxhd863943427/siyuan-plugin-data-query
99
* - 基于该项目的 v0.0.7 版本进行修改

src/core/lute.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ export const initLute = () => {
88
}
99

1010
export const getLute = () => {
11+
if (!lute) {
12+
initLute();
13+
}
1114
return lute;
1215
}

src/core/proxy.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ export interface IWrappedBlock extends Block {
99
unwrapped: Block;
1010

1111
/** Block's URI link in format: siyuan://blocks/xxx */
12-
asuri: string;
12+
asurl: string;
1313
/** Block's URI link in format: siyuan://blocks/xxx */
14-
touri: string;
14+
tourl: string;
1515

16-
/** Block's Markdown format link */
16+
/** Block's Markdown format link [content](siyuan://blocks/xxx) */
1717
aslink: string;
18-
/** Block's Markdown format link */
18+
/** Block's Markdown format link [content](siyuan://blocks/xxx) */
1919
tolink: string;
2020

2121
/** Block's SiYuan reference format text */
@@ -27,6 +27,10 @@ export interface IWrappedBlock extends Block {
2727
* Returns a rendered SiYuan attribute
2828
* @param attr - Attribute name
2929
* @param renderer - Custom render function, uses default rendering when returns null
30+
* @returns {string} Rendered attribute value
31+
* @example
32+
* block.attr('box') // Returns the name of the notebook
33+
* block.attr('root_id') // Returns the block link of the document
3034
*/
3135
attr(attr: keyof Block, renderer?: (block: Block, attr: keyof Block) => string | null): string;
3236

@@ -255,6 +259,15 @@ export const wrapList = (list: Block[], useWrapBlock: boolean = true): IWrappedL
255259
case 'unwrapped':
256260
/** @type {Block[]} 原始数组 */
257261
return target;
262+
case 'asMap':
263+
case 'asmap':
264+
/** @returns {Record<string, Block>} 返回一个以 key 为键值的 Map */
265+
return (key: keyof Block = 'id') => {
266+
return target.reduce((map, block) => {
267+
map[block[key]] = block;
268+
return map;
269+
}, {});
270+
}
258271
case 'pick':
259272
/**
260273
* 返回只包含指定属性的新数组
@@ -313,6 +326,13 @@ export const wrapList = (list: Block[], useWrapBlock: boolean = true): IWrappedL
313326
//@ts-ignore
314327
return wrapList(newTarget);
315328
}
329+
case 'toSorted':
330+
/**
331+
* 返回按指定属性排序的新数组
332+
*/
333+
return (predicate: (a: Block, b: Block) => number) => {
334+
return wrapList(target.toSorted(predicate));
335+
}
316336
case 'sorton':
317337
/**
318338
* 返回按指定属性排序的新数组
@@ -322,7 +342,7 @@ export const wrapList = (list: Block[], useWrapBlock: boolean = true): IWrappedL
322342
* @example list.sorton('updated', 'desc')
323343
*/
324344
return (attr: keyof Block, order: 'asc' | 'desc' = 'asc') => {
325-
let sorted = target.sort((a, b) => {
345+
let sorted = target.toSorted((a, b) => {
326346
if (a[attr] > b[attr]) {
327347
return order === 'asc' ? 1 : -1;
328348
} else if (a[attr] < b[attr]) {
@@ -381,6 +401,10 @@ export const wrapList = (list: Block[], useWrapBlock: boolean = true): IWrappedL
381401
return (start: number, end: number) => {
382402
return wrapList(target.slice(start, end));
383403
}
404+
case 'map':
405+
return (fn: (b: Block, index: number) => any, useWrapBlock: boolean = true) => {
406+
return wrapList(target.map(fn), useWrapBlock);
407+
}
384408
case 'unique':
385409
/**
386410
* 返回一个去重后的新数组

src/core/query.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
/*
2+
* Copyright (c) 2024 by frostime. All Rights Reserved.
3+
* @Author : frostime
4+
* @Date : 2024-12-01 22:34:55
5+
* @FilePath : /src/core/query.ts
6+
* @LastEditTime : 2024-12-09 00:34:25
7+
* @Description :
8+
*/
19
import { IProtyle } from "siyuan";
210

311
import { request, sql, listDocsByPath } from "@/api";
4-
import { initLute } from "./lute";
12+
import { getLute, initLute } from "./lute";
513
import { wrapBlock, wrapList } from "./proxy";
614
import { formatDateTime } from "@/utils/time";
715
import { DataView } from "./data-view";
816
import { getNotebook, openBlock } from "@/utils";
917

1018
import { renderAttr } from "./components";
19+
import { BlockTypeShort } from "@/utils/const";
1120
// import { getSessionStorageSize } from "./gc";
1221

1322

@@ -17,11 +26,10 @@ import { renderAttr } from "./components";
1726
* @param mode uni 模式;
1827
* - 'leaf' 只返回叶子节点
1928
* - 'root' 只返回根节点
20-
* @param ret 返回类型, 'block' 返回 Block[], 'id' 返回 BlockId[]
2129
* @returns BlockId[]
2230
*/
23-
function UniBlocks(blocks: Block[], mode: 'leaf' | 'root' = 'leaf', ret: "block" | "id" = "block") {
24-
console.log('UniBlocks', blocks);
31+
function mergeBlocks(blocks: Block[], mode: 'leaf' | 'root' = 'leaf') {
32+
// console.log('mergeBlocks', blocks);
2533
let p2c = new Map();
2634
let blocksMap = new Map();
2735
blocks.forEach(block => {
@@ -52,7 +60,7 @@ function UniBlocks(blocks: Block[], mode: 'leaf' | 'root' = 'leaf', ret: "block"
5260
}
5361
}
5462
let retBlocks = blockIdsResult.map(id => blocksMap.get(id));
55-
return ret === "block" ? retBlocks : retBlocks.map(block => block.id);
63+
return retBlocks;
5664
}
5765

5866
async function getBlocksByIds(...ids: BlockId[]) {
@@ -146,6 +154,7 @@ const Query = {
146154
initLute();
147155
return new DataView(protyle, item, top);
148156
},
157+
149158
Utils: {
150159
Date: (...args: ConstructorParameters<typeof SiYuanDate>) => new SiYuanDate(...args),
151160
/**
@@ -281,10 +290,21 @@ const Query = {
281290
* Gets the name of a notebook by its ID; equivalent to `notebook(boxid).name`
282291
* @param boxid - Notebook ID
283292
* @returns Notebook name
293+
* @example
294+
* Query.Utils.boxName(block['box']) // 'Notebook 123'
284295
*/
285-
boxname: (boxid: NotebookId) => {
296+
boxName: (boxid: NotebookId) => {
286297
return getNotebook(boxid).name;
287298
},
299+
/**
300+
* Gets the readable name of the type of a block
301+
* @param type - Block type
302+
* @returns Readable block type name
303+
* @example
304+
* Query.Utils.typename(block['type']) // 'Heading'
305+
*/
306+
typeName: (type: BlockType) => BlockTypeShort[type] ?? type,
307+
288308
/**
289309
* Renders the value of a block attribute as markdown format
290310
*/
@@ -295,11 +315,12 @@ const Query = {
295315
/**
296316
* Wraps blocks with additional functionality
297317
* @param blocks - Blocks to wrap
318+
* @param useWrapBlock - Whether to wrap blocks inside the WrappedList
298319
* @returns Wrapped block(s)
299320
*/
300-
wrapBlocks: (blocks: Block[] | Block) => {
321+
wrapBlocks: (blocks: Block[] | Block, useWrapBlock: boolean = true) => {
301322
if (Array.isArray(blocks)) {
302-
return wrapList(blocks);
323+
return wrapList(blocks, useWrapBlock);
303324
}
304325
return wrapBlock(blocks);
305326
},
@@ -422,6 +443,7 @@ const Query = {
422443

423444
/**
424445
* Gets the daily notes document
446+
* @param limit - Maximum number of results
425447
* @param notebook - Notebook ID, if not specified, all daily notes documents will be returned
426448
* @returns Array of daily notes document blocks
427449
*/
@@ -462,6 +484,18 @@ const Query = {
462484
return wrapList(docs);
463485
},
464486

487+
/**
488+
* Return the markdown content of the document of the given block
489+
* @param block - Block
490+
* @returns
491+
*/
492+
docMd: async (block: Block) => {
493+
const { content } = await request('/api/export/exportMdContent', {
494+
id: block.id
495+
});
496+
return content;
497+
},
498+
465499
/**
466500
* Redirects first block IDs to their parent containers
467501
* @param inputs - Array of blocks or block IDs
@@ -565,6 +599,18 @@ const Query = {
565599
},
566600

567601

602+
/**
603+
* Send GPT request, use AI configuration in `siyuan.config.ai.openAI` by default
604+
* @param prompt - Prompt
605+
* @param options - Options
606+
* @param options.timeout - Request timeout
607+
* @param options.url - Custom API URL
608+
* @param options.model - Custom API model
609+
* @param options.apiKey - Custom API key
610+
* @param options.returnRaw - Whether to return raw response (default: false)
611+
* @param options.history - Chat history
612+
* @returns GPT response
613+
*/
568614
gpt: async (prompt: string, options?: {
569615
timeout?: number,
570616
url?: string,

0 commit comments

Comments
 (0)