-
Notifications
You must be signed in to change notification settings - Fork 0
Add manual selection #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pamprn09
wants to merge
3
commits into
main
Choose a base branch
from
manual-select
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| const { addFilter } = wp.hooks; | ||
| const { createHigherOrderComponent } = wp.compose; | ||
| const { InspectorControls } = wp.blockEditor; | ||
| const { PanelBody, FormTokenField, ToggleControl } = wp.components; | ||
| const { Fragment, useState } = wp.element; | ||
| const apiFetch = wp.apiFetch; | ||
|
|
||
| // Module-level map avoids stale closure bugs with useState in onChange callbacks. | ||
| const _titleToId = {}; | ||
|
|
||
|
|
||
| /** | ||
| * Inspector UI | ||
| */ | ||
| const withInspectorControls = createHigherOrderComponent((BlockEdit) => { | ||
|
|
||
| return (props) => { | ||
|
|
||
| if (props.name !== "core/query") { | ||
| return wp.element.createElement(BlockEdit, props); | ||
| } | ||
|
|
||
| const { attributes, setAttributes } = props; | ||
|
|
||
| const query = attributes.query || {}; | ||
| const selected = query.manualPosts || []; | ||
| const isManual = query.manualSelect === true; | ||
|
|
||
| const tokens = selected.map((p) => p.title); | ||
|
|
||
| const [ suggestions, setSuggestions ] = useState([]); | ||
|
|
||
|
|
||
| const onToggleManual = (value) => { | ||
| if ( value ) { | ||
| // Re-enable: manualPosts already stored in query (preserved from last session). | ||
| // Derive include + perPage from them so the editor preview updates immediately. | ||
| const restored = query.manualPosts || []; | ||
| const ids = restored.map( (p) => p.id ); | ||
|
|
||
| // Rebuild _titleToId so onChange works with existing selections immediately. | ||
| restored.forEach( (p) => { _titleToId[ p.title ] = p.id; } ); | ||
|
|
||
| setAttributes({ | ||
| query: { | ||
| ...query, | ||
| manualSelect: true, | ||
| _previousInherit: query.inherit, | ||
| _previousPerPage: query.perPage, | ||
| inherit: false, | ||
| ...( ids.length > 0 && { | ||
| include: ids, | ||
| perPage: ids.length, | ||
| } ) | ||
| } | ||
| }); | ||
| } else { | ||
| // Disable: remove only the active-mode derived keys (include, perPage). | ||
| // manualPosts is intentionally kept so it can be restored on re-enable. | ||
| // eslint-disable-next-line no-unused-vars | ||
| const { include: _inc, manualSelect: _ms, _previousInherit, perPage: _pp, per_page: _ppp, _previousPerPage, ...restQuery } = query; | ||
| setAttributes({ | ||
| query: { | ||
| ...restQuery, | ||
| inherit: _previousInherit !== undefined ? _previousInherit : restQuery.inherit || false, | ||
| ...( _previousPerPage !== undefined && { perPage: _previousPerPage } ) | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| const searchPosts = (value) => { | ||
|
|
||
| if (!value || value.length < 2) return; | ||
|
|
||
| apiFetch({ | ||
| path: "/wp/v2/search?search=" + encodeURIComponent(value) + "&type=post" | ||
| }).then((results) => { | ||
|
|
||
| const titles = []; | ||
|
|
||
| results.forEach((r) => { | ||
| titles.push(r.title); | ||
| _titleToId[r.title] = r.id; | ||
| }); | ||
|
|
||
| setSuggestions(titles); | ||
|
|
||
| }); | ||
|
|
||
| }; | ||
|
|
||
|
|
||
| const onChange = (newTokens) => { | ||
|
|
||
| const posts = newTokens.map((title) => { | ||
|
|
||
| const existing = selected.find((p) => p.title === title); | ||
| if (existing) return existing; | ||
|
|
||
| return { | ||
| id: _titleToId[title], | ||
| title: title | ||
| }; | ||
|
|
||
| }).filter((p) => p && p.id); | ||
|
|
||
| const ids = posts.map((p) => p.id); | ||
|
|
||
| // When posts are selected: force include + disable inherit so the | ||
| // editor preview updates live. When cleared: restore normal behavior. | ||
| if ( ids.length > 0 ) { | ||
| setAttributes({ | ||
| query: { | ||
| ...query, | ||
| manualPosts: posts, | ||
| include: ids, | ||
| perPage: ids.length, | ||
| } | ||
| }); | ||
| } else { | ||
| // eslint-disable-next-line no-unused-vars | ||
| const { include: _inc, perPage: _pp, per_page: _ppp, ...restQuery } = query; | ||
| setAttributes({ | ||
| query: { | ||
| ...restQuery, | ||
| manualPosts: [] | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
|
|
||
| return wp.element.createElement( | ||
| Fragment, | ||
| {}, | ||
| wp.element.createElement(BlockEdit, props), | ||
| wp.element.createElement( | ||
| InspectorControls, | ||
| {}, | ||
| wp.element.createElement( | ||
| PanelBody, | ||
| { title: "Manual Post Selection", initialOpen: false }, | ||
| wp.element.createElement(ToggleControl, { | ||
| label: "Enable manual post selection", | ||
| checked: isManual, | ||
| onChange: onToggleManual | ||
| }), | ||
| isManual && wp.element.createElement(FormTokenField, { | ||
| label: "Search and select posts", | ||
| value: tokens, | ||
| suggestions: suggestions, | ||
| onInputChange: searchPosts, | ||
| onChange: onChange | ||
| }) | ||
| ) | ||
| ) | ||
| ); | ||
|
|
||
| }; | ||
|
|
||
| }, "withInspectorControls"); | ||
|
|
||
|
|
||
| addFilter( | ||
| "editor.BlockEdit", | ||
| "manual-query-loop/add-inspector", | ||
| withInspectorControls | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there an asset version we can use like those above?
filemtimecan give different results when the environment has multiple servers, probably fine for dev/local but could cause mystery issues/worse performance in production