-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Rewire patterns plugin #1674
Rewire patterns plugin #1674
Conversation
blocks/editable/patterns.js
Outdated
* @return {String} Escaped characters | ||
*/ | ||
function escapeRegExp( string ) { | ||
return string.replace( /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&' ); |
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.
you may find lodash.escapeRegExp
incredibly handy. I sure have
Is this only for the TinyMCE blocks? I though that part of what we were getting away from is these awkward patterns. Like, if you want code, make a code block… Can we write text that starts a line with |
Why are they awkward? Regarding code, that one is inline, but I think also the lists are incredibly helpful. |
Well just because they aren't expected from the perspective of editing text. If you do this in Word or Google Docs or Pages or what-not they won't transform. It's just a question of whether we carry these forward or encourage the new semantics. For example, what is the difference between an implicit list in the text block vs an explicit list block? Or, are we converting all these into separate blocks as soon as we type them? |
They are converted into proper blocks. Google Docs, Pages, etc. are also all doing this. |
I'm not sure if I understand your concern on a semantic level. |
Dropbox Paper has a nice implementation of these as well and they're pretty nice to use. |
@dmsnell This PR is one of the reasons I was hoping to redo the editor state, so implementing these patterns would be easier and cleaner (without digging in the DOM). Mentioned in #703 (comment), #847 and #771. |
blocks/editable/patterns.js
Outdated
]; | ||
const [ enterPatterns, spacePatterns ] = partition( patterns, ( { regExp } ) => | ||
regExp.source.endsWith( '$' ) | ||
); |
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.
nice!
although it's awkward to me splitting the function like this. was it too long for one line? I rarely split the arguments ( { regExp } )
from the function body via newline but that's personal style
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.
I guess I should add {}
. :)
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.
it was more about splitting the function up when it's only a single line. all personal style
… = partition(
patterns,
( { regExp } ) => regExp.source.endsWith( '$' )
);
No more will I speak of this 😄
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.
I have no strong opinions on style. More one or two but... :)
8dd67ac
to
22c1815
Compare
Changes Unknown when pulling 22c1815 on try/editable-patterns into ** on master**. |
@coveralls Could you elaborate? |
@felixarntz We can easily add the heading shortcuts here. The rest that is in core has already been added. |
const transformsFrom = get( blockType, 'transforms.from', [] ); | ||
const transforms = transformsFrom.filter( ( { type } ) => type === 'pattern' ); | ||
return [ ...acc, ...transforms ]; | ||
}, [] ); |
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.
nice use of a reducer here!
22c1815
to
3215b68
Compare
Not sure how I should bring over the tests from PhantomJS + QUnit here. Any ideas? Core test file: https://github.com/WordPress/wordpress-develop/blob/master/tests/qunit/wp-includes/js/tinymce/plugins/wptextpattern/plugin.js |
@nylen What are your thoughts on the tests? I think we should either go ahead and merge this to move forward, or try to add the QUnit tests and manually run them for this. |
44d9d35
to
e214fe2
Compare
Let’s merge and iterate. ✨ |
I've been exploring using https://webdriver.io/ to write some integration tests, and I have the basic structure working. This is a much heavier approach than core's existing QUnit tests, but it has the benefit of being able to test block splitting, focus behavior, etc as well as simply the patterns and selection features within a |
That sounds good to me, if it's abstracted and reusable. Do you think this will slow the tests down a lot? Should we run it only when certain files change? In any case, let's explore in a separate PR. |
Yes, they are slower than the rest of our tests. We should probably only run them against master. |
|
||
const [ enterPatterns, spacePatterns ] = partition( | ||
patterns, | ||
( { regExp } ) => regExp.source.endsWith( '$' ), |
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.
ES2015 string prototypes methods aren't polyfilled, so this will error in IE11.
See also: #746
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.
Thanks 🙈
} | ||
|
||
const block = pattern.transform( { | ||
content: [ remainingText, ...drop( content ) ], |
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.
I'm trying to track down what this transform
function is meant to accept as an argument. Is the content
object intended to be an array of two members, the first entry the text following the matching and the second entry the matched substring? Is it then expected that any pattern must match only at the start of a string? Where is this documented?
This PR aims to bring the core patterns plugin to Gutenberg. It's still a WIP, but some patterns already work:
*
. You get a list.*
to a text block. It will be converted into a list.`something`
and it will be wrapped in<code>
.`
and it will be wrapped in<code>
.