Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions classes/views/frm-forms/_outline_panel.php
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>
8 changes: 8 additions & 0 deletions classes/views/frm-forms/add_field_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<?php esc_html_e( 'Field Options', 'formidable' ); ?>
</a>
</li>
<li class="hide-if-no-js">
<a href="#frm-builder-outline" id="frm-builder-outline-toggle">
<?php esc_html_e( 'Outline', 'formidable' ); ?>
</a>
</li>
<?php do_action( 'frm_extra_form_instruction_tabs' ); ?>
</ul>
</div>
Expand Down Expand Up @@ -223,6 +228,9 @@
</form>
</div>
</div>
<div>
<?php require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/_outline_panel.php'; ?>
</div>
<?php
if ( isset( $form ) ) {
/**
Expand Down
23 changes: 0 additions & 23 deletions classes/views/frm-forms/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,6 @@

</div>

<div class="frm-builder-outline hide-if-no-js">
<button type="button" id="frm-builder-outline-toggle" class="button frm-button-secondary frm-with-icon" aria-expanded="false" aria-controls="frm-builder-outline">
<?php FrmAppHelper::icon_by_class( 'frmfont frm_map_icon frm-builder-outline-icon', array( 'aria-hidden' => 'true' ) ); ?>
<span class="frm-builder-outline-current"><?php esc_html_e( 'Form outline', 'formidable' ); ?></span>
</button>
</div>
<div id="frm-builder-outline" class="frm-builder-outline-popover hide-if-no-js" role="dialog" aria-label="<?php esc_attr_e( 'Form outline', 'formidable' ); ?>" hidden>
<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>
</div>

</div>
Expand Down
2 changes: 1 addition & 1 deletion css/frm_admin.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

231 changes: 83 additions & 148 deletions js/src/admin/formOutline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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 ) );

@franky-the-going-merry franky-the-going-merry Bot Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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: Text is followed directly by a Text row, and Page 1: Bringing a guest? by a Bringing a guest? row.

Fix: apply that skip to any first field when no search is active (a search result must still show its match):

if ( field === firstField && text === page.title && pages.length > 1 && query === '' ) {
	return;
}

Replace the existing isSection && field === firstField && ... check with this one.

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 );
Expand All @@ -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 );
Expand Down Expand Up @@ -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() ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateCurrentPage returns early while the tab is closed, and updateOutline (line 230) renders without calling it, so currentPageIndex is only refreshed by scroll events that happen while the Outline tab is open. If the user scrolls down to page 3, then opens the Outline tab, the highlight sits on page 1 (the last value computed) until they scroll again. The PR description says the page you are scrolled to is highlighted.

Fix: compute the index without rendering, and call it from updateOutline before render(). For example, split the loop into a getCurrentPageIndex() helper that updateCurrentPage and updateOutline both use, so opening the tab renders with the right page. (This is read from the source; page tracking needs Pro's page pills, which were not available in the preview env.)

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 => {
Expand Down
Loading