Skip to content

Commit

Permalink
feat: basic help
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed May 21, 2024
1 parent 42ebc5b commit bbfde34
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 14 deletions.
1 change: 0 additions & 1 deletion src/commandpalette/commands/help/Filter.tid
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
command-palette-type: help
command-palette-prefix: [
caption: <<lingo Help/Filter/Caption $:/plugins/linonetwo/commandpalette/language/>>
description: <<lingo Help/Filter/Description $:/plugins/linonetwo/commandpalette/language/>>
Expand Down
3 changes: 1 addition & 2 deletions src/commandpalette/commands/help/Help.tid
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
command-palette-type: help
command-palette-prefix: ? ?
command-palette-prefix: ?
caption: <<lingo Help/Help/Caption $:/plugins/linonetwo/commandpalette/language/>>
description: <<lingo Help/Help/Description $:/plugins/linonetwo/commandpalette/language/>>
tags: $:/tags/CommandPaletteHelp
Expand Down
1 change: 0 additions & 1 deletion src/commandpalette/commands/help/System.tid
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
command-palette-type: help
command-palette-prefix: $ ¥
caption: <<lingo Help/System/Caption $:/plugins/linonetwo/commandpalette/language/>>
description: <<lingo Help/System/Description $:/plugins/linonetwo/commandpalette/language/>>
Expand Down
1 change: 0 additions & 1 deletion src/commandpalette/commands/help/Tags.tid
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
command-palette-type: help
command-palette-prefix: #
caption: <<lingo Help/Tags/Caption $:/plugins/linonetwo/commandpalette/language/>>
description: <<lingo Help/Tags/Description $:/plugins/linonetwo/commandpalette/language/>>
Expand Down
5 changes: 5 additions & 0 deletions src/commandpalette/commands/help/User.tid
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
command-palette-prefix:
caption: <<lingo Help/User/Caption $:/plugins/linonetwo/commandpalette/language/>>
description: <<lingo Help/User/Description $:/plugins/linonetwo/commandpalette/language/>>
tags: $:/tags/CommandPaletteHelp
title: $:/plugins/linonetwo/commandpalette/commands/help/User
13 changes: 12 additions & 1 deletion src/commandpalette/language/en-GB/Translations.multids
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ Message: Trigger system message
Layout: Switch layout
Config: Open config
Tags: Tags
Help: Command palette usage
ActionString: Trigger action string
CurrentTiddler: Current tiddler
CurrentLayout: Current layout
NoResult: No result or no uniq result
NoResult: No result or no uniq result
Help/Help/Caption: Help
Help/Help/Description: Usage of the command menu, describes what the command menu will search when different prefixes are added to the search box.
Help/Filter/Caption: Filters
Help/Filter/Description: A two-step process, first search for a preset filter or a customized filter, enter to confirm the selection; then continue to search in the filter results, enter to open the tiddler. In the second step, only user entries are searched by default, and system entries can be included by adding a prefix to the system entry search.
Help/User/Caption: User tiddlers
Help/User/Description: Not prefixed, just typed, you can search for the title and content of your tiddlers.
Help/System/Caption: System tiddlers
Help/System/Description: Searches for individual micro-system features such as available system messages, layouts, settings, etc.
Help/Tags/Caption: Tags
Help/Tags/Description: In two steps, first search for all tags in the Knowledge Base, enter to confirm selection; then continue searching in entries with this tag added, enter to open the tiddler. In the second step, only user entries are searched by default, and system entries can be included by adding a prefix to the system entry search.
13 changes: 12 additions & 1 deletion src/commandpalette/language/zh-Hans/Translations.multids
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@ Message: 触发系统消息
Layout: 切换布局
Config: 打开设置
Tags: 标签
Help: 命令菜单用法
ActionString: 触发动作字符串
CurrentTiddler: 当前条目
CurrentLayout: 当前布局
NoResult: 无结果或结果重复
NoResult: 无结果或结果重复
Help/Help/Caption: 帮助
Help/Help/Description: 命令菜单用法,介绍在搜索框里加上不同前缀时,命令菜单将会搜索的内容。
Help/Filter/Caption: 筛选器
Help/Filter/Description: 分为两步,首先搜索预置筛选器或自定义筛选器,回车确认选中;然后在筛选结果中继续搜索,回车打开条目。第二步里,默认只搜索用户条目,可加上系统条目搜索的前缀来包括系统条目。
Help/User/Caption: 用户条目
Help/User/Description: 什么前缀也不加,直接打字,可以搜索你的条目的标题和内容。
Help/System/Caption: 系统条目
Help/System/Description: 带有这个前缀时,可以搜索各太微系统功能,如可用的系统消息、布局、设置等。
Help/Tags/Caption: 标签
Help/Tags/Description: 分为两步,首先搜索知识库中所有的标签,回车确认选中;然后在加了这个标签的条目中继续搜索,回车打开条目。第二步里,默认只搜索用户条目,可加上系统条目搜索的前缀来包括系统条目。
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import type { AutocompletePlugin } from '@algolia/autocomplete-js';
import { ITiddlerFields } from 'tiddlywiki';
import { checkIsSearchSystem } from '../utils/checkPrefix';
import { checkIsSearchSystem, checkIsUnderFilter } from '../utils/checkPrefix';
import { IContext } from '../utils/context';
import { filterTiddlersAsync } from '../utils/filterTiddlersAsync';
import { lingo } from '../utils/lingo';
Expand All @@ -10,7 +10,7 @@ import { renderTextWithCache } from '../utils/renderTextWithCache';
export const plugin = {
getSources(parameters) {
if (parameters.query.length === 0) return [];
if (!checkIsSearchSystem(parameters)) return [];
if (!checkIsSearchSystem(parameters) || checkIsUnderFilter(parameters)) return [];
const { widget } = parameters.state.context as IContext;
return [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ export const plugin = {
});
}
// When filter in context is set by previous step, and no prefix, we search under result of that filter
if (checkIsSearchSystem(parameters) && checkIsUnderFilter(parameters)) {
if (checkIsUnderFilter(parameters)) {
sources.push({
sourceId: 'filter',
async getItems({ query, state }) {
return await filterTiddlersAsync(`${(state.context as IContext).filter} +[search[${query}]]`, true);
const isSystem = checkIsSearchSystem(parameters);
return await filterTiddlersAsync(`[all[tiddlers+shadows]]+${(state.context as IContext).filter} +[search[${isSystem ? query.slice(1) : query}]]`, isSystem);
},
getItemUrl({ item }) {
return item.title;
Expand Down
61 changes: 61 additions & 0 deletions src/commandpalette/widgets/build-in-sub-plugins/search-help.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import type { AutocompletePlugin } from '@algolia/autocomplete-js';
import { ITiddlerFields } from 'tiddlywiki';
import { checkIsHelp, checkIsUnderFilter } from '../utils/checkPrefix';
import { IContext } from '../utils/context';
import { lingo } from '../utils/lingo';
import { renderTextWithCache } from '../utils/renderTextWithCache';

export const plugin = {
getSources(parameters) {
const { widget } = parameters.state.context as IContext;
if (!checkIsHelp(parameters) || checkIsUnderFilter(parameters)) return [];
return [
{
sourceId: 'help',
getItems({ query }) {
return ($tw.wiki.filterTiddlers('[all[shadows]tag[$:/tags/CommandPaletteHelp]]')
.map((title) => $tw.wiki.getTiddler(title)?.fields)
.filter(Boolean) as ITiddlerFields[])
.filter((tiddler) =>
// TODO: add pinyinfuse
$tw.wiki.filterTiddlers(
`[search[${query.slice(1)}]]`,
undefined,
$tw.wiki.makeTiddlerIterator([
tiddler.title.replace('$:/plugins/linonetwo/commandpalette/commands/help/', ''),
renderTextWithCache(tiddler.caption, widget),
renderTextWithCache(tiddler.description, widget),
]),
).length > 0
);
},
getItemUrl({ item }) {
return item.title;
},
onSelect({ item }) {
parameters.setContext({ noNavigate: true, noClose: true, newQuery: (item['command-palette-prefix'] as string).charAt(0) } satisfies IContext);
},
templates: {
header() {
return lingo('Help');
},
item({ item, createElement }) {
const description = item.description
? ` ${renderTextWithCache(item.description as string, widget)}`
: '';
return createElement('div', {
style: 'display:flex;flex-direction:column;',
}, [
createElement('div', { style: 'margin-bottom:0.25em;' }, [
createElement('em', { style: 'margin-right:0.25em;' }, [item['command-palette-prefix'] as string]),
renderTextWithCache(item.caption, widget),
]),
createElement('div', { style: 'margin-bottom:0.3em;' }, description),
]);
},
},
},
];
},
} satisfies AutocompletePlugin<ITiddlerFields, unknown>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
title: $:/plugins/linonetwo/commandpalette/widget/build-in-sub-plugins/search-help.js
tags: $:/tags/CommandPalettePlugin
type: application/javascript
module-type: library
priority: -1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
import type { AutocompletePlugin } from '@algolia/autocomplete-js';
import { ITiddlerFields } from 'tiddlywiki';
import { checkIsSearchSystem } from '../utils/checkPrefix';
import { checkIsSearchSystem, checkIsUnderFilter } from '../utils/checkPrefix';
import { IContext } from '../utils/context';
import { filterTiddlersAsync } from '../utils/filterTiddlersAsync';
import { getIconSvg } from '../utils/getIconSvg';
Expand All @@ -11,7 +11,7 @@ import { renderTextWithCache } from '../utils/renderTextWithCache';
export const plugin = {
getSources(parameters) {
if (parameters.query.length === 0) return [];
if (!checkIsSearchSystem(parameters)) return [];
if (!checkIsSearchSystem(parameters) || checkIsUnderFilter(parameters)) return [];
const { widget } = parameters.state.context as IContext;
return [
{
Expand Down
7 changes: 6 additions & 1 deletion src/commandpalette/widgets/utils/checkPrefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ const systemPrefixes = ($tw.wiki.getTiddler('$:/plugins/linonetwo/commandpalette
?.split(' ')?.filter(Boolean);
const filterPrefix = $tw.wiki.getTiddler('$:/plugins/linonetwo/commandpalette/commands/help/Filter')?.fields?.['command-palette-prefix'] as string | undefined;
const tagsPrefix = $tw.wiki.getTiddler('$:/plugins/linonetwo/commandpalette/commands/help/Tags')?.fields?.['command-palette-prefix'] as string | undefined;
const helpPrefix = $tw.wiki.getTiddler('$:/plugins/linonetwo/commandpalette/commands/help/Help')?.fields?.['command-palette-prefix'] as string | undefined;
export function checkIsSearchSystem(parameters: GetSourcesParams<ITiddlerFields>) {
return Boolean(systemPrefixes?.includes(parameters.query[0]));
}

export function checkIsSearchUser(parameters: GetSourcesParams<ITiddlerFields>) {
const firstChar = parameters.query[0];
return !(systemPrefixes?.includes(firstChar)) && !(firstChar === filterPrefix) && !(firstChar === tagsPrefix);
return !(systemPrefixes?.includes(firstChar)) && !([filterPrefix, tagsPrefix, helpPrefix].includes(firstChar));
}

export function checkIsHelp(parameters: GetSourcesParams<ITiddlerFields>) {
return parameters.query[0] === helpPrefix || parameters.query === '';
}

export function checkIsFilter(parameters: GetSourcesParams<ITiddlerFields>) {
Expand Down
1 change: 1 addition & 0 deletions src/commandpalette/widgets/utils/filterTiddlersAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function filterTiddlersAsync(filter: string, system?: boolean, excl
}
return resultFromIPC.data as ITiddlerFields[];
} else {
// FIXME: this prevent [all[tiddlers+shadows]]+[fields[]]+[search[]] to work
return $tw.wiki.filterTiddlers(filter)
.map((title) => $tw.wiki.getTiddler(title)?.fields)
.filter(Boolean) as ITiddlerFields[];
Expand Down

0 comments on commit bbfde34

Please sign in to comment.