@wordpress/core-data 7.0.1 → 7.1.0

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.
@@ -5,63 +5,15 @@ import apiFetch from '@wordpress/api-fetch';
5
5
  import { addQueryArgs } from '@wordpress/url';
6
6
  import { decodeEntities } from '@wordpress/html-entities';
7
7
  import { __ } from '@wordpress/i18n';
8
-
9
8
  /**
10
- * Filters the search by type
9
+ * Fetches link suggestions from the WordPress API.
11
10
  *
12
- * @typedef { 'attachment' | 'post' | 'term' | 'post-format' } WPLinkSearchType
13
- */
14
-
15
- /**
16
- * A link with an id may be of kind post-type or taxonomy
11
+ * WordPress does not support searching multiple tables at once, e.g. posts and terms, so we
12
+ * perform multiple queries at the same time and then merge the results together.
17
13
  *
18
- * @typedef { 'post-type' | 'taxonomy' } WPKind
19
- */
20
-
21
- /**
22
- * @typedef WPLinkSearchOptions
23
- *
24
- * @property {boolean} [isInitialSuggestions] Displays initial search suggestions, when true.
25
- * @property {WPLinkSearchType} [type] Filters by search type.
26
- * @property {string} [subtype] Slug of the post-type or taxonomy.
27
- * @property {number} [page] Which page of results to return.
28
- * @property {number} [perPage] Search results per page.
29
- */
30
-
31
- /**
32
- * @typedef WPLinkSearchResult
33
- *
34
- * @property {number} id Post or term id.
35
- * @property {string} url Link url.
36
- * @property {string} title Title of the link.
37
- * @property {string} type The taxonomy or post type slug or type URL.
38
- * @property {WPKind} [kind] Link kind of post-type or taxonomy
39
- */
40
-
41
- /**
42
- * @typedef WPLinkSearchResultAugments
43
- *
44
- * @property {{kind: WPKind}} [meta] Contains kind information.
45
- * @property {WPKind} [subtype] Optional subtype if it exists.
46
- */
47
-
48
- /**
49
- * @typedef {WPLinkSearchResult & WPLinkSearchResultAugments} WPLinkSearchResultAugmented
50
- */
51
-
52
- /**
53
- * @typedef WPEditorSettings
54
- *
55
- * @property {boolean} [ disablePostFormats ] Disables post formats, when true.
56
- */
57
-
58
- /**
59
- * Fetches link suggestions from the API.
60
- *
61
- * @async
62
- * @param {string} search
63
- * @param {WPLinkSearchOptions} [searchOptions]
64
- * @param {WPEditorSettings} [settings]
14
+ * @param search
15
+ * @param searchOptions
16
+ * @param editorSettings
65
17
  *
66
18
  * @example
67
19
  * ```js
@@ -76,31 +28,22 @@ import { __ } from '@wordpress/i18n';
76
28
  * searchOptions
77
29
  * ) => fetchLinkSuggestions( search, searchOptions, settings );
78
30
  * ```
79
- * @return {Promise< WPLinkSearchResult[] >} List of search suggestions
80
31
  */
81
- const fetchLinkSuggestions = async (search, searchOptions = {}, settings = {}) => {
32
+ export default async function fetchLinkSuggestions(search, searchOptions = {}, editorSettings = {}) {
33
+ const searchOptionsToUse = searchOptions.isInitialSuggestions && searchOptions.initialSuggestionsSearchOptions ? {
34
+ ...searchOptions,
35
+ ...searchOptions.initialSuggestionsSearchOptions
36
+ } : searchOptions;
82
37
  const {
83
- isInitialSuggestions = false,
84
- initialSuggestionsSearchOptions = undefined
85
- } = searchOptions;
38
+ type,
39
+ subtype,
40
+ page,
41
+ perPage = searchOptions.isInitialSuggestions ? 3 : 20
42
+ } = searchOptionsToUse;
86
43
  const {
87
44
  disablePostFormats = false
88
- } = settings;
89
- let {
90
- type = undefined,
91
- subtype = undefined,
92
- page = undefined,
93
- perPage = isInitialSuggestions ? 3 : 20
94
- } = searchOptions;
95
-
96
- /** @type {Promise<WPLinkSearchResult>[]} */
45
+ } = editorSettings;
97
46
  const queries = [];
98
- if (isInitialSuggestions && initialSuggestionsSearchOptions) {
99
- type = initialSuggestionsSearchOptions.type || type;
100
- subtype = initialSuggestionsSearchOptions.subtype || subtype;
101
- page = initialSuggestionsSearchOptions.page || page;
102
- perPage = initialSuggestionsSearchOptions.perPage || perPage;
103
- }
104
47
  if (!type || type === 'post') {
105
48
  queries.push(apiFetch({
106
49
  path: addQueryArgs('/wp/v2/search', {
@@ -113,11 +56,11 @@ const fetchLinkSuggestions = async (search, searchOptions = {}, settings = {}) =
113
56
  }).then(results => {
114
57
  return results.map(result => {
115
58
  return {
116
- ...result,
117
- meta: {
118
- kind: 'post-type',
119
- subtype
120
- }
59
+ id: result.id,
60
+ url: result.url,
61
+ title: decodeEntities(result.title || '') || __('(no title)'),
62
+ type: result.subtype || result.type,
63
+ kind: 'post-type'
121
64
  };
122
65
  });
123
66
  }).catch(() => []) // Fail by returning no results.
@@ -135,11 +78,11 @@ const fetchLinkSuggestions = async (search, searchOptions = {}, settings = {}) =
135
78
  }).then(results => {
136
79
  return results.map(result => {
137
80
  return {
138
- ...result,
139
- meta: {
140
- kind: 'taxonomy',
141
- subtype
142
- }
81
+ id: result.id,
82
+ url: result.url,
83
+ title: decodeEntities(result.title || '') || __('(no title)'),
84
+ type: result.subtype || result.type,
85
+ kind: 'taxonomy'
143
86
  };
144
87
  });
145
88
  }).catch(() => []) // Fail by returning no results.
@@ -157,11 +100,11 @@ const fetchLinkSuggestions = async (search, searchOptions = {}, settings = {}) =
157
100
  }).then(results => {
158
101
  return results.map(result => {
159
102
  return {
160
- ...result,
161
- meta: {
162
- kind: 'taxonomy',
163
- subtype
164
- }
103
+ id: result.id,
104
+ url: result.url,
105
+ title: decodeEntities(result.title || '') || __('(no title)'),
106
+ type: result.subtype || result.type,
107
+ kind: 'taxonomy'
165
108
  };
166
109
  });
167
110
  }).catch(() => []) // Fail by returning no results.
@@ -177,38 +120,63 @@ const fetchLinkSuggestions = async (search, searchOptions = {}, settings = {}) =
177
120
  }).then(results => {
178
121
  return results.map(result => {
179
122
  return {
180
- ...result,
181
- meta: {
182
- kind: 'media'
183
- }
123
+ id: result.id,
124
+ url: result.source_url,
125
+ title: decodeEntities(result.title.rendered || '') || __('(no title)'),
126
+ type: result.type,
127
+ kind: 'media'
184
128
  };
185
129
  });
186
130
  }).catch(() => []) // Fail by returning no results.
187
131
  );
188
132
  }
189
- return Promise.all(queries).then(results => {
190
- return results.reduce(( /** @type {WPLinkSearchResult[]} */accumulator, current) => accumulator.concat(current),
191
- // Flatten list.
192
- []).filter(
193
- /**
194
- * @param {{ id: number }} result
195
- */
196
- result => {
197
- return !!result.id;
198
- }).slice(0, perPage).map(( /** @type {WPLinkSearchResultAugmented} */result) => {
199
- const isMedia = result.type === 'attachment';
200
- return {
201
- id: result.id,
202
- // @ts-ignore fix when we make this a TS file
203
- url: isMedia ? result.source_url : result.url,
204
- title: decodeEntities(isMedia ?
205
- // @ts-ignore fix when we make this a TS file
206
- result.title.rendered : result.title || '') || __('(no title)'),
207
- type: result.subtype || result.type,
208
- kind: result?.meta?.kind
209
- };
210
- });
211
- });
212
- };
213
- export default fetchLinkSuggestions;
133
+ const responses = await Promise.all(queries);
134
+ let results = responses.flat();
135
+ results = results.filter(result => !!result.id);
136
+ results = sortResults(results, search);
137
+ results = results.slice(0, perPage);
138
+ return results;
139
+ }
140
+
141
+ /**
142
+ * Sort search results by relevance to the given query.
143
+ *
144
+ * Sorting is necessary as we're querying multiple endpoints and merging the results. For example
145
+ * a taxonomy title might be more relevant than a post title, but by default taxonomy results will
146
+ * be ordered after all the (potentially irrelevant) post results.
147
+ *
148
+ * We sort by scoring each result, where the score is the number of tokens in the title that are
149
+ * also in the search query, divided by the total number of tokens in the title. This gives us a
150
+ * score between 0 and 1, where 1 is a perfect match.
151
+ *
152
+ * @param results
153
+ * @param search
154
+ */
155
+ export function sortResults(results, search) {
156
+ const searchTokens = new Set(tokenize(search));
157
+ const scores = {};
158
+ for (const result of results) {
159
+ if (result.title) {
160
+ const titleTokens = tokenize(result.title);
161
+ const matchingTokens = titleTokens.filter(token => searchTokens.has(token));
162
+ scores[result.id] = matchingTokens.length / titleTokens.length;
163
+ } else {
164
+ scores[result.id] = 0;
165
+ }
166
+ }
167
+ return results.sort((a, b) => scores[b.id] - scores[a.id]);
168
+ }
169
+
170
+ /**
171
+ * Turns text into an array of tokens, with whitespace and punctuation removed.
172
+ *
173
+ * For example, `"I'm having a ball."` becomes `[ "im", "having", "a", "ball" ]`.
174
+ *
175
+ * @param text
176
+ */
177
+ export function tokenize(text) {
178
+ // \p{L} matches any kind of letter from any language.
179
+ // \p{N} matches any kind of numeric character.
180
+ return text.toLowerCase().match(/[\p{L}\p{N}]+/gu) || [];
181
+ }
214
182
  //# sourceMappingURL=__experimental-fetch-link-suggestions.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["apiFetch","addQueryArgs","decodeEntities","__","fetchLinkSuggestions","search","searchOptions","settings","isInitialSuggestions","initialSuggestionsSearchOptions","undefined","disablePostFormats","type","subtype","page","perPage","queries","push","path","per_page","then","results","map","result","meta","kind","catch","Promise","all","reduce","accumulator","current","concat","filter","id","slice","isMedia","url","source_url","title","rendered"],"sources":["@wordpress/core-data/src/fetch/__experimental-fetch-link-suggestions.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { addQueryArgs } from '@wordpress/url';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Filters the search by type\n *\n * @typedef { 'attachment' | 'post' | 'term' | 'post-format' } WPLinkSearchType\n */\n\n/**\n * A link with an id may be of kind post-type or taxonomy\n *\n * @typedef { 'post-type' | 'taxonomy' } WPKind\n */\n\n/**\n * @typedef WPLinkSearchOptions\n *\n * @property {boolean} [isInitialSuggestions] Displays initial search suggestions, when true.\n * @property {WPLinkSearchType} [type] Filters by search type.\n * @property {string} [subtype] Slug of the post-type or taxonomy.\n * @property {number} [page] Which page of results to return.\n * @property {number} [perPage] Search results per page.\n */\n\n/**\n * @typedef WPLinkSearchResult\n *\n * @property {number} id Post or term id.\n * @property {string} url Link url.\n * @property {string} title Title of the link.\n * @property {string} type The taxonomy or post type slug or type URL.\n * @property {WPKind} [kind] Link kind of post-type or taxonomy\n */\n\n/**\n * @typedef WPLinkSearchResultAugments\n *\n * @property {{kind: WPKind}} [meta] Contains kind information.\n * @property {WPKind} [subtype] Optional subtype if it exists.\n */\n\n/**\n * @typedef {WPLinkSearchResult & WPLinkSearchResultAugments} WPLinkSearchResultAugmented\n */\n\n/**\n * @typedef WPEditorSettings\n *\n * @property {boolean} [ disablePostFormats ] Disables post formats, when true.\n */\n\n/**\n * Fetches link suggestions from the API.\n *\n * @async\n * @param {string} search\n * @param {WPLinkSearchOptions} [searchOptions]\n * @param {WPEditorSettings} [settings]\n *\n * @example\n * ```js\n * import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data';\n *\n * //...\n *\n * export function initialize( id, settings ) {\n *\n * settings.__experimentalFetchLinkSuggestions = (\n * search,\n * searchOptions\n * ) => fetchLinkSuggestions( search, searchOptions, settings );\n * ```\n * @return {Promise< WPLinkSearchResult[] >} List of search suggestions\n */\nconst fetchLinkSuggestions = async (\n\tsearch,\n\tsearchOptions = {},\n\tsettings = {}\n) => {\n\tconst {\n\t\tisInitialSuggestions = false,\n\t\tinitialSuggestionsSearchOptions = undefined,\n\t} = searchOptions;\n\n\tconst { disablePostFormats = false } = settings;\n\n\tlet {\n\t\ttype = undefined,\n\t\tsubtype = undefined,\n\t\tpage = undefined,\n\t\tperPage = isInitialSuggestions ? 3 : 20,\n\t} = searchOptions;\n\n\t/** @type {Promise<WPLinkSearchResult>[]} */\n\tconst queries = [];\n\n\tif ( isInitialSuggestions && initialSuggestionsSearchOptions ) {\n\t\ttype = initialSuggestionsSearchOptions.type || type;\n\t\tsubtype = initialSuggestionsSearchOptions.subtype || subtype;\n\t\tpage = initialSuggestionsSearchOptions.page || page;\n\t\tperPage = initialSuggestionsSearchOptions.perPage || perPage;\n\t}\n\n\tif ( ! type || type === 'post' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'post-type', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'term' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'term',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'taxonomy', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post-format',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'taxonomy', subtype },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'attachment' ) {\n\t\tqueries.push(\n\t\t\tapiFetch( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/media', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...result,\n\t\t\t\t\t\t\tmeta: { kind: 'media' },\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\treturn Promise.all( queries ).then( ( results ) => {\n\t\treturn results\n\t\t\t.reduce(\n\t\t\t\t( /** @type {WPLinkSearchResult[]} */ accumulator, current ) =>\n\t\t\t\t\taccumulator.concat( current ), // Flatten list.\n\t\t\t\t[]\n\t\t\t)\n\t\t\t.filter(\n\t\t\t\t/**\n\t\t\t\t * @param {{ id: number }} result\n\t\t\t\t */\n\t\t\t\t( result ) => {\n\t\t\t\t\treturn !! result.id;\n\t\t\t\t}\n\t\t\t)\n\t\t\t.slice( 0, perPage )\n\t\t\t.map( ( /** @type {WPLinkSearchResultAugmented} */ result ) => {\n\t\t\t\tconst isMedia = result.type === 'attachment';\n\n\t\t\t\treturn {\n\t\t\t\t\tid: result.id,\n\t\t\t\t\t// @ts-ignore fix when we make this a TS file\n\t\t\t\t\turl: isMedia ? result.source_url : result.url,\n\t\t\t\t\ttitle:\n\t\t\t\t\t\tdecodeEntities(\n\t\t\t\t\t\t\tisMedia\n\t\t\t\t\t\t\t\t? // @ts-ignore fix when we make this a TS file\n\t\t\t\t\t\t\t\t result.title.rendered\n\t\t\t\t\t\t\t\t: result.title || ''\n\t\t\t\t\t\t) || __( '(no title)' ),\n\t\t\t\t\ttype: result.subtype || result.type,\n\t\t\t\t\tkind: result?.meta?.kind,\n\t\t\t\t};\n\t\t\t} );\n\t} );\n};\n\nexport default fetchLinkSuggestions;\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,QAAQ,MAAM,sBAAsB;AAC3C,SAASC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C,MAAM,KAAM;MAC9D,MAAMa,OAAO,GAAGb,MAAM,CAACX,IAAI,KAAK,YAAY;MAE5C,OAAO;QACNsB,EAAE,EAAEX,MAAM,CAACW,EAAE;QACb;QACAG,GAAG,EAAED,OAAO,GAAGb,MAAM,CAACe,UAAU,GAAGf,MAAM,CAACc,GAAG;QAC7CE,KAAK,EACJrC,cAAc,CACbkC,OAAO;QACJ;QACAb,MAAM,CAACgB,KAAK,CAACC,QAAQ,GACrBjB,MAAM,CAACgB,KAAK,IAAI,EACpB,CAAC,IAAIpC,EAAE,CAAE,YAAa,CAAC;QACxBS,IAAI,EAAEW,MAAM,CAACV,OAAO,IAAIU,MAAM,CAACX,IAAI;QACnCa,IAAI,EAAEF,MAAM,EAAEC,IAAI,EAAEC;MACrB,CAAC;IACF,CAAE,CAAC;EACL,CAAE,CAAC;AACJ,CAAC;AAED,eAAerB,oBAAoB","ignoreList":[]}
1
+ {"version":3,"names":["apiFetch","addQueryArgs","decodeEntities","__","fetchLinkSuggestions","search","searchOptions","editorSettings","searchOptionsToUse","isInitialSuggestions","initialSuggestionsSearchOptions","type","subtype","page","perPage","disablePostFormats","queries","push","path","per_page","then","results","map","result","id","url","title","kind","catch","source_url","rendered","responses","Promise","all","flat","filter","sortResults","slice","searchTokens","Set","tokenize","scores","titleTokens","matchingTokens","token","has","length","sort","a","b","text","toLowerCase","match"],"sources":["@wordpress/core-data/src/fetch/__experimental-fetch-link-suggestions.ts"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { addQueryArgs } from '@wordpress/url';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\n\nexport type SearchOptions = {\n\t/**\n\t * Displays initial search suggestions, when true.\n\t */\n\tisInitialSuggestions?: boolean;\n\t/**\n\t * Search options for initial suggestions.\n\t */\n\tinitialSuggestionsSearchOptions?: Omit<\n\t\tSearchOptions,\n\t\t'isInitialSuggestions' | 'initialSuggestionsSearchOptions'\n\t>;\n\t/**\n\t * Filters by search type.\n\t */\n\ttype?: 'attachment' | 'post' | 'term' | 'post-format';\n\t/**\n\t * Slug of the post-type or taxonomy.\n\t */\n\tsubtype?: string;\n\t/**\n\t * Which page of results to return.\n\t */\n\tpage?: number;\n\t/**\n\t * Search results per page.\n\t */\n\tperPage?: number;\n};\n\nexport type EditorSettings = {\n\t/**\n\t * Disables post formats, when true.\n\t */\n\tdisablePostFormats?: boolean;\n};\n\ntype SearchAPIResult = {\n\tid: number;\n\ttitle: string;\n\turl: string;\n\ttype: string;\n\tsubtype: string;\n};\n\ntype MediaAPIResult = {\n\tid: number;\n\ttitle: { rendered: string };\n\tsource_url: string;\n\ttype: string;\n};\n\nexport type SearchResult = {\n\t/**\n\t * Post or term id.\n\t */\n\tid: number;\n\t/**\n\t * Link url.\n\t */\n\turl: string;\n\t/**\n\t * Title of the link.\n\t */\n\ttitle: string;\n\t/**\n\t * The taxonomy or post type slug or type URL.\n\t */\n\ttype: string;\n\t/**\n\t * Link kind of post-type or taxonomy\n\t */\n\tkind?: string;\n};\n\n/**\n * Fetches link suggestions from the WordPress API.\n *\n * WordPress does not support searching multiple tables at once, e.g. posts and terms, so we\n * perform multiple queries at the same time and then merge the results together.\n *\n * @param search\n * @param searchOptions\n * @param editorSettings\n *\n * @example\n * ```js\n * import { __experimentalFetchLinkSuggestions as fetchLinkSuggestions } from '@wordpress/core-data';\n *\n * //...\n *\n * export function initialize( id, settings ) {\n *\n * settings.__experimentalFetchLinkSuggestions = (\n * search,\n * searchOptions\n * ) => fetchLinkSuggestions( search, searchOptions, settings );\n * ```\n */\nexport default async function fetchLinkSuggestions(\n\tsearch: string,\n\tsearchOptions: SearchOptions = {},\n\teditorSettings: EditorSettings = {}\n): Promise< SearchResult[] > {\n\tconst searchOptionsToUse =\n\t\tsearchOptions.isInitialSuggestions &&\n\t\tsearchOptions.initialSuggestionsSearchOptions\n\t\t\t? {\n\t\t\t\t\t...searchOptions,\n\t\t\t\t\t...searchOptions.initialSuggestionsSearchOptions,\n\t\t\t }\n\t\t\t: searchOptions;\n\n\tconst {\n\t\ttype,\n\t\tsubtype,\n\t\tpage,\n\t\tperPage = searchOptions.isInitialSuggestions ? 3 : 20,\n\t} = searchOptionsToUse;\n\n\tconst { disablePostFormats = false } = editorSettings;\n\n\tconst queries: Promise< SearchResult[] >[] = [];\n\n\tif ( ! type || type === 'post' ) {\n\t\tqueries.push(\n\t\t\tapiFetch< SearchAPIResult[] >( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tid: result.id,\n\t\t\t\t\t\t\turl: result.url,\n\t\t\t\t\t\t\ttitle:\n\t\t\t\t\t\t\t\tdecodeEntities( result.title || '' ) ||\n\t\t\t\t\t\t\t\t__( '(no title)' ),\n\t\t\t\t\t\t\ttype: result.subtype || result.type,\n\t\t\t\t\t\t\tkind: 'post-type',\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'term' ) {\n\t\tqueries.push(\n\t\t\tapiFetch< SearchAPIResult[] >( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'term',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tid: result.id,\n\t\t\t\t\t\t\turl: result.url,\n\t\t\t\t\t\t\ttitle:\n\t\t\t\t\t\t\t\tdecodeEntities( result.title || '' ) ||\n\t\t\t\t\t\t\t\t__( '(no title)' ),\n\t\t\t\t\t\t\ttype: result.subtype || result.type,\n\t\t\t\t\t\t\tkind: 'taxonomy',\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! disablePostFormats && ( ! type || type === 'post-format' ) ) {\n\t\tqueries.push(\n\t\t\tapiFetch< SearchAPIResult[] >( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/search', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t\ttype: 'post-format',\n\t\t\t\t\tsubtype,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tid: result.id,\n\t\t\t\t\t\t\turl: result.url,\n\t\t\t\t\t\t\ttitle:\n\t\t\t\t\t\t\t\tdecodeEntities( result.title || '' ) ||\n\t\t\t\t\t\t\t\t__( '(no title)' ),\n\t\t\t\t\t\t\ttype: result.subtype || result.type,\n\t\t\t\t\t\t\tkind: 'taxonomy',\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tif ( ! type || type === 'attachment' ) {\n\t\tqueries.push(\n\t\t\tapiFetch< MediaAPIResult[] >( {\n\t\t\t\tpath: addQueryArgs( '/wp/v2/media', {\n\t\t\t\t\tsearch,\n\t\t\t\t\tpage,\n\t\t\t\t\tper_page: perPage,\n\t\t\t\t} ),\n\t\t\t} )\n\t\t\t\t.then( ( results ) => {\n\t\t\t\t\treturn results.map( ( result ) => {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tid: result.id,\n\t\t\t\t\t\t\turl: result.source_url,\n\t\t\t\t\t\t\ttitle:\n\t\t\t\t\t\t\t\tdecodeEntities( result.title.rendered || '' ) ||\n\t\t\t\t\t\t\t\t__( '(no title)' ),\n\t\t\t\t\t\t\ttype: result.type,\n\t\t\t\t\t\t\tkind: 'media',\n\t\t\t\t\t\t};\n\t\t\t\t\t} );\n\t\t\t\t} )\n\t\t\t\t.catch( () => [] ) // Fail by returning no results.\n\t\t);\n\t}\n\n\tconst responses = await Promise.all( queries );\n\n\tlet results = responses.flat();\n\tresults = results.filter( ( result ) => !! result.id );\n\tresults = sortResults( results, search );\n\tresults = results.slice( 0, perPage );\n\treturn results;\n}\n\n/**\n * Sort search results by relevance to the given query.\n *\n * Sorting is necessary as we're querying multiple endpoints and merging the results. For example\n * a taxonomy title might be more relevant than a post title, but by default taxonomy results will\n * be ordered after all the (potentially irrelevant) post results.\n *\n * We sort by scoring each result, where the score is the number of tokens in the title that are\n * also in the search query, divided by the total number of tokens in the title. This gives us a\n * score between 0 and 1, where 1 is a perfect match.\n *\n * @param results\n * @param search\n */\nexport function sortResults( results: SearchResult[], search: string ) {\n\tconst searchTokens = new Set( tokenize( search ) );\n\n\tconst scores = {};\n\tfor ( const result of results ) {\n\t\tif ( result.title ) {\n\t\t\tconst titleTokens = tokenize( result.title );\n\t\t\tconst matchingTokens = titleTokens.filter( ( token ) =>\n\t\t\t\tsearchTokens.has( token )\n\t\t\t);\n\t\t\tscores[ result.id ] = matchingTokens.length / titleTokens.length;\n\t\t} else {\n\t\t\tscores[ result.id ] = 0;\n\t\t}\n\t}\n\n\treturn results.sort( ( a, b ) => scores[ b.id ] - scores[ a.id ] );\n}\n\n/**\n * Turns text into an array of tokens, with whitespace and punctuation removed.\n *\n * For example, `\"I'm having a ball.\"` becomes `[ \"im\", \"having\", \"a\", \"ball\" ]`.\n *\n * @param text\n */\nexport function tokenize( text: string ): string[] {\n\t// \\p{L} matches any kind of letter from any language.\n\t// \\p{N} matches any kind of num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AEO,CAAC,CAACxB,EAAE,CAAG,CAAC;AACnE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASgB,QAAQA,CAAEU,IAAY,EAAa;EAClD;EACA;EACA,OAAOA,IAAI,CAACC,WAAW,CAAC,CAAC,CAACC,KAAK,CAAE,iBAAkB,CAAC,IAAI,EAAE;AAC3D","ignoreList":[]}
@@ -101,7 +101,7 @@ export interface RenderedText<C extends Context> {
101
101
  * For example, in the block editor, content.rendered could used as a visual preview, and
102
102
  * content.raw could be used to populate the code editor.
103
103
  *
104
- * When updating these rendered fields, Javascript is not be able to properly render arbitrary block
104
+ * When updating these rendered fields, JavaScript is not be able to properly render arbitrary block
105
105
  * markup. Therefore, it stores only the raw markup without the rendered part. And since that's a string,
106
106
  * the entire field becomes a string.
107
107
  *
@@ -57,6 +57,6 @@ declare module './base-entity-records' {
57
57
  }
58
58
  }
59
59
  }
60
- export type PluginStatus = 'active' | 'inactive';
60
+ export type PluginStatus = 'active' | 'inactive' | 'network-active';
61
61
  export type Plugin<C extends Context = 'edit'> = OmitNevers<_BaseEntityRecords.Plugin<C>>;
62
62
  //# sourceMappingURL=plugin.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/entity-types/plugin.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EACX,OAAO,EACP,eAAe,EACf,YAAY,EACZ,UAAU,EACV,MAAM,WAAW,CAAC;AAEnB,OAAO,KAAK,EAAE,iBAAiB,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAErF,OAAO,QAAQ,uBAAuB,CAAC;IACtC,UAAiB,iBAAiB,CAAC;QAClC,UAAiB,MAAM,CAAE,CAAC,SAAS,OAAO;YACzC;;eAEG;YACH,MAAM,EAAE,MAAM,CAAC;YACf;;eAEG;YACH,MAAM,EAAE,YAAY,CAAC;YACrB;;eAEG;YACH,IAAI,EAAE,MAAM,CAAC;YACb;;eA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PLinkSearchType = 'attachment' | 'post' | 'term' | 'post-format';
6
- /**
7
- * A link with an id may be of kind post-type or taxonomy
8
- */
9
- export type WPKind = 'post-type' | 'taxonomy';
10
- export type WPLinkSearchOptions = {
1
+ export type SearchOptions = {
11
2
  /**
12
3
  * Displays initial search suggestions, when true.
13
4
  */
14
- isInitialSuggestions?: boolean | undefined;
5
+ isInitialSuggestions?: boolean;
6
+ /**
7
+ * Search options for initial suggestions.
8
+ */
9
+ initialSuggestionsSearchOptions?: Omit<SearchOptions, 'isInitialSuggestions' | 'initialSuggestionsSearchOptions'>;
15
10
  /**
16
11
  * Filters by search type.
17
12
  */
18
- type?: WPLinkSearchType | undefined;
13
+ type?: 'attachment' | 'post' | 'term' | 'post-format';
19
14
  /**
20
15
  * Slug of the post-type or taxonomy.
21
16
  */
22
- subtype?: string | undefined;
17
+ subtype?: string;
23
18
  /**
24
19
  * Which page of results to return.
25
20
  */
26
- page?: number | undefined;
21
+ page?: number;
27
22
  /**
28
23
  * Search results per page.
29
24
  */
30
- perPage?: number | undefined;
25
+ perPage?: number;
31
26
  };
32
- export type WPLinkSearchResult = {
27
+ export type EditorSettings = {
28
+ /**
29
+ * Disables post formats, when true.
30
+ */
31
+ disablePostFormats?: boolean;
32
+ };
33
+ export type SearchResult = {
33
34
  /**
34
35
  * Post or term id.
35
36
  */
@@ -49,76 +50,17 @@ export type WPLinkSearchResult = {
49
50
  /**
50
51
  * Link kind of post-type or taxonomy
51
52
  */
52
- kind?: WPKind | undefined;
53
- };
54
- export type WPLinkSearchResultAugments = {
55
- /**
56
- * Contains kind information.
57
- */
58
- meta?: {
59
- kind: WPKind;
60
- } | undefined;
61
- /**
62
- * Optional subtype if it exists.
63
- */
64
- subtype?: WPKind | undefined;
65
- };
66
- export type WPLinkSearchResultAugmented = WPLinkSearchResult & WPLinkSearchResultAugments;
67
- export type WPEditorSettings = {
68
- /**
69
- * Disables post formats, when true.
70
- */
71
- disablePostFormats?: boolean | undefined;
53
+ kind?: string;
72
54
  };
73
55
  /**
74
- * Filters the search by type
56
+ * Fetches link suggestions from the WordPress API.
75
57
  *
76
- * @typedef { 'attachment' | 'post' | 'term' | 'post-format' } WPLinkSearchType
77
- */
78
- /**
79
- * A link with an id may be of kind post-type or taxonomy
58
+ * WordPress does not support searching multiple tables at once, e.g. posts and terms, so we
59
+ * perform multiple queries at the same time and then merge the results together.
80
60
  *
81
- * @typedef { 'post-type' | 'taxonomy' } WPKind
82
- */
83
- /**
84
- * @typedef WPLinkSearchOptions
85
- *
86
- * @property {boolean} [isInitialSuggestions] Displays initial search suggestions, when true.
87
- * @property {WPLinkSearchType} [type] Filters by search type.
88
- * @property {string} [subtype] Slug of the post-type or taxonomy.
89
- * @property {number} [page] Which page of results to return.
90
- * @property {number} [perPage] Search results per page.
91
- */
92
- /**
93
- * @typedef WPLinkSearchResult
94
- *
95
- * @property {number} id Post or term id.
96
- * @property {string} url Link url.
97
- * @property {string} title Title of the link.
98
- * @property {string} type The taxonomy or post type slug or type URL.
99
- * @property {WPKind} [kind] Link kind of post-type or taxonomy
100
- */
101
- /**
102
- * @typedef WPLinkSearchResultAugments
103
- *
104
- * @property {{kind: WPKind}} [meta] Contains kind information.
105
- * @property {WPKind} [subtype] Optional subtype if it exists.
106
- */
107
- /**
108
- * @typedef {WPLinkSearchResult & WPLinkSearchResultAugments} WPLinkSearchResultAugmented
109
- */
110
- /**
111
- * @typedef WPEditorSettings
112
- *
113
- * @property {boolean} [ disablePostFormats ] Disables post formats, when true.
114
- */
115
- /**
116
- * Fetches link suggestions from the API.
117
- *
118
- * @async
119
- * @param {string} search
120
- * @param {WPLinkSearchOptions} [searchOptions]
121
- * @param {WPEditorSettings} [settings]
61
+ * @param search
62
+ * @param searchOptions
63
+ * @param editorSettings
122
64
  *
123
65
  * @example
124
66
  * ```js
@@ -133,7 +75,29 @@ export type WPEditorSettings = {
133
75
  * searchOptions
134
76
  * ) => fetchLinkSuggestions( search, searchOptions, settings );
135
77
  * ```
136
- * @return {Promise< WPLinkSearchResult[] >} List of search suggestions
137
78
  */
138
- declare function fetchLinkSuggestions(search: string, searchOptions?: WPLinkSearchOptions | undefined, settings?: WPEditorSettings | undefined): Promise<WPLinkSearchResult[]>;
79
+ export default function fetchLinkSuggestions(search: string, searchOptions?: SearchOptions, editorSettings?: EditorSettings): Promise<SearchResult[]>;
80
+ /**
81
+ * Sort search results by relevance to the given query.
82
+ *
83
+ * Sorting is necessary as we're querying multiple endpoints and merging the results. For example
84
+ * a taxonomy title might be more relevant than a post title, but by default taxonomy results will
85
+ * be ordered after all the (potentially irrelevant) post results.
86
+ *
87
+ * We sort by scoring each result, where the score is the number of tokens in the title that are
88
+ * also in the search query, divided by the total number of tokens in the title. This gives us a
89
+ * score between 0 and 1, where 1 is a perfect match.
90
+ *
91
+ * @param results
92
+ * @param search
93
+ */
94
+ export declare function sortResults(results: SearchResult[], search: string): SearchResult[];
95
+ /**
96
+ * Turns text into an array of tokens, with whitespace and punctuation removed.
97
+ *
98
+ * For example, `"I'm having a ball."` becomes `[ "im", "having", "a", "ball" ]`.
99
+ *
100
+ * @param text
101
+ */
102
+ export declare function tokenize(text: string): string[];
139
103
  //# sourceMappingURL=__experimental-fetch-link-suggestions.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"__experimental-fetch-link-suggestions.d.ts","sourceRoot":"","sources":["../../src/fetch/__experimental-fetch-link-suggestions.js"],"names":[],"mappings":";;;;+BAWc,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,aAAa;;;;qBAM9C,WAAW,GAAG,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;QAgBxB,MAAM;;;;SACN,MAAM;;;;WACN,MAAM;;;;UACN,MAAM;;;;;;;;;;;cAOC,MAAM;;;;;;;0CAKd,kBAAkB,GAAG,0BAA0B;;;;;;;AAxC5D;;;;GAIG;AAEH;;;;GAIG;AAEH;;;;;;;;GAQG;AAEH;;;;;;;;GAQG;AAEH;;;;;GAKG;AAEH;;GAEG;AAEH;;;;GAIG;AAEH;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,8CAnBW,MAAM,6FAiBL,QAAS,kBAAkB,EAAE,CAAE,CA4J1C"}
1
+ {"version":3,"file":"__experimental-fetch-link-suggestions.d.ts","sourceRoot":"","sources":["../../src/fetch/__experimental-fetch-link-suggestions.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,aAAa,GAAG;IAC3B;;OAEG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B;;OAEG;IACH,+BAA+B,CAAC,EAAE,IAAI,CACrC,aAAa,EACb,sBAAsB,GAAG,iCAAiC,CAC1D,CAAC;IACF;;OAEG;IACH,IAAI,CAAC,EAAE,YAAY,GAAG,MAAM,GAAG,MAAM,GAAG,aAAa,CAAC;IACtD;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC5B;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B,CAAC;AAiBF,MAAM,MAAM,YAAY,GAAG;IAC1B;;OAEG;IACH,EAAE,EAAE,MAAM,CAAC;IACX;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;CACd,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAA8B,oBAAoB,CACjD,MAAM,EAAE,MAAM,EACd,aAAa,GAAE,aAAkB,EACjC,cAAc,GAAE,cAAmB,GACjC,OAAO,CAAE,YAAY,EAAE,CAAE,CA0I3B;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,WAAW,CAAE,OAAO,EAAE,YAAY,EAAE,EAAE,MAAM,EAAE,MAAM,kBAiBnE;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAE,IAAI,EAAE,MAAM,GAAI,MAAM,EAAE,CAIjD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/core-data",
3
- "version": "7.0.1",
3
+ "version": "7.1.0",
4
4
  "description": "Access to and manipulation of core WordPress entities.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -32,21 +32,21 @@
32
32
  ],
33
33
  "dependencies": {
34
34
  "@babel/runtime": "^7.16.0",
35
- "@wordpress/api-fetch": "^7.0.1",
36
- "@wordpress/block-editor": "^13.0.1",
37
- "@wordpress/blocks": "^13.0.1",
38
- "@wordpress/compose": "^7.0.1",
39
- "@wordpress/data": "^10.0.1",
40
- "@wordpress/deprecated": "^4.0.1",
41
- "@wordpress/element": "^6.0.1",
42
- "@wordpress/html-entities": "^4.0.1",
43
- "@wordpress/i18n": "^5.0.1",
44
- "@wordpress/is-shallow-equal": "^5.0.1",
45
- "@wordpress/private-apis": "^1.0.1",
46
- "@wordpress/rich-text": "^7.0.1",
47
- "@wordpress/sync": "^1.0.1",
48
- "@wordpress/undo-manager": "^1.0.1",
49
- "@wordpress/url": "^4.0.1",
35
+ "@wordpress/api-fetch": "^7.1.0",
36
+ "@wordpress/block-editor": "^13.1.0",
37
+ "@wordpress/blocks": "^13.1.0",
38
+ "@wordpress/compose": "^7.1.0",
39
+ "@wordpress/data": "^10.1.0",
40
+ "@wordpress/deprecated": "^4.1.0",
41
+ "@wordpress/element": "^6.1.0",
42
+ "@wordpress/html-entities": "^4.1.0",
43
+ "@wordpress/i18n": "^5.1.0",
44
+ "@wordpress/is-shallow-equal": "^5.1.0",
45
+ "@wordpress/private-apis": "^1.1.0",
46
+ "@wordpress/rich-text": "^7.1.0",
47
+ "@wordpress/sync": "^1.1.0",
48
+ "@wordpress/undo-manager": "^1.1.0",
49
+ "@wordpress/url": "^4.1.0",
50
50
  "change-case": "^4.1.2",
51
51
  "equivalent-key-map": "^0.2.2",
52
52
  "fast-deep-equal": "^3.1.3",
@@ -60,5 +60,5 @@
60
60
  "publishConfig": {
61
61
  "access": "public"
62
62
  },
63
- "gitHead": "0e973525f7787401b5a544e0727774d52a78639f"
63
+ "gitHead": "66d3bf12e67d16deddc4b4a9ec42e1d0bed3479a"
64
64
  }
@@ -132,7 +132,7 @@ export interface RenderedText< C extends Context > {
132
132
  * For example, in the block editor, content.rendered could used as a visual preview, and
133
133
  * content.raw could be used to populate the code editor.
134
134
  *
135
- * When updating these rendered fields, Javascript is not be able to properly render arbitrary block
135
+ * When updating these rendered fields, JavaScript is not be able to properly render arbitrary block
136
136
  * markup. Therefore, it stores only the raw markup without the rendered part. And since that's a string,
137
137
  * the entire field becomes a string.
138
138
  *
@@ -73,7 +73,7 @@ declare module './base-entity-records' {
73
73
  }
74
74
  }
75
75
 
76
- export type PluginStatus = 'active' | 'inactive';
76
+ export type PluginStatus = 'active' | 'inactive' | 'network-active';
77
77
  export type Plugin< C extends Context = 'edit' > = OmitNevers<
78
78
  _BaseEntityRecords.Plugin< C >
79
79
  >;