-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Footnotes (again): MVP #47682
Closed
Closed
Footnotes (again): MVP #47682
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a78e12c
Rich text: add dynamic data API
ellatrix 4a626af
Try with shortcode
ellatrix 9d48aa9
Try rendering below content again.
ellatrix 55da3a4
wip
ellatrix 3de9f6e
Add UI
ellatrix cee1a68
Simplify UI
ellatrix bea2ff8
PHP: use shortcode regex and make accessible
ellatrix f41ac6d
Fix PHP linting errors
ellatrix 08c8c11
Fix PHP linting errors
ellatrix 92a468c
Reset fn array
ellatrix 9af8ccd
Fix e2e tests
ellatrix 821ed26
Fix PHP docs
ellatrix 169a101
More PHP lint fixes
ellatrix b3c2e64
Reorg
ellatrix 7a981ba
Render footnotes with portal
ellatrix 8e95826
wip
ellatrix b7d5a41
wip
ellatrix f63c285
Use replaceDataByType
ellatrix 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,43 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { insertObject } from '@wordpress/rich-text'; | ||
import { RichTextToolbarButton } from '@wordpress/block-editor'; | ||
import { formatListNumbered } from '@wordpress/icons'; | ||
import { dateI18n } from '@wordpress/date'; | ||
|
||
const name = 'core/current-year'; | ||
const title = __( 'Current Year' ); | ||
|
||
function Time() { | ||
const year = dateI18n( 'Y', new Date() ); | ||
return <time dateTime={ year }>{ year }</time>; | ||
} | ||
|
||
export const currentYear = { | ||
name, | ||
title, | ||
tagName: 'data', | ||
render: Time, | ||
saveFallback: Time, | ||
edit( { isObjectActive, value, onChange, onFocus } ) { | ||
function onClick() { | ||
const newValue = insertObject( value, { | ||
type: name, | ||
} ); | ||
newValue.start = newValue.end - 1; | ||
onChange( newValue ); | ||
onFocus(); | ||
} | ||
|
||
return ( | ||
<RichTextToolbarButton | ||
icon={ formatListNumbered } | ||
title={ title } | ||
onClick={ onClick } | ||
isActive={ isObjectActive } | ||
/> | ||
); | ||
}, | ||
}; |
This file contains 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
This file contains 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,112 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { insertObject, useAnchor } from '@wordpress/rich-text'; | ||
import { RichTextToolbarButton } from '@wordpress/block-editor'; | ||
import { formatListNumbered } from '@wordpress/icons'; | ||
import { Popover, TextControl } from '@wordpress/components'; | ||
|
||
const name = 'core/footnote'; | ||
const title = __( 'Footnote' ); | ||
|
||
export const footnote = { | ||
name, | ||
title, | ||
tagName: 'data', | ||
attributes: { | ||
note: ( element ) => | ||
element.innerHTML.replace( /^\[/, '' ).replace( /\]$/, '' ), | ||
}, | ||
render() { | ||
return ( | ||
<sup> | ||
<a className="note-link" href="#placeholder"> | ||
{ '' } | ||
</a> | ||
</sup> | ||
); | ||
}, | ||
saveFallback( { attributes: { note } } ) { | ||
return `[${ note }]`; | ||
}, | ||
edit( { | ||
isObjectActive, | ||
value, | ||
onChange, | ||
onFocus, | ||
contentRef, | ||
activeObjectAttributes, | ||
} ) { | ||
function onClick() { | ||
const newValue = insertObject( value, { | ||
type: name, | ||
attributes: { | ||
note: '', | ||
}, | ||
} ); | ||
newValue.start = newValue.end - 1; | ||
onChange( newValue ); | ||
onFocus(); | ||
} | ||
|
||
return ( | ||
<> | ||
<RichTextToolbarButton | ||
icon={ formatListNumbered } | ||
title={ title } | ||
onClick={ onClick } | ||
isActive={ isObjectActive } | ||
/> | ||
{ isObjectActive && ( | ||
<InlineUI | ||
value={ value } | ||
onChange={ onChange } | ||
activeObjectAttributes={ activeObjectAttributes } | ||
contentRef={ contentRef } | ||
/> | ||
) } | ||
</> | ||
); | ||
}, | ||
}; | ||
|
||
function InlineUI( { value, onChange, activeObjectAttributes, contentRef } ) { | ||
const { note } = activeObjectAttributes; | ||
const popoverAnchor = useAnchor( { | ||
editableContentElement: contentRef.current, | ||
settings: footnote, | ||
} ); | ||
|
||
return ( | ||
<Popover | ||
placement="bottom" | ||
focusOnMount={ true } | ||
anchor={ popoverAnchor } | ||
className="block-editor-format-toolbar__image-popover" | ||
> | ||
<TextControl | ||
className="block-editor-format-toolbar__image-container-value" | ||
label={ __( 'Note' ) } | ||
value={ note } | ||
onChange={ ( newNote ) => { | ||
const newReplacements = value.replacements.slice(); | ||
|
||
newReplacements[ value.start ] = { | ||
tagName: 'data', | ||
type: name, | ||
attributes: { | ||
...activeObjectAttributes, | ||
note: newNote, | ||
}, | ||
}; | ||
|
||
onChange( { | ||
...value, | ||
replacements: newReplacements, | ||
} ); | ||
} } | ||
/> | ||
</Popover> | ||
); | ||
} |
This file contains 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,12 @@ | ||
.editor-styles-wrapper { | ||
counter-reset: footnotes; | ||
} | ||
|
||
.note-link { | ||
counter-increment: footnotes; | ||
pointer-events: none; | ||
} | ||
|
||
.note-link::after { | ||
content: "[" counter(footnotes) "]"; | ||
} |
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.
Musing a bit here to explore ways this can be done with the HTML API. This is me playing with the idea and seeing how this need might be taken care of and lead the HTML API design…
This first one uses the Tag Processor in a way that's tightly coupled to internal details. It's not the way we want people to use the Tag Processor, but it's technically possible with what's in Core.
The HTML processor should end up making this far less noisy and remove the internal coupling, but isn't available yet. I think that the code above will work today.
And yesterday while exploring some unrelated bits I thought of an idea to use today's Tag Processor to generate the template in easier way, utilizing the funky HTML placeholder syntax I realized is possible through abuse of the "tag closer with invalid tag name is a comment" rule.