Skip to content
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

T360286: Sections #120

Merged
merged 26 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
2d24bbb
Add sections option to preview controllers
medied May 10, 2024
4133293
Update svgs
medied May 10, 2024
ae7d5a3
Sections main functionality
medied May 13, 2024
b822e50
incorporate getSections
medied May 15, 2024
1621678
Add selected logic and update Sections styling
medied May 15, 2024
12e1092
Update Sections styling
medied May 16, 2024
3c622b2
Add ellipsis truncation
medied May 16, 2024
5a890f1
Scroll selected section element into view
medied May 16, 2024
b6deab2
Add close button
medied May 17, 2024
e12dfd1
Add keyboard navigation with hover styling
medied May 17, 2024
6e89003
Remove getArticleText
medied May 17, 2024
c044967
Add media query for mobile rendering
medied May 17, 2024
5248c9a
Use background-image
medied May 20, 2024
4eaefaa
More readable
medied May 20, 2024
b48d835
Fix keyboard navigation for smoother 'scrolling'
medied May 20, 2024
8a04ae1
Ensure section portion is only added to title when needed
medied May 20, 2024
0f3d2bf
Add loading state
medied May 20, 2024
e2c82b0
Remove popover overflow in preview
medied May 22, 2024
7b381d7
Fix warning and simplify
medied May 22, 2024
fdecfd7
Fine-tune mobile rendering
medied May 22, 2024
d8888cf
Update popover overflow rule in mobile
medied May 29, 2024
b9c66fc
Allow scrolling only for the sections list in mobile
medied May 29, 2024
8664099
Use <Fragment> to simplify DOM
medied May 29, 2024
8418bd2
Fix controllers menu bug
medied May 29, 2024
ad7a004
Fix undefined title bug
medied May 29, 2024
e20913d
Merge branch 'main' into T360286-section-preview-ui
May 31, 2024
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
3 changes: 3 additions & 0 deletions src/link/images/article-black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/link/images/article-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions src/link/images/change-black.svg

This file was deleted.

1 change: 0 additions & 1 deletion src/link/images/change.svg

This file was deleted.

File renamed without changes
3 changes: 3 additions & 0 deletions src/link/images/search-black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/link/images/search-white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 0 additions & 3 deletions src/link/images/search.svg

This file was deleted.

3 changes: 3 additions & 0 deletions src/link/images/selected.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/link/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { getTextContent, slice } from '@wordpress/rich-text';
import { useState, useEffect, createRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { getSiteLanguage } from './utils';
import { getSiteLanguage, titleWithoutSection } from './utils';
import { prefixSearch, fulltextSearch, abortAllRequest } from './api';
import { LanguageSelector } from './language-selector';

Expand All @@ -28,7 +28,7 @@
useEffect( () => {
setTitle( activeAttributes.title || getTextContent( slice( value ) ) );
setLang( activeAttributes.lang || getSiteLanguage() );
}, [ activeAttributes ] );

Check warning on line 31 in src/link/inline.js

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'value'. Either include it or remove the dependency array. If 'setTitle' needs the current value of 'value', you can also switch to useReducer instead of useState and read 'value' in the reducer

useEffect( () => {
if ( title ) {
Expand Down Expand Up @@ -76,7 +76,7 @@
langCodeAdjustment ? 'lang-code-adjustment' : ''
}` }
ref={ inputRef }
value={ title }
value={ titleWithoutSection( title ) }
onChange={ setTitle }
onFocus={ () => setFocused( true ) }
onBlur={ () => setFocused( false ) }
Expand Down
4 changes: 3 additions & 1 deletion src/link/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
if ( e.target.className === 'components-popover__content' ) {
stopViewingPreview();
}
}, [] );

Check warning on line 38 in src/link/popover.js

View workflow job for this annotation

GitHub Actions / build

React Hook useCallback has a missing dependency: 'stopViewingPreview'. Either include it or remove the dependency array. If 'stopViewingPreview' changes too often, find the parent component that defines it and wrap that definition in useCallback

const setPlacement = () => {
if ( isTextNearTheEdge( anchor ) ) {
Expand Down Expand Up @@ -69,10 +69,12 @@
) }
{ viewingPreview && (
<PreviewEditUI
onEdit={ goToEdit }
onEditTopic={ goToEdit }
onRemove={ removeAttributes }
onForceClose={ stopViewingPreview }
activeAttributes={ activeAttributes }
updateAttributes={ updateAttributes }
value={ value }
/>
) }
</Popover>
Expand Down
136 changes: 89 additions & 47 deletions src/link/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,59 @@
} from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import wikipediaPreview from 'wikipedia-preview';
import { Sections } from './sections';

export const PreviewEditUI = ( {
activeAttributes,
onForceClose,
onEdit,
onEditTopic,
onRemove,
onForceClose,
activeAttributes,
updateAttributes,
value,
} ) => {
const [ previewHtml, setPreviewHtml ] = useState( null );
const [ selectingSection, setSelectingSection ] = useState( false );
const [ showControllersMenu, setShowControllersMenu ] = useState( true );
const toggleControllersMenu = () => {
/* eslint-disable-next-line no-shadow */
setShowControllersMenu( ( showControllersMenu ) => ! showControllersMenu );
};

const showSections = () => {
setSelectingSection( true );
};

const insertControllersMenu = () => {
const preview = document.querySelector( '.wikipediapreview' );
const previewHeader = document.querySelector(
'.wikipediapreview-header'
);
const previewHeaderCloseBtn = document.querySelector(
'.wikipediapreview-header-closebtn'
);
const controllersMenu = document.createElement( 'div' );
controllersMenu.setAttribute(
'class',
'wikipediapreview-edit-preview-controllers-menu'
);
controllersMenu.addEventListener( 'click', toggleControllersMenu );
setShowControllersMenu( false );

if ( previewHeader ) {
previewHeader.insertBefore(
controllersMenu,
previewHeaderCloseBtn
);
}

// special handle to set the container direction
if ( preview ) {
document
.querySelector( '.wikipediapreview-edit-preview-container' )
.setAttribute( 'dir', preview.getAttribute( 'dir' ) );
}
};

useEffect( () => {
const { title, lang } = activeAttributes;
if ( title && lang ) {
Expand All @@ -30,34 +69,9 @@

useEffect( () => {
if ( isPopoverExpanded() ) {
const preview = document.querySelector( '.wikipediapreview' );
const previewHeader = document.querySelector(
'.wikipediapreview-header'
);
const previewHeaderCloseBtn = document.querySelector(
'.wikipediapreview-header-closebtn'
);
const controllersMenu = document.createElement( 'div' );
controllersMenu.setAttribute(
'class',
'wikipediapreview-edit-preview-controllers-menu'
);
controllersMenu.addEventListener( 'click', toggleControllersMenu );
setShowControllersMenu( false );

if ( previewHeader ) {
previewHeader.insertBefore(
controllersMenu,
previewHeaderCloseBtn
);
}

// special handle to set the container direction
if ( preview ) {
document
.querySelector( '.wikipediapreview-edit-preview-container' )
.setAttribute( 'dir', preview.getAttribute( 'dir' ) );
}
// The parent header div (where the menu needs to be inserted)
// comes from previewHtml so we need to construct the menu on the fly
insertControllersMenu();

return () => {
document
Expand All @@ -67,7 +81,7 @@
?.removeEventListener( 'click', toggleControllersMenu );
};
}
}, [ previewHtml ] );
}, [ previewHtml, selectingSection ] );

Check warning on line 84 in src/link/preview.js

View workflow job for this annotation

GitHub Actions / build

React Hook useEffect has a missing dependency: 'insertControllersMenu'. Either include it or remove the dependency array

useLayoutEffect( () => {
document
Expand All @@ -78,18 +92,30 @@
.querySelector( '.wikipediapreview-header-closebtn' )
?.removeEventListener( 'click', onForceClose );
};
}, [ previewHtml ] );
}, [ previewHtml, selectingSection ] );

Check warning on line 95 in src/link/preview.js

View workflow job for this annotation

GitHub Actions / build

React Hook useLayoutEffect has a missing dependency: 'onForceClose'. Either include it or remove the dependency array

return (
<div className="wikipediapreview-edit-preview-container">
<div
className="wikipediapreview-edit-preview"
dangerouslySetInnerHTML={ { __html: previewHtml } }
></div>
{ previewHtml && showControllersMenu && (
<ControllerEditUI onEdit={ onEdit } onRemove={ onRemove } />
<>
{ ! selectingSection ? (
<div className="wikipediapreview-edit-preview-container">
<div
className="wikipediapreview-edit-preview"
dangerouslySetInnerHTML={ { __html: previewHtml } }
></div>
{ previewHtml && showControllersMenu && (
<ControllerEditUI onEditTopic={ onEditTopic } onEditSection={ showSections } onRemove={ onRemove } />
) }
</div>
) : (
<Sections
value={ value }
updateAttributes={ updateAttributes }
activeAttributes={ activeAttributes }
setPreviewHtml={ setPreviewHtml }
setSelectingSection={ setSelectingSection }
/>
) }
</div>
</>
);
};

Expand All @@ -101,22 +127,38 @@
return hasPreviewPopup && hasExpandedClass;
};

const ControllerEditUI = ( { onEdit, onRemove } ) => {
const ControllerEditUI = ( { onEditTopic, onEditSection, onRemove } ) => {
return (
<div className="wikipediapreview-edit-preview-controllers">
<div
className="wikipediapreview-edit-preview-controllers-change"
onClick={ onEdit }
className="wikipediapreview-edit-preview-controllers-option"
onClick={ onEditTopic }
role="presentation"
>
<div className="wikipediapreview-edit-preview-controllers-option-icon-change"></div>
<div className="wikipediapreview-edit-preview-controllers-option-message">
{ __( 'Topic', 'wikipedia-preview' ) }
</div>
</div>
<div
className="wikipediapreview-edit-preview-controllers-option"
onClick={ onEditSection }
role="presentation"
>
{ __( 'Change', 'wikipedia-preview' ) }
<div className="wikipediapreview-edit-preview-controllers-option-icon-sections"></div>
<div className="wikipediapreview-edit-preview-controllers-option-message">
{ __( 'Sections', 'wikipedia-preview' ) }
</div>
</div>
<div
className="wikipediapreview-edit-preview-controllers-remove"
className="wikipediapreview-edit-preview-controllers-option"
onClick={ onRemove }
role="presentation"
>
{ __( 'Remove', 'wikipedia-preview' ) }
<div className="wikipediapreview-edit-preview-controllers-option-icon-remove"></div>
<div className="wikipediapreview-edit-preview-controllers-option-message">
{ __( 'Remove', 'wikipedia-preview' ) }
</div>
</div>
</div>
);
Expand Down
Loading
Loading