Repository navigation
Move the form outline into an Outline tab in the builder sidebar #3689
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
| /** | ||
| * The Outline tab in the builder's side panel: search and jump to pages, sections and fields. | ||
| */ | ||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| die( 'You are not allowed to call this page directly.' ); | ||
| } | ||
| ?> | ||
| <div id="frm-builder-outline" class="tabs-panel frm-builder-outline-panel"> | ||
| <p class="frm-search"> | ||
| <label class="screen-reader-text" for="frm-builder-outline-search-input"> | ||
| <?php esc_html_e( 'Search pages and fields', 'formidable' ); ?> | ||
| </label> | ||
| <?php FrmAppHelper::icon_by_class( 'frmfont frm_search_icon frm_svg20' ); ?> | ||
| <input type="search" id="frm-builder-outline-search-input" class="frm-search-input" autocomplete="off" placeholder="<?php esc_attr_e( 'Search pages and fields', 'formidable' ); ?>" /> | ||
| </p> | ||
| <nav aria-label="<?php esc_attr_e( 'Form Outline', 'formidable' ); ?>"> | ||
| <ul class="frm-builder-outline-list"></ul> | ||
| </nav> | ||
| <div class="frm-builder-outline-empty frm_hidden"> | ||
| <strong class="frm-builder-outline-empty-title"></strong> | ||
| <span class="frm-builder-outline-empty-text"></span> | ||
| </div> | ||
| <p class="screen-reader-text frm-builder-outline-status" aria-live="polite"></p> | ||
| </div> |
Large diffs are not rendered by default.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,17 +31,18 @@ const getFieldText = field => { | |
| }; | ||
|
|
||
| /** | ||
| * Add a form outline to the builder: a button floating at the bottom right of the canvas that opens a popover | ||
| * to search and jump to pages, sections and fields. | ||
| * Add a form outline to the builder: the Outline tab in the side panel lists the form's pages, | ||
| * sections and fields in order, and jumps to any of them. | ||
| * | ||
| * @since x.x | ||
| * @return {void} | ||
| */ | ||
| export function initFormOutline() { | ||
| const popover = document.getElementById( 'frm-builder-outline' ); | ||
| const toggle = document.getElementById( 'frm-builder-outline-toggle' ); | ||
| const tab = toggle?.closest( 'li' ); | ||
| const builder = document.getElementById( 'frm-show-fields' ); | ||
| if ( ! popover || ! toggle || ! builder ) { | ||
| if ( ! popover || ! tab || ! builder ) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -51,7 +52,7 @@ export function initFormOutline() { | |
| const emptyTitle = emptyMessage.querySelector( '.frm-builder-outline-empty-title' ); | ||
| const emptyText = emptyMessage.querySelector( '.frm-builder-outline-empty-text' ); | ||
| const status = popover.querySelector( '.frm-builder-outline-status' ); | ||
| const isOpen = () => ! popover.hidden; | ||
| const isOpen = () => Boolean( tab?.classList.contains( 'frm-active' ) ); | ||
| const scroller = document.getElementById( 'post-body-content' ); | ||
| let currentPageIndex = 0; | ||
| let observer; | ||
|
|
@@ -94,105 +95,88 @@ export function initFormOutline() { | |
| const children = []; | ||
| let fieldResultCount = 0; | ||
|
|
||
| pages.forEach( page => { | ||
| const pageMatches = page.title.toLocaleLowerCase().includes( query ); | ||
| const matchingFields = query === '' | ||
| ? [] | ||
| : page.fields.filter( field => { | ||
| // Sections already get their own row. | ||
| if ( field.dataset.type === SECTION_TYPE ) { | ||
| return false; | ||
| } | ||
| const fieldText = getFieldText( field ).toLocaleLowerCase(); | ||
| return fieldText.includes( query ) || field.dataset.fid.includes( query ); | ||
| } ); | ||
|
|
||
| const matchingSections = page.fields.filter( field => { | ||
| if ( field.dataset.type !== SECTION_TYPE ) { | ||
| return false; | ||
| } | ||
| if ( query === '' || pageMatches ) { | ||
| return true; | ||
| } | ||
|
|
||
| const sectionText = getFieldText( field ).toLocaleLowerCase(); | ||
| return sectionText.includes( query ) || field.dataset.fid.includes( query ); | ||
| const makeButton = ( className, text, field, pageIndex ) => { | ||
| const button = tag( 'button', { | ||
| className, | ||
| child: span( { className: 'frm-builder-outline-label', text } ) | ||
| } ); | ||
| button.type = 'button'; | ||
| button.title = text; | ||
| button.dataset.fieldId = field.dataset.fid; | ||
| button.dataset.pageIndex = pageIndex; | ||
| return tag( 'li', { child: button } ); | ||
| }; | ||
|
|
||
| if ( query !== '' && ! pageMatches && matchingFields.length === 0 && matchingSections.length === 0 ) { | ||
| return; | ||
| } | ||
|
|
||
| pages.forEach( page => { | ||
| const pageMatches = page.title.toLocaleLowerCase().includes( query ); | ||
| const [ firstField ] = page.fields; | ||
| if ( ! firstField ) { | ||
| return; | ||
| } | ||
|
|
||
| const pageLabel = `${ sprintf( __( 'Page %1$d', 'formidable' ), page.index + 1 ) }: ${ page.title }`; | ||
| const fieldCount = page.fields.filter( field => ! STRUCTURE_TYPES.includes( field.dataset.type ) ).length; | ||
| const pageButton = tag( 'button', { | ||
| className: 'frm-builder-outline-page', | ||
| children: [ | ||
| span( { className: 'frm-builder-outline-label', text: pageLabel } ), | ||
| /* translators: %d: Number of fields on the page */ | ||
| span( { className: 'frm-builder-outline-meta', text: sprintf( _n( '%d field', '%d fields', fieldCount, 'formidable' ), fieldCount ) } ) | ||
| ] | ||
| } ); | ||
| pageButton.type = 'button'; | ||
| pageButton.title = pageLabel; | ||
| if ( page.index === currentPageIndex && query === '' ) { | ||
| pageButton.setAttribute( 'aria-current', 'location' ); | ||
| } | ||
| pageButton.dataset.fieldId = firstField.dataset.fid; | ||
| pageButton.dataset.pageIndex = page.index; | ||
| children.push( tag( 'li', { child: pageButton } ) ); | ||
|
|
||
| matchingSections.forEach( field => { | ||
| const sectionText = getFieldText( field ); | ||
| // Sections and fields in form order. With a search, only the ones that match. | ||
| const rows = []; | ||
| page.fields.forEach( field => { | ||
| const { type } = field.dataset; | ||
| if ( type === 'end_divider' ) { | ||
| return; | ||
| } | ||
| const text = getFieldText( field ); | ||
| if ( ! text ) { | ||
| return; | ||
| } | ||
| const isSection = type === SECTION_TYPE; | ||
| if ( query !== '' ) { | ||
| const matches = text.toLocaleLowerCase().includes( query ) || field.dataset.fid.includes( query ); | ||
| if ( ! matches && ! ( isSection && pageMatches ) ) { | ||
| return; | ||
| } | ||
| if ( ! isSection && fieldResultCount >= MAX_FIELD_RESULTS ) { | ||
| return; | ||
| } | ||
| } | ||
| // A page named after its first section would list that name twice in a row. | ||
| if ( ! sectionText || ( query === '' && field === firstField && sectionText === page.title ) ) { | ||
| if ( isSection && field === firstField && text === page.title && pages.length > 1 ) { | ||
| return; | ||
| } | ||
|
|
||
| const sectionButton = tag( 'button', { | ||
| className: 'frm-builder-outline-section', | ||
| child: span( { className: 'frm-builder-outline-label', text: sectionText } ) | ||
| } ); | ||
| sectionButton.type = 'button'; | ||
| sectionButton.title = sectionText; | ||
| sectionButton.dataset.fieldId = field.dataset.fid; | ||
| sectionButton.dataset.pageIndex = page.index; | ||
| children.push( tag( 'li', { child: sectionButton } ) ); | ||
| rows.push( makeButton( isSection ? 'frm-builder-outline-section' : 'frm-builder-outline-field', text, field, page.index ) ); | ||
| if ( ! isSection && query !== '' ) { | ||
| fieldResultCount++; | ||
| } | ||
| } ); | ||
|
|
||
| if ( query === '' || fieldResultCount >= MAX_FIELD_RESULTS ) { | ||
| if ( query !== '' && ! pageMatches && rows.length === 0 ) { | ||
| return; | ||
| } | ||
|
|
||
| matchingFields.forEach( field => { | ||
| if ( fieldResultCount >= MAX_FIELD_RESULTS ) { | ||
| return; | ||
| } | ||
| const fieldText = getFieldText( field ); | ||
| const fieldButton = tag( 'button', { | ||
| className: 'frm-builder-outline-field', | ||
| child: span( { className: 'frm-builder-outline-label', text: fieldText } ) | ||
| // A one-page form has no pages to show, only its sections and fields. | ||
| if ( pages.length > 1 ) { | ||
| const pageLabel = `${ sprintf( __( 'Page %1$d', 'formidable' ), page.index + 1 ) }: ${ page.title }`; | ||
| const fieldCount = page.fields.filter( field => ! STRUCTURE_TYPES.includes( field.dataset.type ) ).length; | ||
| const pageButton = tag( 'button', { | ||
| className: 'frm-builder-outline-page', | ||
| children: [ | ||
| span( { className: 'frm-builder-outline-label', text: pageLabel } ), | ||
| /* translators: %d: Number of fields on the page */ | ||
| span( { className: 'frm-builder-outline-meta', text: sprintf( _n( '%d field', '%d fields', fieldCount, 'formidable' ), fieldCount ) } ) | ||
| ] | ||
| } ); | ||
| fieldButton.type = 'button'; | ||
| fieldButton.title = fieldText; | ||
| fieldButton.dataset.fieldId = field.dataset.fid; | ||
| fieldButton.dataset.pageIndex = page.index; | ||
| children.push( tag( 'li', { child: fieldButton } ) ); | ||
| fieldResultCount++; | ||
| } ); | ||
| pageButton.type = 'button'; | ||
| pageButton.title = pageLabel; | ||
| if ( page.index === currentPageIndex && query === '' ) { | ||
| pageButton.setAttribute( 'aria-current', 'location' ); | ||
| } | ||
| pageButton.dataset.fieldId = firstField.dataset.fid; | ||
| pageButton.dataset.pageIndex = page.index; | ||
| children.push( tag( 'li', { child: pageButton } ) ); | ||
| } | ||
| children.push( ...rows ); | ||
| } ); | ||
|
|
||
| const resultCount = children.length; | ||
| const hasFields = pages.some( page => page.fields.length > 0 ); | ||
| // A lone page with no sections gives nothing to jump to, so explain how to add some instead. | ||
| const hasOutline = pages.length > 1 || resultCount > 1; | ||
| const noResults = query !== '' && resultCount === 0; | ||
| const showEmpty = noResults || ( query === '' && ! hasOutline ); | ||
| const showEmpty = noResults || ! hasFields; | ||
| const fragment = document.createDocumentFragment(); | ||
| if ( ! showEmpty ) { | ||
| fragment.append( ...children ); | ||
|
|
@@ -205,10 +189,8 @@ export function initFormOutline() { | |
| emptyTitle.textContent = sprintf( __( 'No results for “%s”', 'formidable' ), search.value.trim() ); | ||
| emptyText.textContent = __( 'Try a page, section or field name.', 'formidable' ); | ||
| } else if ( showEmpty ) { | ||
| emptyTitle.textContent = hasFields ? __( 'No pages or sections yet', 'formidable' ) : __( 'No fields yet', 'formidable' ); | ||
| emptyText.textContent = hasFields | ||
| ? __( 'Add a Page Break or Section field to split this form into parts. You can still search above to jump to any field.', 'formidable' ) | ||
| : __( 'Fields you add to this form will show up here.', 'formidable' ); | ||
| emptyTitle.textContent = __( 'No fields yet', 'formidable' ); | ||
| emptyText.textContent = __( 'Fields you add to this form will show up here, so you can jump to any of them.', 'formidable' ); | ||
| } | ||
| if ( query === '' ) { | ||
| status.textContent = sprintf( _n( '%d page', '%d pages', pages.length, 'formidable' ), pages.length ); | ||
|
|
@@ -259,80 +241,33 @@ export function initFormOutline() { | |
| const getPagePills = () => Array.from( document.querySelectorAll( '#frm-fake-page .frm-collapse-page, #frm-show-fields li[data-type="break"] .frm-collapse-page' ) ); | ||
| const getPillPage = pill => Number( pill.querySelector( '.frm-page-num' )?.textContent ) - 1; | ||
|
|
||
| const placePopover = () => { | ||
| const rect = toggle.getBoundingClientRect(); | ||
| // Line up with the button's right edge, like a menu opened from it. | ||
| popover.style.right = `${ window.innerWidth - rect.right }px`; | ||
| // Open upward, so the list grows away from the button. | ||
| popover.style.bottom = `${ window.innerHeight - rect.top + 8 }px`; | ||
| }; | ||
|
|
||
| // Track the page at the top of the canvas, so the outline can mark it. | ||
| const updateDock = () => { | ||
| const pills = getPagePills(); | ||
| const updateCurrentPage = () => { | ||
| if ( ! isOpen() ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Fix: compute the index without rendering, and call it from |
||
| return; | ||
| } | ||
| const { top } = scroller.getBoundingClientRect(); | ||
| let index = -1; | ||
| pills.forEach( pill => { | ||
| getPagePills().forEach( pill => { | ||
| if ( pill.offsetParent && pill.getBoundingClientRect().top < top + 8 ) { | ||
| index = Math.max( index, getPillPage( pill ) ); | ||
| } | ||
| } ); | ||
| const pages = getPages(); | ||
| const pageChanged = currentPageIndex !== Math.max( index, 0 ); | ||
| currentPageIndex = Math.max( index, 0 ); | ||
| const page = pages[ currentPageIndex ]; | ||
| const hasPages = pages.length > 1 && Boolean( page ); | ||
| // The outline only earns its place once the form is split into pages. | ||
| toggle.parentElement.hidden = ! hasPages; | ||
| if ( ! hasPages ) { | ||
| if ( isOpen() ) { | ||
| setOpen( false ); | ||
| } | ||
| return; | ||
| } | ||
| if ( isOpen() ) { | ||
| placePopover(); | ||
| if ( pageChanged ) { | ||
| render(); | ||
| } | ||
| if ( currentPageIndex !== Math.max( index, 0 ) ) { | ||
| currentPageIndex = Math.max( index, 0 ); | ||
| render(); | ||
| } | ||
| }; | ||
|
|
||
| let dockFrame; | ||
| const queueDockUpdate = () => { | ||
| window.cancelAnimationFrame( dockFrame ); | ||
| dockFrame = window.requestAnimationFrame( updateDock ); | ||
| }; | ||
|
|
||
| const setOpen = open => { | ||
| popover.hidden = ! open; | ||
| toggle.setAttribute( 'aria-expanded', String( open ) ); | ||
| toggle.classList.toggle( 'frm-active', open ); | ||
| updateDock(); | ||
| updateOutline(); | ||
| if ( open ) { | ||
| search.focus(); | ||
| } | ||
| let scrollFrame; | ||
| const queueCurrentPageUpdate = () => { | ||
| window.cancelAnimationFrame( scrollFrame ); | ||
| scrollFrame = window.requestAnimationFrame( updateCurrentPage ); | ||
| }; | ||
|
|
||
| toggle.addEventListener( 'click', () => setOpen( popover.hidden ) ); | ||
| document.addEventListener( 'keydown', event => { | ||
| if ( event.key === 'Escape' && ! popover.hidden ) { | ||
| setOpen( false ); | ||
| toggle.focus(); | ||
| } | ||
| } ); | ||
| // Not a modal: the builder stays usable, and clicking anywhere else closes the outline. | ||
| document.addEventListener( 'pointerdown', event => { | ||
| if ( ! popover.hidden && ! popover.contains( event.target ) && ! toggle.contains( event.target ) ) { | ||
| setOpen( false ); | ||
| } | ||
| } ); | ||
| scroller.addEventListener( 'scroll', queueDockUpdate, { passive: true } ); | ||
| window.addEventListener( 'resize', queueDockUpdate ); | ||
| // Page breaks load and move with the builder, so recheck the dock when they do. | ||
| new MutationObserver( queueDockUpdate ).observe( builder, { childList: true, subtree: true } ); | ||
| updateDock(); | ||
| // The tab navigator switches tabs by class, from clicks, keys and code, so watch the class. | ||
| new MutationObserver( updateOutline ).observe( tab, { attributes: true, attributeFilter: [ 'class' ] } ); | ||
| scroller.addEventListener( 'scroll', queueCurrentPageUpdate, { passive: true } ); | ||
| search.addEventListener( 'input', render ); | ||
| // Arrow keys move between the search box and the results, like a menu. Safari's Tab skips buttons by default. | ||
| popover.addEventListener( 'keydown', event => { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
On a multi-page form the first field of each page is listed twice in a row. The page row's title falls back to the first non-HTML field's label when the page has no heading (
getPages,title), and this line then also adds that field as its own row. The old code skipped this duplicate for sections; the check above (isSection && field === firstField && text === page.title && pages.length > 1) still only covers sections. Seen in a simulated multi-page DOM (screenshot in the review body):Page 2: Textis followed directly by aTextrow, andPage 1: Bringing a guest?by aBringing a guest?row.Fix: apply that skip to any first field when no search is active (a search result must still show its match):
Replace the existing
isSection && field === firstField && ...check with this one.