@dxos/plugin-search 0.6.8-main.046e6cf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +8 -0
- package/README.md +4 -0
- package/dist/lib/browser/chunk-56ITWEGS.mjs +23 -0
- package/dist/lib/browser/chunk-56ITWEGS.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +616 -0
- package/dist/lib/browser/index.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -0
- package/dist/lib/browser/meta.mjs +11 -0
- package/dist/lib/browser/meta.mjs.map +7 -0
- package/dist/types/src/SearchPlugin.d.ts +4 -0
- package/dist/types/src/SearchPlugin.d.ts.map +1 -0
- package/dist/types/src/components/Search.stories.d.ts +25 -0
- package/dist/types/src/components/Search.stories.d.ts.map +1 -0
- package/dist/types/src/components/SearchDialog.d.ts +10 -0
- package/dist/types/src/components/SearchDialog.d.ts.map +1 -0
- package/dist/types/src/components/SearchMain.d.ts +6 -0
- package/dist/types/src/components/SearchMain.d.ts.map +1 -0
- package/dist/types/src/components/SearchResults/SearchResults.d.ts +19 -0
- package/dist/types/src/components/SearchResults/SearchResults.d.ts.map +1 -0
- package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts +20 -0
- package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts.map +1 -0
- package/dist/types/src/components/SearchResults/index.d.ts +2 -0
- package/dist/types/src/components/SearchResults/index.d.ts.map +1 -0
- package/dist/types/src/components/Searchbar/Searchbar.d.ts +13 -0
- package/dist/types/src/components/Searchbar/Searchbar.d.ts.map +1 -0
- package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts +18 -0
- package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts.map +1 -0
- package/dist/types/src/components/Searchbar/index.d.ts +2 -0
- package/dist/types/src/components/Searchbar/index.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +4 -0
- package/dist/types/src/components/index.d.ts.map +1 -0
- package/dist/types/src/context.d.ts +12 -0
- package/dist/types/src/context.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +6 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/meta.d.ts +16 -0
- package/dist/types/src/meta.d.ts.map +1 -0
- package/dist/types/src/search-sync.d.ts +13 -0
- package/dist/types/src/search-sync.d.ts.map +1 -0
- package/dist/types/src/search.d.ts +3 -0
- package/dist/types/src/search.d.ts.map +1 -0
- package/dist/types/src/search.test.d.ts +2 -0
- package/dist/types/src/search.test.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +15 -0
- package/dist/types/src/translations.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +6 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/package.json +66 -0
- package/src/SearchPlugin.tsx +122 -0
- package/src/components/Search.stories.tsx +63 -0
- package/src/components/SearchDialog.tsx +141 -0
- package/src/components/SearchMain.tsx +48 -0
- package/src/components/SearchResults/SearchResults.stories.tsx +57 -0
- package/src/components/SearchResults/SearchResults.tsx +124 -0
- package/src/components/SearchResults/index.ts +5 -0
- package/src/components/Searchbar/Searchbar.stories.tsx +34 -0
- package/src/components/Searchbar/Searchbar.tsx +57 -0
- package/src/components/Searchbar/index.ts +5 -0
- package/src/components/index.ts +7 -0
- package/src/context.tsx +41 -0
- package/src/index.ts +12 -0
- package/src/meta.tsx +20 -0
- package/src/search-sync.ts +130 -0
- package/src/search.test.ts +36 -0
- package/src/search.ts +61 -0
- package/src/translations.ts +21 -0
- package/src/types.tsx +25 -0
package/src/context.tsx
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import React, { createContext, type PropsWithChildren, useContext, useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { raise } from '@dxos/debug';
|
|
8
|
+
|
|
9
|
+
import { filterObjectsSync, queryStringToMatch, type SearchResult } from './search-sync';
|
|
10
|
+
|
|
11
|
+
type SearchContextType = {
|
|
12
|
+
match?: RegExp;
|
|
13
|
+
setMatch?: (text?: string) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const SearchContext = createContext<SearchContextType>({});
|
|
17
|
+
|
|
18
|
+
export const SearchContextProvider = ({ children }: PropsWithChildren) => {
|
|
19
|
+
const [match, setMatch] = useState<RegExp>();
|
|
20
|
+
const handleMatch = (text?: string) => setMatch(queryStringToMatch(text));
|
|
21
|
+
return <SearchContext.Provider value={{ match, setMatch: handleMatch }}>{children}</SearchContext.Provider>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const useGlobalSearch = () => {
|
|
25
|
+
return useContext(SearchContext) ?? raise(new Error('Missing SearchContext.'));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const useGlobalSearchResults = <T extends Record<string, any>>(objects?: T[]): SearchResult[] => {
|
|
29
|
+
const { match } = useGlobalSearch();
|
|
30
|
+
return objects && match ? filterObjectsSync(objects, match) : [];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const useGlobalFilteredObjects = <T extends Record<string, any>>(objects?: T[]): T[] => {
|
|
34
|
+
const { match } = useGlobalSearch();
|
|
35
|
+
if (!match || !objects) {
|
|
36
|
+
return objects ?? [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const matching = filterObjectsSync(objects, match);
|
|
40
|
+
return matching.map((result) => result.object);
|
|
41
|
+
};
|
package/src/index.ts
ADDED
package/src/meta.tsx
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { MagnifyingGlass, type IconProps } from '@phosphor-icons/react';
|
|
6
|
+
import React from 'react';
|
|
7
|
+
|
|
8
|
+
import { pluginMeta } from '@dxos/app-framework';
|
|
9
|
+
|
|
10
|
+
export const SEARCH_PLUGIN = 'dxos.org/plugin/search';
|
|
11
|
+
export const SEARCH_RESULT = `${SEARCH_PLUGIN}/result`;
|
|
12
|
+
|
|
13
|
+
export default pluginMeta({
|
|
14
|
+
id: SEARCH_PLUGIN,
|
|
15
|
+
name: 'Search',
|
|
16
|
+
description: 'Search ECHO spaces for content.',
|
|
17
|
+
tags: ['experimental'],
|
|
18
|
+
iconComponent: (props: IconProps) => <MagnifyingGlass {...props} />,
|
|
19
|
+
iconSymbol: 'ph--magnifying-glass--regular',
|
|
20
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { getSchema, type S } from '@dxos/echo-schema';
|
|
6
|
+
import { AST } from '@dxos/echo-schema';
|
|
7
|
+
import { TextType } from '@dxos/plugin-markdown/types';
|
|
8
|
+
|
|
9
|
+
export const queryStringToMatch = (queryString?: string): RegExp | undefined => {
|
|
10
|
+
const trimmed = queryString?.trim();
|
|
11
|
+
return trimmed ? new RegExp(trimmed, 'i') : undefined;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// TODO(burdon): Type name registry linked to schema?
|
|
15
|
+
const getIcon = (schema: S.Schema<any> | undefined): string | undefined => {
|
|
16
|
+
if (!(schema && AST.isTypeLiteral(schema.ast))) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const keys = schema.ast.propertySignatures.map((p) => p.name);
|
|
20
|
+
if (keys.indexOf('email') !== -1) {
|
|
21
|
+
return 'user';
|
|
22
|
+
}
|
|
23
|
+
if (keys.indexOf('website') !== -1) {
|
|
24
|
+
return 'organization';
|
|
25
|
+
}
|
|
26
|
+
if (keys.indexOf('repo') !== -1) {
|
|
27
|
+
return 'project';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return undefined;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Plain text fields.
|
|
34
|
+
// TODO(burdon): Reconcile with agent index.
|
|
35
|
+
export type TextFields = Record<string, string>;
|
|
36
|
+
|
|
37
|
+
export type SearchResult = {
|
|
38
|
+
id: string;
|
|
39
|
+
type?: string;
|
|
40
|
+
label?: string;
|
|
41
|
+
match?: RegExp;
|
|
42
|
+
snippet?: string;
|
|
43
|
+
object?: any;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// TODO(thure): This returns `SearchResult[]` which is not just a narrower set of objects as the name implies.
|
|
47
|
+
export const filterObjectsSync = <T extends Record<string, any>>(objects: T[], match?: RegExp): SearchResult[] => {
|
|
48
|
+
if (!match) {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return objects.reduce<SearchResult[]>((results, object) => {
|
|
53
|
+
// TODO(burdon): Hack to ignore Text objects.
|
|
54
|
+
if (object instanceof TextType) {
|
|
55
|
+
return results;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const fields = mapObjectToTextFields(object);
|
|
59
|
+
Object.entries(fields).some(([, value]) => {
|
|
60
|
+
const result = value.match(match);
|
|
61
|
+
if (result) {
|
|
62
|
+
// TODO(burdon): Use schema.
|
|
63
|
+
const label = getStringProperty(object, ['label', 'name', 'title']);
|
|
64
|
+
|
|
65
|
+
results.push({
|
|
66
|
+
id: object.id,
|
|
67
|
+
type: getIcon(getSchema(object)),
|
|
68
|
+
label,
|
|
69
|
+
match,
|
|
70
|
+
// TODO(burdon): Truncate.
|
|
71
|
+
// TODO(burdon): Issue with sketch documents.
|
|
72
|
+
snippet: value !== label ? value : fields.content ?? fields.description ?? undefined,
|
|
73
|
+
object,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return false;
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return results;
|
|
83
|
+
}, []);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// TODO(burdon): Use schema?
|
|
87
|
+
const getStringProperty = (object: Record<string, unknown>, keys: string[]): string | undefined => {
|
|
88
|
+
let label;
|
|
89
|
+
keys.some((key) => {
|
|
90
|
+
const value = object[key];
|
|
91
|
+
if (typeof value === 'string') {
|
|
92
|
+
label = value;
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return false;
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
return label;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// TODO(burdon): Filter system fields.
|
|
103
|
+
const getKeys = (object: Record<string, unknown>): string[] => {
|
|
104
|
+
try {
|
|
105
|
+
const obj = JSON.parse(JSON.stringify(object));
|
|
106
|
+
return Object.keys(obj).filter(
|
|
107
|
+
(key) => key !== 'id' && key !== 'identityKey' && key[0] !== '_' && key[0] !== '@',
|
|
108
|
+
) as string[];
|
|
109
|
+
} catch (err) {
|
|
110
|
+
// TODO(burdon): Error with TLDraw sketch.
|
|
111
|
+
// Uncaught Error: Type with the name content has already been defined with a different constructor
|
|
112
|
+
return [];
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// TODO(burdon): Use ECHO schema.
|
|
117
|
+
export const mapObjectToTextFields = <T extends Record<string, unknown>>(object: T): TextFields => {
|
|
118
|
+
return getKeys(object).reduce<TextFields>((fields, key) => {
|
|
119
|
+
const value = object[key] as any;
|
|
120
|
+
if (typeof value === 'string' || value instanceof TextType) {
|
|
121
|
+
try {
|
|
122
|
+
fields[key] = String(value);
|
|
123
|
+
} catch (err) {
|
|
124
|
+
// TODO(burdon): Exception when parsing sketch document.
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return fields;
|
|
129
|
+
}, {});
|
|
130
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2022 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { expect } from 'chai';
|
|
6
|
+
|
|
7
|
+
import { Client } from '@dxos/client';
|
|
8
|
+
import { create, Expando } from '@dxos/echo-schema';
|
|
9
|
+
import { faker } from '@dxos/random';
|
|
10
|
+
import { describe, test } from '@dxos/test';
|
|
11
|
+
|
|
12
|
+
import { filterObjectsSync } from './search-sync';
|
|
13
|
+
|
|
14
|
+
faker.seed(1);
|
|
15
|
+
|
|
16
|
+
// TODO(burdon): Reconcile with agent/minisearch.
|
|
17
|
+
|
|
18
|
+
describe('Search', () => {
|
|
19
|
+
test('Prefix text search', async () => {
|
|
20
|
+
const client = new Client();
|
|
21
|
+
await client.initialize();
|
|
22
|
+
await client.halo.createIdentity();
|
|
23
|
+
|
|
24
|
+
const match = 'xxx';
|
|
25
|
+
const space = await client.spaces.create();
|
|
26
|
+
Array.from({ length: 20 }).map((_, i) => {
|
|
27
|
+
const content =
|
|
28
|
+
i === 10 ? faker.lorem.sentence() + ` ${match}}. ` + faker.lorem.sentence() : faker.lorem.sentences();
|
|
29
|
+
return space.db.add(create(Expando, { title: faker.lorem.sentence(), content }));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const { objects } = await space.db.query().run();
|
|
33
|
+
const results = filterObjectsSync(objects, new RegExp(match, 'i'));
|
|
34
|
+
expect(results).to.have.length(1);
|
|
35
|
+
});
|
|
36
|
+
});
|
package/src/search.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { yieldOrContinue } from 'main-thread-scheduling';
|
|
6
|
+
import { useEffect, useState } from 'react';
|
|
7
|
+
|
|
8
|
+
import { TextType } from '@dxos/plugin-markdown/types';
|
|
9
|
+
|
|
10
|
+
// TODO(thure): Deprecate search-sync, move still-relevant utilities elsewhere (here, probably).
|
|
11
|
+
import { mapObjectToTextFields, queryStringToMatch } from './search-sync';
|
|
12
|
+
|
|
13
|
+
export const filterObjects = async <T extends Record<string, any>>(
|
|
14
|
+
objects: T[],
|
|
15
|
+
match?: RegExp,
|
|
16
|
+
): Promise<Map<T, string[][]>> => {
|
|
17
|
+
const result = new Map<T, string[][]>();
|
|
18
|
+
if (!match) {
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
await Promise.all(
|
|
22
|
+
objects
|
|
23
|
+
.filter((object) => !(object instanceof TextType))
|
|
24
|
+
.map(async (object) => {
|
|
25
|
+
await yieldOrContinue('interactive');
|
|
26
|
+
const fields = mapObjectToTextFields<T>(object);
|
|
27
|
+
Object.entries(fields)
|
|
28
|
+
.filter(([_, value]) => value.match(match))
|
|
29
|
+
.forEach(([key, value]) => {
|
|
30
|
+
if (!result.has(object)) {
|
|
31
|
+
result.set(object, []);
|
|
32
|
+
}
|
|
33
|
+
result.set(object, [...result.get(object)!, [key, value]]);
|
|
34
|
+
});
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const useSearchResults = <T extends Record<string, any>>(
|
|
41
|
+
queryString?: string,
|
|
42
|
+
objects?: T[],
|
|
43
|
+
delay: number = 400,
|
|
44
|
+
): [boolean, Map<T, string[][]>] => {
|
|
45
|
+
const [results, setResults] = useState<Map<T, string[][]>>(new Map());
|
|
46
|
+
const [pending, setPending] = useState(false);
|
|
47
|
+
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
setPending(!!(objects && queryString));
|
|
50
|
+
const timeoutId = setTimeout(async () => {
|
|
51
|
+
const nextResults =
|
|
52
|
+
objects && queryString ? await filterObjects(objects, queryStringToMatch(queryString)) : new Map();
|
|
53
|
+
setResults(nextResults);
|
|
54
|
+
setPending(false);
|
|
55
|
+
}, delay);
|
|
56
|
+
|
|
57
|
+
return () => clearTimeout(timeoutId);
|
|
58
|
+
}, [queryString, objects]);
|
|
59
|
+
|
|
60
|
+
return [pending, results];
|
|
61
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { SEARCH_PLUGIN } from './meta';
|
|
6
|
+
|
|
7
|
+
export default [
|
|
8
|
+
{
|
|
9
|
+
'en-US': {
|
|
10
|
+
[SEARCH_PLUGIN]: {
|
|
11
|
+
'plugin name': 'Search',
|
|
12
|
+
'search action label': 'Search spaces',
|
|
13
|
+
'search placeholder': 'Search…',
|
|
14
|
+
'search dialog title': 'Open',
|
|
15
|
+
'recently closed heading': 'Recently closed',
|
|
16
|
+
'pending results message': 'Searching…',
|
|
17
|
+
'empty results message': 'No matching items in your spaces',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
];
|
package/src/types.tsx
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2023 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type {
|
|
6
|
+
GraphBuilderProvides,
|
|
7
|
+
IntentResolverProvides,
|
|
8
|
+
MetadataRecordsProvides,
|
|
9
|
+
SurfaceProvides,
|
|
10
|
+
TranslationsProvides,
|
|
11
|
+
} from '@dxos/app-framework';
|
|
12
|
+
|
|
13
|
+
import { SEARCH_PLUGIN } from './meta';
|
|
14
|
+
|
|
15
|
+
const SEARCH_ACTION = `${SEARCH_PLUGIN}/action`;
|
|
16
|
+
|
|
17
|
+
export enum SearchAction {
|
|
18
|
+
SEARCH = `${SEARCH_ACTION}/search`,
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type SearchPluginProvides = SurfaceProvides &
|
|
22
|
+
IntentResolverProvides &
|
|
23
|
+
GraphBuilderProvides &
|
|
24
|
+
MetadataRecordsProvides &
|
|
25
|
+
TranslationsProvides;
|