fix: prevent asset conflicts between React and Grid.js versions

Add coexistence checks to all enqueue methods to prevent loading
both React and Grid.js assets simultaneously.

Changes:
- ReactAdmin.php: Only enqueue React assets when ?react=1
- Init.php: Skip Grid.js when React active on admin pages
- Form.php, Coupon.php, Access.php: Restore classic assets when ?react=0
- Customer.php, Product.php, License.php: Add coexistence checks

Now the toggle between Classic and React versions works correctly.

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
dwindown
2026-04-18 17:02:14 +07:00
parent bd9cdac02e
commit e8fbfb14c1
74973 changed files with 6658406 additions and 71 deletions

229
node_modules/@wordpress/rich-text/CHANGELOG.md generated vendored Normal file
View File

@@ -0,0 +1,229 @@
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. -->
## Unreleased
## 6.35.0 (2024-05-16)
## 6.34.0 (2024-05-02)
## 6.33.0 (2024-04-19)
## 6.32.0 (2024-04-03)
## 6.31.0 (2024-03-21)
## 6.30.0 (2024-03-06)
## 6.29.0 (2024-02-21)
## 6.28.0 (2024-02-09)
## 6.27.0 (2024-01-24)
## 6.26.0 (2024-01-10)
## 6.25.0 (2023-12-13)
## 6.24.0 (2023-11-29)
## 6.23.0 (2023-11-16)
## 6.22.0 (2023-11-02)
## 6.21.0 (2023-10-18)
## 6.20.0 (2023-10-05)
## 6.19.0 (2023-09-20)
## 6.18.0 (2023-08-31)
## 6.17.0 (2023-08-16)
## 6.16.0 (2023-08-10)
## 6.15.0 (2023-07-20)
## 6.14.0 (2023-07-05)
## 6.13.0 (2023-06-23)
## 6.12.0 (2023-06-07)
## 6.11.0 (2023-05-24)
## 6.10.0 (2023-05-10)
## 6.9.0 (2023-04-26)
## 6.8.0 (2023-04-12)
## 6.7.0 (2023-03-29)
## 6.6.0 (2023-03-15)
## 6.5.0 (2023-03-01)
## 6.4.0 (2023-02-15)
## 6.3.0 (2023-02-01)
## 6.2.0 (2023-01-11)
## 6.1.0 (2023-01-02)
## 6.0.0 (2022-12-14)
### Breaking Changes
- Updated dependencies to require React 18 ([45235](https://github.com/WordPress/gutenberg/pull/45235))
## 5.20.0 (2022-11-16)
## 5.19.0 (2022-11-02)
### Deprecations
- Update deprecation message for the `useAnchorRef` hook ([#45195](https://github.com/WordPress/gutenberg/pull/45195)).
## 5.18.0 (2022-10-19)
## 5.17.0 (2022-10-05)
## 5.16.0 (2022-09-21)
### Deprecations
- Introduced new `useAnchor` hook, which works better with the new `Popover` component APIs. The previous `useAnchorRef` hook is now marked as deprecated, and is scheduled to be removed in WordPress 6.3 ([#43691](https://github.com/WordPress/gutenberg/pull/43691)).
## 5.15.0 (2022-09-13)
## 5.14.0 (2022-08-24)
## 5.13.0 (2022-08-10)
## 5.12.0 (2022-07-27)
## 5.11.0 (2022-07-13)
## 5.10.0 (2022-06-29)
## 5.9.0 (2022-06-15)
## 5.8.0 (2022-06-01)
## 5.7.0 (2022-05-18)
## 5.6.0 (2022-05-04)
## 5.5.0 (2022-04-21)
## 5.4.0 (2022-04-08)
## 5.3.0 (2022-03-23)
## 5.2.0 (2022-03-11)
## 5.1.1 (2022-02-10)
### Bug Fix
- Removed unused `@wordpress/dom`, `@wordpress/is-shallow-equal` and `classnames` dependencies ([#38388](https://github.com/WordPress/gutenberg/pull/38388)).
## 5.1.0 (2022-01-27)
## 5.0.0 (2021-07-29)
### Breaking Change
- Upgraded React components to work with v17.0 ([#29118](https://github.com/WordPress/gutenberg/pull/29118)). There are no new features in React v17.0 as explained in the [blog post](https://reactjs.org/blog/2020/10/20/react-v17.html).
## 4.2.0 (2021-07-21)
## 4.1.0 (2021-05-20)
## 4.0.0 (2021-05-14)
### Breaking Changes
- Drop support for Internet Explorer 11 ([#31110](https://github.com/WordPress/gutenberg/pull/31110)). Learn more at https://make.wordpress.org/core/2021/04/22/ie-11-support-phase-out-plan/.
- Increase the minimum Node.js version to v12 matching Long Term Support releases ([#31270](https://github.com/WordPress/gutenberg/pull/31270)). Learn more at https://nodejs.org/en/about/releases/.
## 3.25.0 (2021-03-17)
## 3.24.0 (2020-12-17)
### New Feature
- Added a store definition `store` for the rich-text namespace to use with `@wordpress/data` API ([#26655](https://github.com/WordPress/gutenberg/pull/26655)).
## 3.3.0 (2019-05-21)
### Internal
- Removed and renamed undocumented functions and constants:
- Removed `charAt`
- Removed `getSelectionStart`
- Removed `getSelectionEnd`
- Removed `insertLineBreak`
- Renamed `isEmptyLine` to `__unstableIsEmptyLine`
- Renamed `insertLineSeparator` to `__unstableInsertLineSeparator`
- Renamed `apply` to `__unstableApply`
- Renamed `unstableToDom` to `__unstableToDom`
- Renamed `LINE_SEPARATOR` to `__UNSTABLE_LINE_SEPARATOR`
- Renamed `indentListItems` to `__unstableIndentListItems`
- Renamed `outdentListItems` to `__unstableOutdentListItems`
- Renamed `changeListType` to `__unstableChangeListType`
## 3.1.0 (2019-03-06)
### Enhancement
- Added format boundaries.
- Removed parameters from `create` to filter out content.
- Removed the `createLinePadding` from `apply`, which is now built in.
- Improved format placeholder.
- Improved dom diffing.
## 3.0.4 (2019-01-03)
## 3.0.3 (2018-12-12)
### Internal
- Internal performance optimizations to avoid excessive expensive creation of DOM documents.
## 3.0.2 (2018-11-21)
## 3.0.1 (2018-11-20)
## 3.0.0 (2018-11-15)
### Breaking Changes
- `toHTMLString` always expects an object instead of multiple arguments.
## 2.0.4 (2018-11-09)
## 2.0.3 (2018-11-09)
### Bug Fix
- Fix Format Type Assignment During Parsing.
- Fix applying formats on multiline values without wrapper tags.
## 2.0.2 (2018-11-03)
## 2.0.1 (2018-10-30)
## 2.0.0 (2018-10-30)
- Remove `@wordpress/blocks` as a dependency.
## 1.0.2 (2018-10-29)
## 1.0.1 (2018-10-19)
## 1.0.0 (2018-10-18)
- Initial release.

788
node_modules/@wordpress/rich-text/LICENSE.md generated vendored Normal file
View File

@@ -0,0 +1,788 @@
## Gutenberg
Copyright 2016-2024 by the contributors
**License for Contributions (on and after April 15, 2021)**
All code contributed to the Gutenberg project is dual-licensed,
and released under both of the following licenses:
the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License or (at your option) any later version (the “GPL”)
and the Mozilla Public License, Version 2.0 (the “MPL”).
**Project License**
The Gutenberg project license is not affected by the License for Contributions (as
discussed in the [Dual License section](#dual-license) below). The Gutenberg project
continues to be free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License or (at your option) any
later version (the “GPL”).
This program incorporates work covered by the following copyright and
permission notices:
b2 is (c) 2001, 2002 Michel Valdrighi - m@tidakada.com -
http://tidakada.com
Wherever third party code has been used, credit has been given in the code's
comments.
b2 is released under the GPL
and
WordPress - Web publishing software
Copyright 2003-2010 by the contributors
WordPress is released under the GPL
### Dual License
**We are currently in the process of changing Gutenbergs software license from
GPL to a dual license: GPL and MPL.**
**This process involves two independent steps (1) obtaining permission for dual
licensing from contributors of already contributed Gutenberg code and (2)
dual licensing of all contributions to Gutenberg that are made on or after
April 15, 2021.**
**For part (1): Were reaching out to everyone who has contributed code, prior
to April 15, 2021, and asking that they agree to dual license their
contribution to the project. We expect this process to be completed by
mid-year, 2021.**
**For part (2): We have changed the license for contributed code to this
repository to make clear that all contributions on or after April 15, 2021
will be made under the dual-license.**
**When we have received all necessary rights and permissions to dual license
the pre-April 15, 2021 code of the Gutenberg project (Part 1 above), you will
have the option to use and distribute all of the Gutenberg project under
either the GPL or MPL license. At this time we will change the “Project
License” to the following:**
The Gutenberg project is free software; you can redistribute it and/or modify
it under the terms of either of the following licenses:
1. the GNU General Public License as published by the Free Software Foundation;
either version 2 of the License or (at your option) any later version (the
“GPL”) OR
2. the Mozilla Public License Version 2.0 (the “MPL”).
---
## Full Text of Referenced Licenses
1. [GNU General Public License, Version 2](#gnu-general-public-license-version-2)
2. [Mozilla Public License, Version 2.0](#mozilla-public-license-version-20)
## GNU General Public License, Version 2
### GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
### Preamble
The licenses for most software are designed to take away your freedom
to share and change it. By contrast, the GNU General Public License is
intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if
you distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on,
we want its recipients to know that what they have is not the
original, so that any problems introduced by others will not reflect
on the original authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at
all.
The precise terms and conditions for copying, distribution and
modification follow.
### TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
**0.** This License applies to any program or other work which
contains a notice placed by the copyright holder saying it may be
distributed under the terms of this General Public License. The
"Program", below, refers to any such program or work, and a "work
based on the Program" means either the Program or any derivative work
under copyright law: that is to say, a work containing the Program or
a portion of it, either verbatim or with modifications and/or
translated into another language. (Hereinafter, translation is
included without limitation in the term "modification".) Each licensee
is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the Program
(independent of having been made by running the Program). Whether that
is true depends on what the Program does.
**1.** You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a
fee.
**2.** You may modify your copy or copies of the Program or any
portion of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
**a)** You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
**b)** You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any part
thereof, to be licensed as a whole at no charge to all third parties
under the terms of this License.
**c)** If the modified program normally reads commands interactively
when run, you must cause it, when started running for such interactive
use in the most ordinary way, to print or display an announcement
including an appropriate copyright notice and a notice that there is
no warranty (or else, saying that you provide a warranty) and that
users may redistribute the program under these conditions, and telling
the user how to view a copy of this License. (Exception: if the
Program itself is interactive but does not normally print such an
announcement, your work based on the Program is not required to print
an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
**3.** You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
**a)** Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections 1
and 2 above on a medium customarily used for software interchange; or,
**b)** Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your cost of
physically performing source distribution, a complete machine-readable
copy of the corresponding source code, to be distributed under the
terms of Sections 1 and 2 above on a medium customarily used for
software interchange; or,
**c)** Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is allowed
only for noncommercial distribution and only if you received the
program in object code or executable form with such an offer, in
accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
**4.** You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt otherwise
to copy, modify, sublicense or distribute the Program is void, and
will automatically terminate your rights under this License. However,
parties who have received copies, or rights, from you under this
License will not have their licenses terminated so long as such
parties remain in full compliance.
**5.** You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
**6.** Each time you redistribute the Program (or any work based on
the Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
**7.** If, as a consequence of a court judgment or allegation of
patent infringement or for any other reason (not limited to patent
issues), conditions are imposed on you (whether by court order,
agreement or otherwise) that contradict the conditions of this
License, they do not excuse you from the conditions of this License.
If you cannot distribute so as to satisfy simultaneously your
obligations under this License and any other pertinent obligations,
then as a consequence you may not distribute the Program at all. For
example, if a patent license would not permit royalty-free
redistribution of the Program by all those who receive copies directly
or indirectly through you, then the only way you could satisfy both it
and this License would be to refrain entirely from distribution of the
Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
**8.** If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
**9.** The Free Software Foundation may publish revised and/or new
versions of the General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Program does not specify a
version number of this License, you may choose any version ever
published by the Free Software Foundation.
**10.** If you wish to incorporate parts of the Program into other
free programs whose distribution conditions are different, write to
the author to ask for permission. For software which is copyrighted by
the Free Software Foundation, write to the Free Software Foundation;
we sometimes make exceptions for this. Our decision will be guided by
the two goals of preserving the free status of all derivatives of our
free software and of promoting the sharing and reuse of software
generally.
**NO WARRANTY**
**11.** BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
**12.** IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
### END OF TERMS AND CONDITIONS
### How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these
terms.
To do so, attach the following notices to the program. It is safest to
attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
one line to give the program's name and an idea of what it does.
Copyright (C) yyyy name of author
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Also add information on how to contact you by electronic and paper
mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details
type `show w'. This is free software, and you are welcome
to redistribute it under certain conditions; type `show c'
for details.
The hypothetical commands \`show w' and \`show c' should show the
appropriate parts of the General Public License. Of course, the
commands you use may be called something other than \`show w' and
\`show c'; they could even be mouse-clicks or menu items--whatever
suits your program.
You should also get your employer (if you work as a programmer) or
your school, if any, to sign a "copyright disclaimer" for the program,
if necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright
interest in the program `Gnomovision'
(which makes passes at compilers) written
by James Hacker.
signature of Ty Coon, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library,
you may consider it more useful to permit linking proprietary
applications with the library. If this is what you want to do, use the
[GNU Lesser General Public
License](http://www.gnu.org/licenses/lgpl.html) instead of this
License.
---
## Mozilla Public License, Version 2.0
### 1. Definitions
**1.1. “Contributor”**
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
**1.2. “Contributor Version”**
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
**1.3. “Contribution”**
means Covered Software of a particular Contributor.
**1.4. “Covered Software”**
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
**1.5. “Incompatible With Secondary Licenses”**
means
- **(a)** that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
- **(b)** that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
**1.6. “Executable Form”**
means any form of the work other than Source Code Form.
**1.7. “Larger Work”**
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
**1.8. “License”**
means this document.
**1.9. “Licensable”**
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
**1.10. “Modifications”**
means any of the following:
- **(a)** any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
- **(b)** any new file in Source Code Form that contains any Covered
Software.
**1.11. “Patent Claims” of a Contributor**
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
**1.12. “Secondary License”**
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
**1.13. “Source Code Form”**
means the form of the work preferred for making modifications.
**1.14. “You” (or “Your”)**
means an individual or a legal entity exercising rights under this
License. For legal entities, “You” includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, “control” means **(a)** the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or **(b)** ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
### 2. License Grants and Conditions
#### 2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
- **(a)** under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
- **(b)** under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
#### 2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
#### 2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
- **(a)** for any code that a Contributor has removed from Covered Software;
or
- **(b)** for infringements caused by: **(i)** Your and any other third party's
modifications of Covered Software, or **(ii)** the combination of its
Contributions with other software (except as part of its Contributor
Version); or
- **(c)** under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
#### 2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
#### 2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
#### 2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
#### 2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
### 3. Responsibilities
#### 3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
#### 3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
- **(a)** such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
- **(b)** You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
#### 3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
#### 3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
#### 3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
### 4. Inability to Comply Due to Statute or Regulation
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: **(a)** comply with
the terms of this License to the maximum extent possible; and **(b)**
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
### 5. Termination
**5.1.** The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated **(a)** provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and **(b)** on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
**5.2.** If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
**5.3.** In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
### 6. Disclaimer of Warranty
> Covered Software is provided under this License on an “as is”
> basis, without warranty of any kind, either expressed, implied, or
> statutory, including, without limitation, warranties that the
> Covered Software is free of defects, merchantable, fit for a
> particular purpose or non-infringing. The entire risk as to the
> quality and performance of the Covered Software is with You.
> Should any Covered Software prove defective in any respect, You
> (not any Contributor) assume the cost of any necessary servicing,
> repair, or correction. This disclaimer of warranty constitutes an
> essential part of this License. No use of any Covered Software is
> authorized under this License except under this disclaimer.
### 7. Limitation of Liability
> Under no circumstances and under no legal theory, whether tort
> (including negligence), contract, or otherwise, shall any
> Contributor, or anyone who distributes Covered Software as
> permitted above, be liable to You for any direct, indirect,
> special, incidental, or consequential damages of any character
> including, without limitation, damages for lost profits, loss of
> goodwill, work stoppage, computer failure or malfunction, or any
> and all other commercial damages or losses, even if such party
> shall have been informed of the possibility of such damages. This
> limitation of liability shall not apply to liability for death or
> personal injury resulting from such party's negligence to the
> extent applicable law prohibits such limitation. Some
> jurisdictions do not allow the exclusion or limitation of
> incidental or consequential damages, so this exclusion and
> limitation may not apply to You.
### 8. Litigation
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
### 9. Miscellaneous
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
### 10. Versions of the License
#### 10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
#### 10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
#### 10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
#### 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
## Exhibit A - Source Code Form License Notice
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
## Exhibit B - “Incompatible With Secondary Licenses” Notice
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.

490
node_modules/@wordpress/rich-text/README.md generated vendored Normal file
View File

@@ -0,0 +1,490 @@
# Rich Text
This module contains helper functions to convert HTML or a DOM tree into a rich text value and back, and to modify the value with functions that are similar to `String` methods, plus some additional ones for formatting.
## Installation
Install the module
```bash
npm install @wordpress/rich-text
```
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
## Usage
The Rich Text package is designed to aid in the manipulation of plain text strings in order that they can represent complex formatting.
By using a `RichTextValue` value object (referred to from here on as `value`) it is possible to separate text from formatting, thereby affording the ability to easily search and manipulate rich formats.
Examples of rich formats include:
- bold, italic, superscript (etc)
- links
- unordered/ordered lists
### The RichTextValue object
The value object is comprised of the following:
- `text` - the string of text to which rich formats are to be applied.
- `formats` - a sparse array of the same length as `text` that is filled with [formats](https://developer.wordpress.org/block-editor/how-to-guides/format-api/) (e.g. `core/link`, `core/bold` etc.) at the positions where the text is formatted.
- `start` - an index in the `text` representing the _start_ of the currently active selection.
- `end` - an index in the `text` representing the _end_ of the currently active selection.
You should not attempt to create your own `value` objects. Rather you should rely on the built in methods of the `@wordpress/rich-text` package to build these for you.
It is important to understand how a value represents richly formatted text. Here is an example to illustrate.
If `text` is formatted from position 2-5 in bold (`core/bold`) and from position 2-8 with a link (`core/link`), then you'll find:
- arrays within the sparse array at positions 2-5 that include the `core/bold` format
- arrays within the sparse array at positions 2-8 that include the `core/link` format
Here's how that would look:
```js
{
text: 'Hello world', // length 11
formats: [
[], // 0
[],
[ // 2
{
type: 'core/bold',
},
{
type: 'core/link',
}
],
[
{
type: 'core/bold',
},
{
type: 'core/link',
}
],
[
{
type: 'core/bold',
},
{
type: 'core/link',
}
],
[
{
type: 'core/bold',
},
{
type: 'core/link',
}
],
[ // 6
{
type: 'core/link',
}
]
[
{
type: 'core/link',
}
],
[
{
type: 'core/link',
}
],
[], // 9
[], // 10
[], // 11
]
}
```
### Selections
Let's continue to consider the above example with the text `Hello world`.
If, as a user, I make a selection of the word `Hello` this would result in a value object with `start` and `end` as `0` and `5` respectively.
In general, this is useful for knowing which portion of the text is selected. However, we need to consider that selections may also be "collapsed".
#### Collapsed selections
A collapsed selection is one where `start` and `end` values are _identical_ (e.g. `start: 4, end: 4`). This happens when no characters are selected, but there is a caret present. This most often occurs when a user places the cursor/caret within a string of text but does not make a selection.
Given that the selection has no "range" (i.e. there is no difference between `start` and `end` indices), finding the currently selected portion of text from collapsed values can be challenging.
## API
<!-- START TOKEN(Autogenerated API docs) -->
### applyFormat
Apply a format object to a Rich Text value from the given `startIndex` to the given `endIndex`. Indices are retrieved from the selection if none are provided.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _format_ `RichTextFormat`: Format to apply.
- _startIndex_ `[number]`: Start index.
- _endIndex_ `[number]`: End index.
_Returns_
- `RichTextValue`: A new value with the format applied.
### concat
Combine all Rich Text values into one. This is similar to `String.prototype.concat`.
_Parameters_
- _values_ `...RichTextValue`: Objects to combine.
_Returns_
- `RichTextValue`: A new value combining all given records.
### create
Create a RichText value from an `Element` tree (DOM), an HTML string or a plain text string, with optionally a `Range` object to set the selection. If called without any input, an empty value will be created. The optional functions can be used to filter out content.
A value will have the following shape, which you are strongly encouraged not to modify without the use of helper functions:
```js
{
text: string,
formats: Array,
replacements: Array,
?start: number,
?end: number,
}
```
As you can see, text and formatting are separated. `text` holds the text, including any replacement characters for objects and lines. `formats`, `objects` and `lines` are all sparse arrays of the same length as `text`. It holds information about the formatting at the relevant text indices. Finally `start` and `end` state which text indices are selected. They are only provided if a `Range` was given.
_Parameters_
- _$1_ `[Object]`: Optional named arguments.
- _$1.element_ `[Element]`: Element to create value from.
- _$1.text_ `[string]`: Text to create value from.
- _$1.html_ `[string]`: HTML to create value from.
- _$1.range_ `[Range]`: Range to create value from.
- _$1.\_\_unstableIsEditableTree_ `[boolean]`:
_Returns_
- `RichTextValue`: A rich text value.
### getActiveFormat
Gets the format object by type at the start of the selection. This can be used to get e.g. the URL of a link format at the current selection, but also to check if a format is active at the selection. Returns undefined if there is no format at the selection.
_Parameters_
- _value_ `RichTextValue`: Value to inspect.
- _formatType_ `string`: Format type to look for.
_Returns_
- `RichTextFormat|undefined`: Active format object of the specified type, or undefined.
### getActiveFormats
Gets the all format objects at the start of the selection.
_Parameters_
- _value_ `RichTextValue`: Value to inspect.
- _EMPTY_ACTIVE_FORMATS_ `Array`: Array to return if there are no active formats.
_Returns_
- `RichTextFormatList`: Active format objects.
### getActiveObject
Gets the active object, if there is any.
_Parameters_
- _value_ `RichTextValue`: Value to inspect.
_Returns_
- `RichTextFormat|void`: Active object, or undefined.
### getTextContent
Get the textual content of a Rich Text value. This is similar to `Element.textContent`.
_Parameters_
- _value_ `RichTextValue`: Value to use.
_Returns_
- `string`: The text content.
### insert
Insert a Rich Text value, an HTML string, or a plain text string, into a Rich Text value at the given `startIndex`. Any content between `startIndex` and `endIndex` will be removed. Indices are retrieved from the selection if none are provided.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _valueToInsert_ `RichTextValue|string`: Value to insert.
- _startIndex_ `[number]`: Start index.
- _endIndex_ `[number]`: End index.
_Returns_
- `RichTextValue`: A new value with the value inserted.
### insertObject
Insert a format as an object into a Rich Text value at the given `startIndex`. Any content between `startIndex` and `endIndex` will be removed. Indices are retrieved from the selection if none are provided.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _formatToInsert_ `RichTextFormat`: Format to insert as object.
- _startIndex_ `[number]`: Start index.
- _endIndex_ `[number]`: End index.
_Returns_
- `RichTextValue`: A new value with the object inserted.
### isCollapsed
Check if the selection of a Rich Text value is collapsed or not. Collapsed means that no characters are selected, but there is a caret present. If there is no selection, `undefined` will be returned. This is similar to `window.getSelection().isCollapsed()`.
_Parameters_
- _props_ `RichTextValue`: The rich text value to check.
- _props.start_ `RichTextValue[ 'start' ]`:
- _props.end_ `RichTextValue[ 'end' ]`:
_Returns_
- `boolean | undefined`: True if the selection is collapsed, false if not, undefined if there is no selection.
### isEmpty
Check if a Rich Text value is Empty, meaning it contains no text or any objects (such as images).
_Parameters_
- _value_ `RichTextValue`: Value to use.
_Returns_
- `boolean`: True if the value is empty, false if not.
### join
Combine an array of Rich Text values into one, optionally separated by `separator`, which can be a Rich Text value, HTML string, or plain text string. This is similar to `Array.prototype.join`.
_Parameters_
- _values_ `Array<RichTextValue>`: An array of values to join.
- _separator_ `[string|RichTextValue]`: Separator string or value.
_Returns_
- `RichTextValue`: A new combined value.
### registerFormatType
Registers a new format provided a unique name and an object defining its behavior.
_Parameters_
- _name_ `string`: Format name.
- _settings_ `WPFormat`: Format settings.
_Returns_
- `WPFormat|undefined`: The format, if it has been successfully registered; otherwise `undefined`.
### remove
Remove content from a Rich Text value between the given `startIndex` and `endIndex`. Indices are retrieved from the selection if none are provided.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _startIndex_ `[number]`: Start index.
- _endIndex_ `[number]`: End index.
_Returns_
- `RichTextValue`: A new value with the content removed.
### removeFormat
Remove any format object from a Rich Text value by type from the given `startIndex` to the given `endIndex`. Indices are retrieved from the selection if none are provided.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _formatType_ `string`: Format type to remove.
- _startIndex_ `[number]`: Start index.
- _endIndex_ `[number]`: End index.
_Returns_
- `RichTextValue`: A new value with the format applied.
### replace
Search a Rich Text value and replace the match(es) with `replacement`. This is similar to `String.prototype.replace`.
_Parameters_
- _value_ `RichTextValue`: The value to modify.
- _pattern_ `RegExp|string`: A RegExp object or literal. Can also be a string. It is treated as a verbatim string and is not interpreted as a regular expression. Only the first occurrence will be replaced.
- _replacement_ `Function|string`: The match or matches are replaced with the specified or the value returned by the specified function.
_Returns_
- `RichTextValue`: A new value with replacements applied.
### RichTextData
The RichTextData class is used to instantiate a wrapper around rich text values, with methods that can be used to transform or manipulate the data.
- Create an empty instance: `new RichTextData()`.
- Create one from an HTML string: `RichTextData.fromHTMLString(
'<em>hello</em>' )`.
- Create one from a wrapper HTMLElement: `RichTextData.fromHTMLElement(
document.querySelector( 'p' ) )`.
- Create one from plain text: `RichTextData.fromPlainText( '1\n2' )`.
- Create one from a rich text value: `new RichTextData( { text: '...',
formats: [ ... ] } )`.
### RichTextValue
An object which represents a formatted string. See main `@wordpress/rich-text` documentation for more information.
### slice
Slice a Rich Text value from `startIndex` to `endIndex`. Indices are retrieved from the selection if none are provided. This is similar to `String.prototype.slice`.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _startIndex_ `[number]`: Start index.
- _endIndex_ `[number]`: End index.
_Returns_
- `RichTextValue`: A new extracted value.
### split
Split a Rich Text value in two at the given `startIndex` and `endIndex`, or split at the given separator. This is similar to `String.prototype.split`. Indices are retrieved from the selection if none are provided.
_Parameters_
- _value_ `RichTextValue`:
- _string_ `[number|string]`: Start index, or string at which to split.
_Returns_
- `Array<RichTextValue>|undefined`: An array of new values.
### store
Store definition for the rich-text namespace.
_Related_
- <https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore>
_Type_
- `Object`
### toggleFormat
Toggles a format object to a Rich Text value at the current selection.
_Parameters_
- _value_ `RichTextValue`: Value to modify.
- _format_ `RichTextFormat`: Format to apply or remove.
_Returns_
- `RichTextValue`: A new value with the format applied or removed.
### toHTMLString
Create an HTML string from a Rich Text value.
_Parameters_
- _$1_ `Object`: Named argements.
- _$1.value_ `RichTextValue`: Rich text value.
- _$1.preserveWhiteSpace_ `[boolean]`: Preserves newlines if true.
_Returns_
- `string`: HTML string.
### unregisterFormatType
Unregisters a format.
_Parameters_
- _name_ `string`: Format name.
_Returns_
- `WPFormat|undefined`: The previous format value, if it has been successfully unregistered; otherwise `undefined`.
### useAnchor
This hook, to be used in a format type's Edit component, returns the active element that is formatted, or a virtual element for the selection range if no format is active. The returned value is meant to be used for positioning UI, e.g. by passing it to the `Popover` component via the `anchor` prop.
_Parameters_
- _$1_ `Object`: Named parameters.
- _$1.editableContentElement_ `HTMLElement|null`: The element containing the editable content.
- _$1.settings_ `WPFormat=`: The format type's settings.
_Returns_
- `Element|VirtualAnchorElement|undefined|null`: The active element or selection range.
### useAnchorRef
This hook, to be used in a format type's Edit component, returns the active element that is formatted, or the selection range if no format is active. The returned value is meant to be used for positioning UI, e.g. by passing it to the `Popover` component.
_Parameters_
- _$1_ `Object`: Named parameters.
- _$1.ref_ `RefObject<HTMLElement>`: React ref of the element containing the editable content.
- _$1.value_ `RichTextValue`: Value to check for selection.
- _$1.settings_ `WPFormat`: The format type's settings.
_Returns_
- `Element|Range`: The active element or selection range.
<!-- END TOKEN(Autogenerated API docs) -->
## Contributing to this package
This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
<br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>

View File

@@ -0,0 +1,87 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
function replace(array, index, value) {
array = array.slice();
array[index] = value;
return array;
}
/**
* Apply a format object to a Rich Text value from the given `startIndex` to the
* given `endIndex`. Indices are retrieved from the selection if none are
* provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} format Format to apply.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the format applied.
*/
export function applyFormat(value, format, startIndex = value.start, endIndex = value.end) {
const {
formats,
activeFormats
} = value;
const newFormats = formats.slice();
// The selection is collapsed.
if (startIndex === endIndex) {
const startFormat = newFormats[startIndex]?.find(({
type
}) => type === format.type);
// If the caret is at a format of the same type, expand start and end to
// the edges of the format. This is useful to apply new attributes.
if (startFormat) {
const index = newFormats[startIndex].indexOf(startFormat);
while (newFormats[startIndex] && newFormats[startIndex][index] === startFormat) {
newFormats[startIndex] = replace(newFormats[startIndex], index, format);
startIndex--;
}
endIndex++;
while (newFormats[endIndex] && newFormats[endIndex][index] === startFormat) {
newFormats[endIndex] = replace(newFormats[endIndex], index, format);
endIndex++;
}
}
} else {
// Determine the highest position the new format can be inserted at.
let position = +Infinity;
for (let index = startIndex; index < endIndex; index++) {
if (newFormats[index]) {
newFormats[index] = newFormats[index].filter(({
type
}) => type !== format.type);
const length = newFormats[index].length;
if (length < position) {
position = length;
}
} else {
newFormats[index] = [];
position = 0;
}
}
for (let index = startIndex; index < endIndex; index++) {
newFormats[index].splice(position, 0, format);
}
}
return normaliseFormats({
...value,
formats: newFormats,
// Always revise active formats. This serves as a placeholder for new
// inputs with the format so new input appears with the format applied,
// and ensures a format of the same type uses the latest values.
activeFormats: [...(activeFormats?.filter(({
type
}) => type !== format.type) || []), format]
});
}
//# sourceMappingURL=apply-format.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,42 @@
/**
* Internal dependencies
*/
import { toHTMLString } from '../../to-html-string';
import { isCollapsed } from '../../is-collapsed';
import { slice } from '../../slice';
import { getTextContent } from '../../get-text-content';
export default (props => element => {
function onCopy(event) {
const {
record
} = props.current;
const {
ownerDocument
} = element;
if (isCollapsed(record.current) || !element.contains(ownerDocument.activeElement)) {
return;
}
const selectedRecord = slice(record.current);
const plainText = getTextContent(selectedRecord);
const html = toHTMLString({
value: selectedRecord
});
event.clipboardData.setData('text/plain', plainText);
event.clipboardData.setData('text/html', html);
event.clipboardData.setData('rich-text', 'true');
event.preventDefault();
if (event.type === 'cut') {
ownerDocument.execCommand('delete');
}
}
const {
defaultView
} = element.ownerDocument;
defaultView.addEventListener('copy', onCopy);
defaultView.addEventListener('cut', onCopy);
return () => {
defaultView.removeEventListener('copy', onCopy);
defaultView.removeEventListener('cut', onCopy);
};
});
//# sourceMappingURL=copy-handler.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["toHTMLString","isCollapsed","slice","getTextContent","props","element","onCopy","event","record","current","ownerDocument","contains","activeElement","selectedRecord","plainText","html","value","clipboardData","setData","preventDefault","type","execCommand","defaultView","addEventListener","removeEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/copy-handler.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { toHTMLString } from '../../to-html-string';\nimport { isCollapsed } from '../../is-collapsed';\nimport { slice } from '../../slice';\nimport { getTextContent } from '../../get-text-content';\n\nexport default ( props ) => ( element ) => {\n\tfunction onCopy( event ) {\n\t\tconst { record } = props.current;\n\t\tconst { ownerDocument } = element;\n\t\tif (\n\t\t\tisCollapsed( record.current ) ||\n\t\t\t! element.contains( ownerDocument.activeElement )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst selectedRecord = slice( record.current );\n\t\tconst plainText = getTextContent( selectedRecord );\n\t\tconst html = toHTMLString( { value: selectedRecord } );\n\t\tevent.clipboardData.setData( 'text/plain', plainText );\n\t\tevent.clipboardData.setData( 'text/html', html );\n\t\tevent.clipboardData.setData( 'rich-text', 'true' );\n\t\tevent.preventDefault();\n\n\t\tif ( event.type === 'cut' ) {\n\t\t\townerDocument.execCommand( 'delete' );\n\t\t}\n\t}\n\n\tconst { defaultView } = element.ownerDocument;\n\n\tdefaultView.addEventListener( 'copy', onCopy );\n\tdefaultView.addEventListener( 'cut', onCopy );\n\treturn () => {\n\t\tdefaultView.removeEventListener( 'copy', onCopy );\n\t\tdefaultView.removeEventListener( 'cut', onCopy );\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,sBAAsB;AACnD,SAASC,WAAW,QAAQ,oBAAoB;AAChD,SAASC,KAAK,QAAQ,aAAa;AACnC,SAASC,cAAc,QAAQ,wBAAwB;AAEvD,gBAAiBC,KAAK,IAAQC,OAAO,IAAM;EAC1C,SAASC,MAAMA,CAAEC,KAAK,EAAG;IACxB,MAAM;MAAEC;IAAO,CAAC,GAAGJ,KAAK,CAACK,OAAO;IAChC,MAAM;MAAEC;IAAc,CAAC,GAAGL,OAAO;IACjC,IACCJ,WAAW,CAAEO,MAAM,CAACC,OAAQ,CAAC,IAC7B,CAAEJ,OAAO,CAACM,QAAQ,CAAED,aAAa,CAACE,aAAc,CAAC,EAChD;MACD;IACD;IAEA,MAAMC,cAAc,GAAGX,KAAK,CAAEM,MAAM,CAACC,OAAQ,CAAC;IAC9C,MAAMK,SAAS,GAAGX,cAAc,CAAEU,cAAe,CAAC;IAClD,MAAME,IAAI,GAAGf,YAAY,CAAE;MAAEgB,KAAK,EAAEH;IAAe,CAAE,CAAC;IACtDN,KAAK,CAACU,aAAa,CAACC,OAAO,CAAE,YAAY,EAAEJ,SAAU,CAAC;IACtDP,KAAK,CAACU,aAAa,CAACC,OAAO,CAAE,WAAW,EAAEH,IAAK,CAAC;IAChDR,KAAK,CAACU,aAAa,CAACC,OAAO,CAAE,WAAW,EAAE,MAAO,CAAC;IAClDX,KAAK,CAACY,cAAc,CAAC,CAAC;IAEtB,IAAKZ,KAAK,CAACa,IAAI,KAAK,KAAK,EAAG;MAC3BV,aAAa,CAACW,WAAW,CAAE,QAAS,CAAC;IACtC;EACD;EAEA,MAAM;IAAEC;EAAY,CAAC,GAAGjB,OAAO,CAACK,aAAa;EAE7CY,WAAW,CAACC,gBAAgB,CAAE,MAAM,EAAEjB,MAAO,CAAC;EAC9CgB,WAAW,CAACC,gBAAgB,CAAE,KAAK,EAAEjB,MAAO,CAAC;EAC7C,OAAO,MAAM;IACZgB,WAAW,CAACE,mBAAmB,CAAE,MAAM,EAAElB,MAAO,CAAC;IACjDgB,WAAW,CAACE,mBAAmB,CAAE,KAAK,EAAElB,MAAO,CAAC;EACjD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { BACKSPACE, DELETE } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { remove } from '../../remove';
export default (props => element => {
function onKeyDown(event) {
const {
keyCode
} = event;
const {
createRecord,
handleChange
} = props.current;
if (event.defaultPrevented) {
return;
}
if (keyCode !== DELETE && keyCode !== BACKSPACE) {
return;
}
const currentValue = createRecord();
const {
start,
end,
text
} = currentValue;
// Always handle full content deletion ourselves.
if (start === 0 && end !== 0 && end === text.length) {
handleChange(remove(currentValue));
event.preventDefault();
}
}
element.addEventListener('keydown', onKeyDown);
return () => {
element.removeEventListener('keydown', onKeyDown);
};
});
//# sourceMappingURL=delete.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["BACKSPACE","DELETE","remove","props","element","onKeyDown","event","keyCode","createRecord","handleChange","current","defaultPrevented","currentValue","start","end","text","length","preventDefault","addEventListener","removeEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/delete.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { BACKSPACE, DELETE } from '@wordpress/keycodes';\n\n/**\n * Internal dependencies\n */\nimport { remove } from '../../remove';\n\nexport default ( props ) => ( element ) => {\n\tfunction onKeyDown( event ) {\n\t\tconst { keyCode } = event;\n\t\tconst { createRecord, handleChange } = props.current;\n\n\t\tif ( event.defaultPrevented ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( keyCode !== DELETE && keyCode !== BACKSPACE ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst currentValue = createRecord();\n\t\tconst { start, end, text } = currentValue;\n\n\t\t// Always handle full content deletion ourselves.\n\t\tif ( start === 0 && end !== 0 && end === text.length ) {\n\t\t\thandleChange( remove( currentValue ) );\n\t\t\tevent.preventDefault();\n\t\t}\n\t}\n\n\telement.addEventListener( 'keydown', onKeyDown );\n\treturn () => {\n\t\telement.removeEventListener( 'keydown', onKeyDown );\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,EAAEC,MAAM,QAAQ,qBAAqB;;AAEvD;AACA;AACA;AACA,SAASC,MAAM,QAAQ,cAAc;AAErC,gBAAiBC,KAAK,IAAQC,OAAO,IAAM;EAC1C,SAASC,SAASA,CAAEC,KAAK,EAAG;IAC3B,MAAM;MAAEC;IAAQ,CAAC,GAAGD,KAAK;IACzB,MAAM;MAAEE,YAAY;MAAEC;IAAa,CAAC,GAAGN,KAAK,CAACO,OAAO;IAEpD,IAAKJ,KAAK,CAACK,gBAAgB,EAAG;MAC7B;IACD;IAEA,IAAKJ,OAAO,KAAKN,MAAM,IAAIM,OAAO,KAAKP,SAAS,EAAG;MAClD;IACD;IAEA,MAAMY,YAAY,GAAGJ,YAAY,CAAC,CAAC;IACnC,MAAM;MAAEK,KAAK;MAAEC,GAAG;MAAEC;IAAK,CAAC,GAAGH,YAAY;;IAEzC;IACA,IAAKC,KAAK,KAAK,CAAC,IAAIC,GAAG,KAAK,CAAC,IAAIA,GAAG,KAAKC,IAAI,CAACC,MAAM,EAAG;MACtDP,YAAY,CAAEP,MAAM,CAAEU,YAAa,CAAE,CAAC;MACtCN,KAAK,CAACW,cAAc,CAAC,CAAC;IACvB;EACD;EAEAb,OAAO,CAACc,gBAAgB,CAAE,SAAS,EAAEb,SAAU,CAAC;EAChD,OAAO,MAAM;IACZD,OAAO,CAACe,mBAAmB,CAAE,SAAS,EAAEd,SAAU,CAAC;EACpD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,101 @@
/**
* WordPress dependencies
*/
import { LEFT, RIGHT } from '@wordpress/keycodes';
/**
* Internal dependencies
*/
import { isCollapsed } from '../../is-collapsed';
const EMPTY_ACTIVE_FORMATS = [];
export default (props => element => {
function onKeyDown(event) {
const {
keyCode,
shiftKey,
altKey,
metaKey,
ctrlKey
} = event;
if (
// Only override left and right keys without modifiers pressed.
shiftKey || altKey || metaKey || ctrlKey || keyCode !== LEFT && keyCode !== RIGHT) {
return;
}
const {
record,
applyRecord,
forceRender
} = props.current;
const {
text,
formats,
start,
end,
activeFormats: currentActiveFormats = []
} = record.current;
const collapsed = isCollapsed(record.current);
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
// To do: ideally, we should look at visual position instead.
const {
direction
} = defaultView.getComputedStyle(element);
const reverseKey = direction === 'rtl' ? RIGHT : LEFT;
const isReverse = event.keyCode === reverseKey;
// If the selection is collapsed and at the very start, do nothing if
// navigating backward.
// If the selection is collapsed and at the very end, do nothing if
// navigating forward.
if (collapsed && currentActiveFormats.length === 0) {
if (start === 0 && isReverse) {
return;
}
if (end === text.length && !isReverse) {
return;
}
}
// If the selection is not collapsed, let the browser handle collapsing
// the selection for now. Later we could expand this logic to set
// boundary positions if needed.
if (!collapsed) {
return;
}
const formatsBefore = formats[start - 1] || EMPTY_ACTIVE_FORMATS;
const formatsAfter = formats[start] || EMPTY_ACTIVE_FORMATS;
const destination = isReverse ? formatsBefore : formatsAfter;
const isIncreasing = currentActiveFormats.every((format, index) => format === destination[index]);
let newActiveFormatsLength = currentActiveFormats.length;
if (!isIncreasing) {
newActiveFormatsLength--;
} else if (newActiveFormatsLength < destination.length) {
newActiveFormatsLength++;
}
if (newActiveFormatsLength === currentActiveFormats.length) {
record.current._newActiveFormats = destination;
return;
}
event.preventDefault();
const origin = isReverse ? formatsAfter : formatsBefore;
const source = isIncreasing ? destination : origin;
const newActiveFormats = source.slice(0, newActiveFormatsLength);
const newValue = {
...record.current,
activeFormats: newActiveFormats
};
record.current = newValue;
applyRecord(newValue);
forceRender();
}
element.addEventListener('keydown', onKeyDown);
return () => {
element.removeEventListener('keydown', onKeyDown);
};
});
//# sourceMappingURL=format-boundaries.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { useMemo, useRef } from '@wordpress/element';
import { useRefEffect } from '@wordpress/compose';
/**
* Internal dependencies
*/
import copyHandler from './copy-handler';
import selectObject from './select-object';
import formatBoundaries from './format-boundaries';
import deleteHandler from './delete';
import inputAndSelection from './input-and-selection';
import selectionChangeCompat from './selection-change-compat';
const allEventListeners = [copyHandler, selectObject, formatBoundaries, deleteHandler, inputAndSelection, selectionChangeCompat];
export function useEventListeners(props) {
const propsRef = useRef(props);
propsRef.current = props;
const refEffects = useMemo(() => allEventListeners.map(refEffect => refEffect(propsRef)), [propsRef]);
return useRefEffect(element => {
const cleanups = refEffects.map(effect => effect(element));
return () => {
cleanups.forEach(cleanup => cleanup());
};
}, [refEffects]);
}
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useMemo","useRef","useRefEffect","copyHandler","selectObject","formatBoundaries","deleteHandler","inputAndSelection","selectionChangeCompat","allEventListeners","useEventListeners","props","propsRef","current","refEffects","map","refEffect","element","cleanups","effect","forEach","cleanup"],"sources":["@wordpress/rich-text/src/component/event-listeners/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useMemo, useRef } from '@wordpress/element';\nimport { useRefEffect } from '@wordpress/compose';\n\n/**\n * Internal dependencies\n */\nimport copyHandler from './copy-handler';\nimport selectObject from './select-object';\nimport formatBoundaries from './format-boundaries';\nimport deleteHandler from './delete';\nimport inputAndSelection from './input-and-selection';\nimport selectionChangeCompat from './selection-change-compat';\n\nconst allEventListeners = [\n\tcopyHandler,\n\tselectObject,\n\tformatBoundaries,\n\tdeleteHandler,\n\tinputAndSelection,\n\tselectionChangeCompat,\n];\n\nexport function useEventListeners( props ) {\n\tconst propsRef = useRef( props );\n\tpropsRef.current = props;\n\tconst refEffects = useMemo(\n\t\t() => allEventListeners.map( ( refEffect ) => refEffect( propsRef ) ),\n\t\t[ propsRef ]\n\t);\n\n\treturn useRefEffect(\n\t\t( element ) => {\n\t\t\tconst cleanups = refEffects.map( ( effect ) => effect( element ) );\n\t\t\treturn () => {\n\t\t\t\tcleanups.forEach( ( cleanup ) => cleanup() );\n\t\t\t};\n\t\t},\n\t\t[ refEffects ]\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,OAAO,EAAEC,MAAM,QAAQ,oBAAoB;AACpD,SAASC,YAAY,QAAQ,oBAAoB;;AAEjD;AACA;AACA;AACA,OAAOC,WAAW,MAAM,gBAAgB;AACxC,OAAOC,YAAY,MAAM,iBAAiB;AAC1C,OAAOC,gBAAgB,MAAM,qBAAqB;AAClD,OAAOC,aAAa,MAAM,UAAU;AACpC,OAAOC,iBAAiB,MAAM,uBAAuB;AACrD,OAAOC,qBAAqB,MAAM,2BAA2B;AAE7D,MAAMC,iBAAiB,GAAG,CACzBN,WAAW,EACXC,YAAY,EACZC,gBAAgB,EAChBC,aAAa,EACbC,iBAAiB,EACjBC,qBAAqB,CACrB;AAED,OAAO,SAASE,iBAAiBA,CAAEC,KAAK,EAAG;EAC1C,MAAMC,QAAQ,GAAGX,MAAM,CAAEU,KAAM,CAAC;EAChCC,QAAQ,CAACC,OAAO,GAAGF,KAAK;EACxB,MAAMG,UAAU,GAAGd,OAAO,CACzB,MAAMS,iBAAiB,CAACM,GAAG,CAAIC,SAAS,IAAMA,SAAS,CAAEJ,QAAS,CAAE,CAAC,EACrE,CAAEA,QAAQ,CACX,CAAC;EAED,OAAOV,YAAY,CAChBe,OAAO,IAAM;IACd,MAAMC,QAAQ,GAAGJ,UAAU,CAACC,GAAG,CAAII,MAAM,IAAMA,MAAM,CAAEF,OAAQ,CAAE,CAAC;IAClE,OAAO,MAAM;MACZC,QAAQ,CAACE,OAAO,CAAIC,OAAO,IAAMA,OAAO,CAAC,CAAE,CAAC;IAC7C,CAAC;EACF,CAAC,EACD,CAAEP,UAAU,CACb,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,235 @@
/**
* Internal dependencies
*/
import { getActiveFormats } from '../../get-active-formats';
import { updateFormats } from '../../update-formats';
/**
* All inserting input types that would insert HTML into the DOM.
*
* @see https://www.w3.org/TR/input-events-2/#interface-InputEvent-Attributes
*
* @type {Set}
*/
const INSERTION_INPUT_TYPES_TO_IGNORE = new Set(['insertParagraph', 'insertOrderedList', 'insertUnorderedList', 'insertHorizontalRule', 'insertLink']);
const EMPTY_ACTIVE_FORMATS = [];
const PLACEHOLDER_ATTR_NAME = 'data-rich-text-placeholder';
/**
* If the selection is set on the placeholder element, collapse the selection to
* the start (before the placeholder).
*
* @param {Window} defaultView
*/
function fixPlaceholderSelection(defaultView) {
const selection = defaultView.getSelection();
const {
anchorNode,
anchorOffset
} = selection;
if (anchorNode.nodeType !== anchorNode.ELEMENT_NODE) {
return;
}
const targetNode = anchorNode.childNodes[anchorOffset];
if (!targetNode || targetNode.nodeType !== targetNode.ELEMENT_NODE || !targetNode.hasAttribute(PLACEHOLDER_ATTR_NAME)) {
return;
}
selection.collapseToStart();
}
export default (props => element => {
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
let isComposing = false;
function onInput(event) {
// Do not trigger a change if characters are being composed. Browsers
// will usually emit a final `input` event when the characters are
// composed. As of December 2019, Safari doesn't support
// nativeEvent.isComposing.
if (isComposing) {
return;
}
let inputType;
if (event) {
inputType = event.inputType;
}
const {
record,
applyRecord,
createRecord,
handleChange
} = props.current;
// The browser formatted something or tried to insert HTML. Overwrite
// it. It will be handled later by the format library if needed.
if (inputType && (inputType.indexOf('format') === 0 || INSERTION_INPUT_TYPES_TO_IGNORE.has(inputType))) {
applyRecord(record.current);
return;
}
const currentValue = createRecord();
const {
start,
activeFormats: oldActiveFormats = []
} = record.current;
// Update the formats between the last and new caret position.
const change = updateFormats({
value: currentValue,
start,
end: currentValue.start,
formats: oldActiveFormats
});
handleChange(change);
}
/**
* Syncs the selection to local state. A callback for the `selectionchange`
* event.
*/
function handleSelectionChange() {
const {
record,
applyRecord,
createRecord,
onSelectionChange
} = props.current;
// Check if the implementor disabled editing. `contentEditable` does
// disable input, but not text selection, so we must ignore selection
// changes.
if (element.contentEditable !== 'true') {
return;
}
// Ensure the active element is the rich text element.
if (ownerDocument.activeElement !== element) {
// If it is not, we can stop listening for selection changes. We
// resume listening when the element is focused.
ownerDocument.removeEventListener('selectionchange', handleSelectionChange);
return;
}
// In case of a keyboard event, ignore selection changes during
// composition.
if (isComposing) {
return;
}
const {
start,
end,
text
} = createRecord();
const oldRecord = record.current;
// Fallback mechanism for IE11, which doesn't support the input event.
// Any input results in a selection change.
if (text !== oldRecord.text) {
onInput();
return;
}
if (start === oldRecord.start && end === oldRecord.end) {
// Sometimes the browser may set the selection on the placeholder
// element, in which case the caret is not visible. We need to set
// the caret before the placeholder if that's the case.
if (oldRecord.text.length === 0 && start === 0) {
fixPlaceholderSelection(defaultView);
}
return;
}
const newValue = {
...oldRecord,
start,
end,
// _newActiveFormats may be set on arrow key navigation to control
// the right boundary position. If undefined, getActiveFormats will
// give the active formats according to the browser.
activeFormats: oldRecord._newActiveFormats,
_newActiveFormats: undefined
};
const newActiveFormats = getActiveFormats(newValue, EMPTY_ACTIVE_FORMATS);
// Update the value with the new active formats.
newValue.activeFormats = newActiveFormats;
// It is important that the internal value is updated first,
// otherwise the value will be wrong on render!
record.current = newValue;
applyRecord(newValue, {
domOnly: true
});
onSelectionChange(start, end);
}
function onCompositionStart() {
isComposing = true;
// Do not update the selection when characters are being composed as
// this rerenders the component and might destroy internal browser
// editing state.
ownerDocument.removeEventListener('selectionchange', handleSelectionChange);
// Remove the placeholder. Since the rich text value doesn't update
// during composition, the placeholder doesn't get removed. There's no
// need to re-add it, when the value is updated on compositionend it
// will be re-added when the value is empty.
element.querySelector(`[${PLACEHOLDER_ATTR_NAME}]`)?.remove();
}
function onCompositionEnd() {
isComposing = false;
// Ensure the value is up-to-date for browsers that don't emit a final
// input event after composition.
onInput({
inputType: 'insertText'
});
// Tracking selection changes can be resumed.
ownerDocument.addEventListener('selectionchange', handleSelectionChange);
}
function onFocus() {
const {
record,
isSelected,
onSelectionChange,
applyRecord
} = props.current;
// When the whole editor is editable, let writing flow handle
// selection.
if (element.parentElement.closest('[contenteditable="true"]')) {
return;
}
if (!isSelected) {
// We know for certain that on focus, the old selection is invalid.
// It will be recalculated on the next mouseup, keyup, or touchend
// event.
const index = undefined;
record.current = {
...record.current,
start: index,
end: index,
activeFormats: EMPTY_ACTIVE_FORMATS
};
} else {
applyRecord(record.current, {
domOnly: true
});
}
onSelectionChange(record.current.start, record.current.end);
// There is no selection change event when the element is focused, so
// we need to manually trigger it. The selection is also not available
// yet in this call stack.
window.queueMicrotask(handleSelectionChange);
ownerDocument.addEventListener('selectionchange', handleSelectionChange);
}
element.addEventListener('input', onInput);
element.addEventListener('compositionstart', onCompositionStart);
element.addEventListener('compositionend', onCompositionEnd);
element.addEventListener('focus', onFocus);
return () => {
element.removeEventListener('input', onInput);
element.removeEventListener('compositionstart', onCompositionStart);
element.removeEventListener('compositionend', onCompositionEnd);
element.removeEventListener('focus', onFocus);
};
});
//# sourceMappingURL=input-and-selection.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,46 @@
export default (() => element => {
function onClick(event) {
const {
target
} = event;
// If the child element has no text content, it must be an object.
if (target === element || target.textContent && target.isContentEditable) {
return;
}
const {
ownerDocument
} = target;
const {
defaultView
} = ownerDocument;
const selection = defaultView.getSelection();
// If it's already selected, do nothing and let default behavior happen.
// This means it's "click-through".
if (selection.containsNode(target)) {
return;
}
const range = ownerDocument.createRange();
// If the target is within a non editable element, select the non
// editable element.
const nodeToSelect = target.isContentEditable ? target : target.closest('[contenteditable]');
range.selectNode(nodeToSelect);
selection.removeAllRanges();
selection.addRange(range);
event.preventDefault();
}
function onFocusIn(event) {
// When there is incoming focus from a link, select the object.
if (event.relatedTarget && !element.contains(event.relatedTarget) && event.relatedTarget.tagName === 'A') {
onClick(event);
}
}
element.addEventListener('click', onClick);
element.addEventListener('focusin', onFocusIn);
return () => {
element.removeEventListener('click', onClick);
element.removeEventListener('focusin', onFocusIn);
};
});
//# sourceMappingURL=select-object.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["element","onClick","event","target","textContent","isContentEditable","ownerDocument","defaultView","selection","getSelection","containsNode","range","createRange","nodeToSelect","closest","selectNode","removeAllRanges","addRange","preventDefault","onFocusIn","relatedTarget","contains","tagName","addEventListener","removeEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/select-object.js"],"sourcesContent":["export default () => ( element ) => {\n\tfunction onClick( event ) {\n\t\tconst { target } = event;\n\n\t\t// If the child element has no text content, it must be an object.\n\t\tif (\n\t\t\ttarget === element ||\n\t\t\t( target.textContent && target.isContentEditable )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { ownerDocument } = target;\n\t\tconst { defaultView } = ownerDocument;\n\t\tconst selection = defaultView.getSelection();\n\n\t\t// If it's already selected, do nothing and let default behavior happen.\n\t\t// This means it's \"click-through\".\n\t\tif ( selection.containsNode( target ) ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst range = ownerDocument.createRange();\n\t\t// If the target is within a non editable element, select the non\n\t\t// editable element.\n\t\tconst nodeToSelect = target.isContentEditable\n\t\t\t? target\n\t\t\t: target.closest( '[contenteditable]' );\n\n\t\trange.selectNode( nodeToSelect );\n\t\tselection.removeAllRanges();\n\t\tselection.addRange( range );\n\n\t\tevent.preventDefault();\n\t}\n\n\tfunction onFocusIn( event ) {\n\t\t// When there is incoming focus from a link, select the object.\n\t\tif (\n\t\t\tevent.relatedTarget &&\n\t\t\t! element.contains( event.relatedTarget ) &&\n\t\t\tevent.relatedTarget.tagName === 'A'\n\t\t) {\n\t\t\tonClick( event );\n\t\t}\n\t}\n\n\telement.addEventListener( 'click', onClick );\n\telement.addEventListener( 'focusin', onFocusIn );\n\treturn () => {\n\t\telement.removeEventListener( 'click', onClick );\n\t\telement.removeEventListener( 'focusin', onFocusIn );\n\t};\n};\n"],"mappings":"AAAA,gBAAe,MAAQA,OAAO,IAAM;EACnC,SAASC,OAAOA,CAAEC,KAAK,EAAG;IACzB,MAAM;MAAEC;IAAO,CAAC,GAAGD,KAAK;;IAExB;IACA,IACCC,MAAM,KAAKH,OAAO,IAChBG,MAAM,CAACC,WAAW,IAAID,MAAM,CAACE,iBAAmB,EACjD;MACD;IACD;IAEA,MAAM;MAAEC;IAAc,CAAC,GAAGH,MAAM;IAChC,MAAM;MAAEI;IAAY,CAAC,GAAGD,aAAa;IACrC,MAAME,SAAS,GAAGD,WAAW,CAACE,YAAY,CAAC,CAAC;;IAE5C;IACA;IACA,IAAKD,SAAS,CAACE,YAAY,CAAEP,MAAO,CAAC,EAAG;MACvC;IACD;IAEA,MAAMQ,KAAK,GAAGL,aAAa,CAACM,WAAW,CAAC,CAAC;IACzC;IACA;IACA,MAAMC,YAAY,GAAGV,MAAM,CAACE,iBAAiB,GAC1CF,MAAM,GACNA,MAAM,CAACW,OAAO,CAAE,mBAAoB,CAAC;IAExCH,KAAK,CAACI,UAAU,CAAEF,YAAa,CAAC;IAChCL,SAAS,CAACQ,eAAe,CAAC,CAAC;IAC3BR,SAAS,CAACS,QAAQ,CAAEN,KAAM,CAAC;IAE3BT,KAAK,CAACgB,cAAc,CAAC,CAAC;EACvB;EAEA,SAASC,SAASA,CAAEjB,KAAK,EAAG;IAC3B;IACA,IACCA,KAAK,CAACkB,aAAa,IACnB,CAAEpB,OAAO,CAACqB,QAAQ,CAAEnB,KAAK,CAACkB,aAAc,CAAC,IACzClB,KAAK,CAACkB,aAAa,CAACE,OAAO,KAAK,GAAG,EAClC;MACDrB,OAAO,CAAEC,KAAM,CAAC;IACjB;EACD;EAEAF,OAAO,CAACuB,gBAAgB,CAAE,OAAO,EAAEtB,OAAQ,CAAC;EAC5CD,OAAO,CAACuB,gBAAgB,CAAE,SAAS,EAAEJ,SAAU,CAAC;EAChD,OAAO,MAAM;IACZnB,OAAO,CAACwB,mBAAmB,CAAE,OAAO,EAAEvB,OAAQ,CAAC;IAC/CD,OAAO,CAACwB,mBAAmB,CAAE,SAAS,EAAEL,SAAU,CAAC;EACpD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,50 @@
/**
* Internal dependencies
*/
import { isRangeEqual } from '../../is-range-equal';
/**
* Sometimes some browsers are not firing a `selectionchange` event when
* changing the selection by mouse or keyboard. This hook makes sure that, if we
* detect no `selectionchange` or `input` event between the up and down events,
* we fire a `selectionchange` event.
*/
export default (() => element => {
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
const selection = defaultView?.getSelection();
let range;
function getRange() {
return selection.rangeCount ? selection.getRangeAt(0) : null;
}
function onDown(event) {
const type = event.type === 'keydown' ? 'keyup' : 'pointerup';
function onCancel() {
ownerDocument.removeEventListener(type, onUp);
ownerDocument.removeEventListener('selectionchange', onCancel);
ownerDocument.removeEventListener('input', onCancel);
}
function onUp() {
onCancel();
if (isRangeEqual(range, getRange())) {
return;
}
ownerDocument.dispatchEvent(new Event('selectionchange'));
}
ownerDocument.addEventListener(type, onUp);
ownerDocument.addEventListener('selectionchange', onCancel);
ownerDocument.addEventListener('input', onCancel);
range = getRange();
}
element.addEventListener('pointerdown', onDown);
element.addEventListener('keydown', onDown);
return () => {
element.removeEventListener('pointerdown', onDown);
element.removeEventListener('keydown', onDown);
};
});
//# sourceMappingURL=selection-change-compat.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isRangeEqual","element","ownerDocument","defaultView","selection","getSelection","range","getRange","rangeCount","getRangeAt","onDown","event","type","onCancel","removeEventListener","onUp","dispatchEvent","Event","addEventListener"],"sources":["@wordpress/rich-text/src/component/event-listeners/selection-change-compat.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { isRangeEqual } from '../../is-range-equal';\n\n/**\n * Sometimes some browsers are not firing a `selectionchange` event when\n * changing the selection by mouse or keyboard. This hook makes sure that, if we\n * detect no `selectionchange` or `input` event between the up and down events,\n * we fire a `selectionchange` event.\n */\nexport default () => ( element ) => {\n\tconst { ownerDocument } = element;\n\tconst { defaultView } = ownerDocument;\n\tconst selection = defaultView?.getSelection();\n\n\tlet range;\n\n\tfunction getRange() {\n\t\treturn selection.rangeCount ? selection.getRangeAt( 0 ) : null;\n\t}\n\n\tfunction onDown( event ) {\n\t\tconst type = event.type === 'keydown' ? 'keyup' : 'pointerup';\n\n\t\tfunction onCancel() {\n\t\t\townerDocument.removeEventListener( type, onUp );\n\t\t\townerDocument.removeEventListener( 'selectionchange', onCancel );\n\t\t\townerDocument.removeEventListener( 'input', onCancel );\n\t\t}\n\n\t\tfunction onUp() {\n\t\t\tonCancel();\n\t\t\tif ( isRangeEqual( range, getRange() ) ) {\n\t\t\t\treturn;\n\t\t\t}\n\t\t\townerDocument.dispatchEvent( new Event( 'selectionchange' ) );\n\t\t}\n\n\t\townerDocument.addEventListener( type, onUp );\n\t\townerDocument.addEventListener( 'selectionchange', onCancel );\n\t\townerDocument.addEventListener( 'input', onCancel );\n\n\t\trange = getRange();\n\t}\n\n\telement.addEventListener( 'pointerdown', onDown );\n\telement.addEventListener( 'keydown', onDown );\n\treturn () => {\n\t\telement.removeEventListener( 'pointerdown', onDown );\n\t\telement.removeEventListener( 'keydown', onDown );\n\t};\n};\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,YAAY,QAAQ,sBAAsB;;AAEnD;AACA;AACA;AACA;AACA;AACA;AACA,gBAAe,MAAQC,OAAO,IAAM;EACnC,MAAM;IAAEC;EAAc,CAAC,GAAGD,OAAO;EACjC,MAAM;IAAEE;EAAY,CAAC,GAAGD,aAAa;EACrC,MAAME,SAAS,GAAGD,WAAW,EAAEE,YAAY,CAAC,CAAC;EAE7C,IAAIC,KAAK;EAET,SAASC,QAAQA,CAAA,EAAG;IACnB,OAAOH,SAAS,CAACI,UAAU,GAAGJ,SAAS,CAACK,UAAU,CAAE,CAAE,CAAC,GAAG,IAAI;EAC/D;EAEA,SAASC,MAAMA,CAAEC,KAAK,EAAG;IACxB,MAAMC,IAAI,GAAGD,KAAK,CAACC,IAAI,KAAK,SAAS,GAAG,OAAO,GAAG,WAAW;IAE7D,SAASC,QAAQA,CAAA,EAAG;MACnBX,aAAa,CAACY,mBAAmB,CAAEF,IAAI,EAAEG,IAAK,CAAC;MAC/Cb,aAAa,CAACY,mBAAmB,CAAE,iBAAiB,EAAED,QAAS,CAAC;MAChEX,aAAa,CAACY,mBAAmB,CAAE,OAAO,EAAED,QAAS,CAAC;IACvD;IAEA,SAASE,IAAIA,CAAA,EAAG;MACfF,QAAQ,CAAC,CAAC;MACV,IAAKb,YAAY,CAAEM,KAAK,EAAEC,QAAQ,CAAC,CAAE,CAAC,EAAG;QACxC;MACD;MACAL,aAAa,CAACc,aAAa,CAAE,IAAIC,KAAK,CAAE,iBAAkB,CAAE,CAAC;IAC9D;IAEAf,aAAa,CAACgB,gBAAgB,CAAEN,IAAI,EAAEG,IAAK,CAAC;IAC5Cb,aAAa,CAACgB,gBAAgB,CAAE,iBAAiB,EAAEL,QAAS,CAAC;IAC7DX,aAAa,CAACgB,gBAAgB,CAAE,OAAO,EAAEL,QAAS,CAAC;IAEnDP,KAAK,GAAGC,QAAQ,CAAC,CAAC;EACnB;EAEAN,OAAO,CAACiB,gBAAgB,CAAE,aAAa,EAAER,MAAO,CAAC;EACjDT,OAAO,CAACiB,gBAAgB,CAAE,SAAS,EAAER,MAAO,CAAC;EAC7C,OAAO,MAAM;IACZT,OAAO,CAACa,mBAAmB,CAAE,aAAa,EAAEJ,MAAO,CAAC;IACpDT,OAAO,CAACa,mBAAmB,CAAE,SAAS,EAAEJ,MAAO,CAAC;EACjD,CAAC;AACF,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,199 @@
/**
* WordPress dependencies
*/
import { useRef, useLayoutEffect, useReducer } from '@wordpress/element';
import { useMergeRefs, useRefEffect } from '@wordpress/compose';
import { useRegistry } from '@wordpress/data';
/**
* Internal dependencies
*/
import { create, RichTextData } from '../create';
import { apply } from '../to-dom';
import { toHTMLString } from '../to-html-string';
import { useDefaultStyle } from './use-default-style';
import { useBoundaryStyle } from './use-boundary-style';
import { useEventListeners } from './event-listeners';
export function useRichText({
value = '',
selectionStart,
selectionEnd,
placeholder,
onSelectionChange,
preserveWhiteSpace,
onChange,
__unstableDisableFormats: disableFormats,
__unstableIsSelected: isSelected,
__unstableDependencies = [],
__unstableAfterParse,
__unstableBeforeSerialize,
__unstableAddInvisibleFormats
}) {
const registry = useRegistry();
const [, forceRender] = useReducer(() => ({}));
const ref = useRef();
function createRecord() {
const {
ownerDocument: {
defaultView
}
} = ref.current;
const selection = defaultView.getSelection();
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
return create({
element: ref.current,
range,
__unstableIsEditableTree: true
});
}
function applyRecord(newRecord, {
domOnly
} = {}) {
apply({
value: newRecord,
current: ref.current,
prepareEditableTree: __unstableAddInvisibleFormats,
__unstableDomOnly: domOnly,
placeholder
});
}
// Internal values are updated synchronously, unlike props and state.
const _value = useRef(value);
const record = useRef();
function setRecordFromProps() {
_value.current = value;
record.current = value;
if (!(value instanceof RichTextData)) {
record.current = value ? RichTextData.fromHTMLString(value, {
preserveWhiteSpace
}) : RichTextData.empty();
}
// To do: make rich text internally work with RichTextData.
record.current = {
text: record.current.text,
formats: record.current.formats,
replacements: record.current.replacements
};
if (disableFormats) {
record.current.formats = Array(value.length);
record.current.replacements = Array(value.length);
}
if (__unstableAfterParse) {
record.current.formats = __unstableAfterParse(record.current);
}
record.current.start = selectionStart;
record.current.end = selectionEnd;
}
const hadSelectionUpdate = useRef(false);
if (!record.current) {
hadSelectionUpdate.current = isSelected;
setRecordFromProps();
} else if (selectionStart !== record.current.start || selectionEnd !== record.current.end) {
hadSelectionUpdate.current = isSelected;
record.current = {
...record.current,
start: selectionStart,
end: selectionEnd,
activeFormats: undefined
};
}
/**
* Sync the value to global state. The node tree and selection will also be
* updated if differences are found.
*
* @param {Object} newRecord The record to sync and apply.
*/
function handleChange(newRecord) {
record.current = newRecord;
applyRecord(newRecord);
if (disableFormats) {
_value.current = newRecord.text;
} else {
const newFormats = __unstableBeforeSerialize ? __unstableBeforeSerialize(newRecord) : newRecord.formats;
newRecord = {
...newRecord,
formats: newFormats
};
if (typeof value === 'string') {
_value.current = toHTMLString({
value: newRecord,
preserveWhiteSpace
});
} else {
_value.current = new RichTextData(newRecord);
}
}
const {
start,
end,
formats,
text
} = record.current;
// Selection must be updated first, so it is recorded in history when
// the content change happens.
// We batch both calls to only attempt to rerender once.
registry.batch(() => {
onSelectionChange(start, end);
onChange(_value.current, {
__unstableFormats: formats,
__unstableText: text
});
});
forceRender();
}
function applyFromProps() {
setRecordFromProps();
applyRecord(record.current);
}
const didMount = useRef(false);
// Value updates must happen synchonously to avoid overwriting newer values.
useLayoutEffect(() => {
if (didMount.current && value !== _value.current) {
applyFromProps();
forceRender();
}
}, [value]);
// Value updates must happen synchonously to avoid overwriting newer values.
useLayoutEffect(() => {
if (!hadSelectionUpdate.current) {
return;
}
if (ref.current.ownerDocument.activeElement !== ref.current) {
ref.current.focus();
}
applyRecord(record.current);
hadSelectionUpdate.current = false;
}, [hadSelectionUpdate.current]);
const mergedRefs = useMergeRefs([ref, useDefaultStyle(), useBoundaryStyle({
record
}), useEventListeners({
record,
handleChange,
applyRecord,
createRecord,
isSelected,
onSelectionChange,
forceRender
}), useRefEffect(() => {
applyFromProps();
didMount.current = true;
}, [placeholder, ...__unstableDependencies])]);
return {
value: record.current,
// A function to get the most recent value so event handlers in
// useRichText implementations have access to it. For example when
// listening to input events, we internally update the state, but this
// state is not yet available to the input event handler because React
// may re-render asynchronously.
getValue: () => record.current,
onChange: handleChange,
ref: mergedRefs
};
}
export default function __experimentalRichText() {}
//# sourceMappingURL=index.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,75 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { getActiveFormat } from '../get-active-format';
/**
* @template T
* @typedef {import('@wordpress/element').RefObject<T>} RefObject<T>
*/
/** @typedef {import('../register-format-type').WPFormat} WPFormat */
/** @typedef {import('../types').RichTextValue} RichTextValue */
/**
* This hook, to be used in a format type's Edit component, returns the active
* element that is formatted, or the selection range if no format is active.
* The returned value is meant to be used for positioning UI, e.g. by passing it
* to the `Popover` component.
*
* @param {Object} $1 Named parameters.
* @param {RefObject<HTMLElement>} $1.ref React ref of the element
* containing the editable content.
* @param {RichTextValue} $1.value Value to check for selection.
* @param {WPFormat} $1.settings The format type's settings.
*
* @return {Element|Range} The active element or selection range.
*/
export function useAnchorRef({
ref,
value,
settings = {}
}) {
deprecated('`useAnchorRef` hook', {
since: '6.1',
alternative: '`useAnchor` hook'
});
const {
tagName,
className,
name
} = settings;
const activeFormat = name ? getActiveFormat(value, name) : undefined;
return useMemo(() => {
if (!ref.current) {
return;
}
const {
ownerDocument: {
defaultView
}
} = ref.current;
const selection = defaultView.getSelection();
if (!selection.rangeCount) {
return;
}
const range = selection.getRangeAt(0);
if (!activeFormat) {
return range;
}
let element = range.startContainer;
// If the caret is right before the element, select the next element.
element = element.nextElementSibling || element;
while (element.nodeType !== element.ELEMENT_NODE) {
element = element.parentNode;
}
return element.closest(tagName + (className ? '.' + className : ''));
}, [activeFormat, value.start, value.end, tagName, className]);
}
//# sourceMappingURL=use-anchor-ref.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useMemo","deprecated","getActiveFormat","useAnchorRef","ref","value","settings","since","alternative","tagName","className","name","activeFormat","undefined","current","ownerDocument","defaultView","selection","getSelection","rangeCount","range","getRangeAt","element","startContainer","nextElementSibling","nodeType","ELEMENT_NODE","parentNode","closest","start","end"],"sources":["@wordpress/rich-text/src/component/use-anchor-ref.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useMemo } from '@wordpress/element';\nimport deprecated from '@wordpress/deprecated';\n\n/**\n * Internal dependencies\n */\nimport { getActiveFormat } from '../get-active-format';\n\n/**\n * @template T\n * @typedef {import('@wordpress/element').RefObject<T>} RefObject<T>\n */\n/** @typedef {import('../register-format-type').WPFormat} WPFormat */\n/** @typedef {import('../types').RichTextValue} RichTextValue */\n\n/**\n * This hook, to be used in a format type's Edit component, returns the active\n * element that is formatted, or the selection range if no format is active.\n * The returned value is meant to be used for positioning UI, e.g. by passing it\n * to the `Popover` component.\n *\n * @param {Object} $1 Named parameters.\n * @param {RefObject<HTMLElement>} $1.ref React ref of the element\n * containing the editable content.\n * @param {RichTextValue} $1.value Value to check for selection.\n * @param {WPFormat} $1.settings The format type's settings.\n *\n * @return {Element|Range} The active element or selection range.\n */\nexport function useAnchorRef( { ref, value, settings = {} } ) {\n\tdeprecated( '`useAnchorRef` hook', {\n\t\tsince: '6.1',\n\t\talternative: '`useAnchor` hook',\n\t} );\n\n\tconst { tagName, className, name } = settings;\n\tconst activeFormat = name ? getActiveFormat( value, name ) : undefined;\n\n\treturn useMemo( () => {\n\t\tif ( ! ref.current ) {\n\t\t\treturn;\n\t\t}\n\t\tconst {\n\t\t\townerDocument: { defaultView },\n\t\t} = ref.current;\n\t\tconst selection = defaultView.getSelection();\n\n\t\tif ( ! selection.rangeCount ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst range = selection.getRangeAt( 0 );\n\n\t\tif ( ! activeFormat ) {\n\t\t\treturn range;\n\t\t}\n\n\t\tlet element = range.startContainer;\n\n\t\t// If the caret is right before the element, select the next element.\n\t\telement = element.nextElementSibling || element;\n\n\t\twhile ( element.nodeType !== element.ELEMENT_NODE ) {\n\t\t\telement = element.parentNode;\n\t\t}\n\n\t\treturn element.closest(\n\t\t\ttagName + ( className ? '.' + className : '' )\n\t\t);\n\t}, [ activeFormat, value.start, value.end, tagName, className ] );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,OAAO,QAAQ,oBAAoB;AAC5C,OAAOC,UAAU,MAAM,uBAAuB;;AAE9C;AACA;AACA;AACA,SAASC,eAAe,QAAQ,sBAAsB;;AAEtD;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAE;EAAEC,GAAG;EAAEC,KAAK;EAAEC,QAAQ,GAAG,CAAC;AAAE,CAAC,EAAG;EAC7DL,UAAU,CAAE,qBAAqB,EAAE;IAClCM,KAAK,EAAE,KAAK;IACZC,WAAW,EAAE;EACd,CAAE,CAAC;EAEH,MAAM;IAAEC,OAAO;IAAEC,SAAS;IAAEC;EAAK,CAAC,GAAGL,QAAQ;EAC7C,MAAMM,YAAY,GAAGD,IAAI,GAAGT,eAAe,CAAEG,KAAK,EAAEM,IAAK,CAAC,GAAGE,SAAS;EAEtE,OAAOb,OAAO,CAAE,MAAM;IACrB,IAAK,CAAEI,GAAG,CAACU,OAAO,EAAG;MACpB;IACD;IACA,MAAM;MACLC,aAAa,EAAE;QAAEC;MAAY;IAC9B,CAAC,GAAGZ,GAAG,CAACU,OAAO;IACf,MAAMG,SAAS,GAAGD,WAAW,CAACE,YAAY,CAAC,CAAC;IAE5C,IAAK,CAAED,SAAS,CAACE,UAAU,EAAG;MAC7B;IACD;IAEA,MAAMC,KAAK,GAAGH,SAAS,CAACI,UAAU,CAAE,CAAE,CAAC;IAEvC,IAAK,CAAET,YAAY,EAAG;MACrB,OAAOQ,KAAK;IACb;IAEA,IAAIE,OAAO,GAAGF,KAAK,CAACG,cAAc;;IAElC;IACAD,OAAO,GAAGA,OAAO,CAACE,kBAAkB,IAAIF,OAAO;IAE/C,OAAQA,OAAO,CAACG,QAAQ,KAAKH,OAAO,CAACI,YAAY,EAAG;MACnDJ,OAAO,GAAGA,OAAO,CAACK,UAAU;IAC7B;IAEA,OAAOL,OAAO,CAACM,OAAO,CACrBnB,OAAO,IAAKC,SAAS,GAAG,GAAG,GAAGA,SAAS,GAAG,EAAE,CAC7C,CAAC;EACF,CAAC,EAAE,CAAEE,YAAY,EAAEP,KAAK,CAACwB,KAAK,EAAExB,KAAK,CAACyB,GAAG,EAAErB,OAAO,EAAEC,SAAS,CAAG,CAAC;AAClE","ignoreList":[]}

View File

@@ -0,0 +1,184 @@
/**
* WordPress dependencies
*/
import { usePrevious } from '@wordpress/compose';
import { useState, useLayoutEffect } from '@wordpress/element';
/** @typedef {import('../register-format-type').WPFormat} WPFormat */
/** @typedef {import('../types').RichTextValue} RichTextValue */
/**
* Given a range and a format tag name and class name, returns the closest
* format element.
*
* @param {Range} range The Range to check.
* @param {HTMLElement} editableContentElement The editable wrapper.
* @param {string} tagName The tag name of the format element.
* @param {string} className The class name of the format element.
*
* @return {HTMLElement|undefined} The format element, if found.
*/
function getFormatElement(range, editableContentElement, tagName, className) {
let element = range.startContainer;
// Even if the active format is defined, the actualy DOM range's start
// container may be outside of the format's DOM element:
// `a‸<strong>b</strong>` (DOM) while visually it's `a<strong>‸b</strong>`.
// So at a given selection index, start with the deepest format DOM element.
if (element.nodeType === element.TEXT_NODE && range.startOffset === element.length && element.nextSibling) {
element = element.nextSibling;
while (element.firstChild) {
element = element.firstChild;
}
}
if (element.nodeType !== element.ELEMENT_NODE) {
element = element.parentElement;
}
if (!element) {
return;
}
if (element === editableContentElement) {
return;
}
if (!editableContentElement.contains(element)) {
return;
}
const selector = tagName + (className ? '.' + className : '');
// .closest( selector ), but with a boundary. Check if the element matches
// the selector. If it doesn't match, try the parent element if it's not the
// editable wrapper. We don't want to try to match ancestors of the editable
// wrapper, which is what .closest( selector ) would do. When the element is
// the editable wrapper (which is most likely the case because most text is
// unformatted), this never runs.
while (element !== editableContentElement) {
if (element.matches(selector)) {
return element;
}
element = element.parentElement;
}
}
/**
* @typedef {Object} VirtualAnchorElement
* @property {() => DOMRect} getBoundingClientRect A function returning a DOMRect
* @property {HTMLElement} contextElement The actual DOM element
*/
/**
* Creates a virtual anchor element for a range.
*
* @param {Range} range The range to create a virtual anchor element for.
* @param {HTMLElement} editableContentElement The editable wrapper.
*
* @return {VirtualAnchorElement} The virtual anchor element.
*/
function createVirtualAnchorElement(range, editableContentElement) {
return {
contextElement: editableContentElement,
getBoundingClientRect() {
return editableContentElement.contains(range.startContainer) ? range.getBoundingClientRect() : editableContentElement.getBoundingClientRect();
}
};
}
/**
* Get the anchor: a format element if there is a matching one based on the
* tagName and className or a range otherwise.
*
* @param {HTMLElement} editableContentElement The editable wrapper.
* @param {string} tagName The tag name of the format
* element.
* @param {string} className The class name of the format
* element.
*
* @return {HTMLElement|VirtualAnchorElement|undefined} The anchor.
*/
function getAnchor(editableContentElement, tagName, className) {
if (!editableContentElement) {
return;
}
const {
ownerDocument
} = editableContentElement;
const {
defaultView
} = ownerDocument;
const selection = defaultView.getSelection();
if (!selection) {
return;
}
if (!selection.rangeCount) {
return;
}
const range = selection.getRangeAt(0);
if (!range || !range.startContainer) {
return;
}
const formatElement = getFormatElement(range, editableContentElement, tagName, className);
if (formatElement) {
return formatElement;
}
return createVirtualAnchorElement(range, editableContentElement);
}
/**
* This hook, to be used in a format type's Edit component, returns the active
* element that is formatted, or a virtual element for the selection range if
* no format is active. The returned value is meant to be used for positioning
* UI, e.g. by passing it to the `Popover` component via the `anchor` prop.
*
* @param {Object} $1 Named parameters.
* @param {HTMLElement|null} $1.editableContentElement The element containing
* the editable content.
* @param {WPFormat=} $1.settings The format type's settings.
* @return {Element|VirtualAnchorElement|undefined|null} The active element or selection range.
*/
export function useAnchor({
editableContentElement,
settings = {}
}) {
const {
tagName,
className,
isActive
} = settings;
const [anchor, setAnchor] = useState(() => getAnchor(editableContentElement, tagName, className));
const wasActive = usePrevious(isActive);
useLayoutEffect(() => {
if (!editableContentElement) {
return;
}
function callback() {
setAnchor(getAnchor(editableContentElement, tagName, className));
}
function attach() {
ownerDocument.addEventListener('selectionchange', callback);
}
function detach() {
ownerDocument.removeEventListener('selectionchange', callback);
}
const {
ownerDocument
} = editableContentElement;
if (editableContentElement === ownerDocument.activeElement ||
// When a link is created, we need to attach the popover to the newly created anchor.
!wasActive && isActive ||
// Sometimes we're _removing_ an active anchor, such as the inline color popover.
// When we add the color, it switches from a virtual anchor to a `<mark>` element.
// When we _remove_ the color, it switches from a `<mark>` element to a virtual anchor.
wasActive && !isActive) {
setAnchor(getAnchor(editableContentElement, tagName, className));
attach();
}
editableContentElement.addEventListener('focusin', attach);
editableContentElement.addEventListener('focusout', detach);
return () => {
detach();
editableContentElement.removeEventListener('focusin', attach);
editableContentElement.removeEventListener('focusout', detach);
};
}, [editableContentElement, tagName, className, isActive, wasActive]);
return anchor;
}
//# sourceMappingURL=use-anchor.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,55 @@
/**
* WordPress dependencies
*/
import { useEffect, useRef } from '@wordpress/element';
/*
* Calculates and renders the format boundary style when the active formats
* change.
*/
export function useBoundaryStyle({
record
}) {
const ref = useRef();
const {
activeFormats = [],
replacements,
start
} = record.current;
const activeReplacement = replacements[start];
useEffect(() => {
// There's no need to recalculate the boundary styles if no formats are
// active, because no boundary styles will be visible.
if ((!activeFormats || !activeFormats.length) && !activeReplacement) {
return;
}
const boundarySelector = '*[data-rich-text-format-boundary]';
const element = ref.current.querySelector(boundarySelector);
if (!element) {
return;
}
const {
ownerDocument
} = element;
const {
defaultView
} = ownerDocument;
const computedStyle = defaultView.getComputedStyle(element);
const newColor = computedStyle.color.replace(')', ', 0.2)').replace('rgb', 'rgba');
const selector = `.rich-text:focus ${boundarySelector}`;
const rule = `background-color: ${newColor}`;
const style = `${selector} {${rule}}`;
const globalStyleId = 'rich-text-boundary-style';
let globalStyle = ownerDocument.getElementById(globalStyleId);
if (!globalStyle) {
globalStyle = ownerDocument.createElement('style');
globalStyle.id = globalStyleId;
ownerDocument.head.appendChild(globalStyle);
}
if (globalStyle.innerHTML !== style) {
globalStyle.innerHTML = style;
}
}, [activeFormats, activeReplacement]);
return ref;
}
//# sourceMappingURL=use-boundary-style.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useEffect","useRef","useBoundaryStyle","record","ref","activeFormats","replacements","start","current","activeReplacement","length","boundarySelector","element","querySelector","ownerDocument","defaultView","computedStyle","getComputedStyle","newColor","color","replace","selector","rule","style","globalStyleId","globalStyle","getElementById","createElement","id","head","appendChild","innerHTML"],"sources":["@wordpress/rich-text/src/component/use-boundary-style.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useEffect, useRef } from '@wordpress/element';\n\n/*\n * Calculates and renders the format boundary style when the active formats\n * change.\n */\nexport function useBoundaryStyle( { record } ) {\n\tconst ref = useRef();\n\tconst { activeFormats = [], replacements, start } = record.current;\n\tconst activeReplacement = replacements[ start ];\n\tuseEffect( () => {\n\t\t// There's no need to recalculate the boundary styles if no formats are\n\t\t// active, because no boundary styles will be visible.\n\t\tif (\n\t\t\t( ! activeFormats || ! activeFormats.length ) &&\n\t\t\t! activeReplacement\n\t\t) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst boundarySelector = '*[data-rich-text-format-boundary]';\n\t\tconst element = ref.current.querySelector( boundarySelector );\n\n\t\tif ( ! element ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst { ownerDocument } = element;\n\t\tconst { defaultView } = ownerDocument;\n\t\tconst computedStyle = defaultView.getComputedStyle( element );\n\t\tconst newColor = computedStyle.color\n\t\t\t.replace( ')', ', 0.2)' )\n\t\t\t.replace( 'rgb', 'rgba' );\n\t\tconst selector = `.rich-text:focus ${ boundarySelector }`;\n\t\tconst rule = `background-color: ${ newColor }`;\n\t\tconst style = `${ selector } {${ rule }}`;\n\t\tconst globalStyleId = 'rich-text-boundary-style';\n\n\t\tlet globalStyle = ownerDocument.getElementById( globalStyleId );\n\n\t\tif ( ! globalStyle ) {\n\t\t\tglobalStyle = ownerDocument.createElement( 'style' );\n\t\t\tglobalStyle.id = globalStyleId;\n\t\t\townerDocument.head.appendChild( globalStyle );\n\t\t}\n\n\t\tif ( globalStyle.innerHTML !== style ) {\n\t\t\tglobalStyle.innerHTML = style;\n\t\t}\n\t}, [ activeFormats, activeReplacement ] );\n\treturn ref;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,SAAS,EAAEC,MAAM,QAAQ,oBAAoB;;AAEtD;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAE;EAAEC;AAAO,CAAC,EAAG;EAC9C,MAAMC,GAAG,GAAGH,MAAM,CAAC,CAAC;EACpB,MAAM;IAAEI,aAAa,GAAG,EAAE;IAAEC,YAAY;IAAEC;EAAM,CAAC,GAAGJ,MAAM,CAACK,OAAO;EAClE,MAAMC,iBAAiB,GAAGH,YAAY,CAAEC,KAAK,CAAE;EAC/CP,SAAS,CAAE,MAAM;IAChB;IACA;IACA,IACC,CAAE,CAAEK,aAAa,IAAI,CAAEA,aAAa,CAACK,MAAM,KAC3C,CAAED,iBAAiB,EAClB;MACD;IACD;IAEA,MAAME,gBAAgB,GAAG,mCAAmC;IAC5D,MAAMC,OAAO,GAAGR,GAAG,CAACI,OAAO,CAACK,aAAa,CAAEF,gBAAiB,CAAC;IAE7D,IAAK,CAAEC,OAAO,EAAG;MAChB;IACD;IAEA,MAAM;MAAEE;IAAc,CAAC,GAAGF,OAAO;IACjC,MAAM;MAAEG;IAAY,CAAC,GAAGD,aAAa;IACrC,MAAME,aAAa,GAAGD,WAAW,CAACE,gBAAgB,CAAEL,OAAQ,CAAC;IAC7D,MAAMM,QAAQ,GAAGF,aAAa,CAACG,KAAK,CAClCC,OAAO,CAAE,GAAG,EAAE,QAAS,CAAC,CACxBA,OAAO,CAAE,KAAK,EAAE,MAAO,CAAC;IAC1B,MAAMC,QAAQ,GAAI,oBAAoBV,gBAAkB,EAAC;IACzD,MAAMW,IAAI,GAAI,qBAAqBJ,QAAU,EAAC;IAC9C,MAAMK,KAAK,GAAI,GAAGF,QAAU,KAAKC,IAAM,GAAE;IACzC,MAAME,aAAa,GAAG,0BAA0B;IAEhD,IAAIC,WAAW,GAAGX,aAAa,CAACY,cAAc,CAAEF,aAAc,CAAC;IAE/D,IAAK,CAAEC,WAAW,EAAG;MACpBA,WAAW,GAAGX,aAAa,CAACa,aAAa,CAAE,OAAQ,CAAC;MACpDF,WAAW,CAACG,EAAE,GAAGJ,aAAa;MAC9BV,aAAa,CAACe,IAAI,CAACC,WAAW,CAAEL,WAAY,CAAC;IAC9C;IAEA,IAAKA,WAAW,CAACM,SAAS,KAAKR,KAAK,EAAG;MACtCE,WAAW,CAACM,SAAS,GAAGR,KAAK;IAC9B;EACD,CAAC,EAAE,CAAElB,aAAa,EAAEI,iBAAiB,CAAG,CAAC;EACzC,OAAOL,GAAG;AACX","ignoreList":[]}

View File

@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
import { useCallback } from '@wordpress/element';
/**
* In HTML, leading and trailing spaces are not visible, and multiple spaces
* elsewhere are visually reduced to one space. This rule prevents spaces from
* collapsing so all space is visible in the editor and can be removed. It also
* prevents some browsers from inserting non-breaking spaces at the end of a
* line to prevent the space from visually disappearing. Sometimes these non
* breaking spaces can linger in the editor causing unwanted non breaking spaces
* in between words. If also prevent Firefox from inserting a trailing `br` node
* to visualise any trailing space, causing the element to be saved.
*
* > Authors are encouraged to set the 'white-space' property on editing hosts
* > and on markup that was originally created through these editing mechanisms
* > to the value 'pre-wrap'. Default HTML whitespace handling is not well
* > suited to WYSIWYG editing, and line wrapping will not work correctly in
* > some corner cases if 'white-space' is left at its default value.
*
* https://html.spec.whatwg.org/multipage/interaction.html#best-practices-for-in-page-editors
*
* @type {string}
*/
const whiteSpace = 'pre-wrap';
/**
* A minimum width of 1px will prevent the rich text container from collapsing
* to 0 width and hiding the caret. This is useful for inline containers.
*/
const minWidth = '1px';
export function useDefaultStyle() {
return useCallback(element => {
if (!element) {
return;
}
element.style.whiteSpace = whiteSpace;
element.style.minWidth = minWidth;
}, []);
}
//# sourceMappingURL=use-default-style.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["useCallback","whiteSpace","minWidth","useDefaultStyle","element","style"],"sources":["@wordpress/rich-text/src/component/use-default-style.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\n\n/**\n * In HTML, leading and trailing spaces are not visible, and multiple spaces\n * elsewhere are visually reduced to one space. This rule prevents spaces from\n * collapsing so all space is visible in the editor and can be removed. It also\n * prevents some browsers from inserting non-breaking spaces at the end of a\n * line to prevent the space from visually disappearing. Sometimes these non\n * breaking spaces can linger in the editor causing unwanted non breaking spaces\n * in between words. If also prevent Firefox from inserting a trailing `br` node\n * to visualise any trailing space, causing the element to be saved.\n *\n * > Authors are encouraged to set the 'white-space' property on editing hosts\n * > and on markup that was originally created through these editing mechanisms\n * > to the value 'pre-wrap'. Default HTML whitespace handling is not well\n * > suited to WYSIWYG editing, and line wrapping will not work correctly in\n * > some corner cases if 'white-space' is left at its default value.\n *\n * https://html.spec.whatwg.org/multipage/interaction.html#best-practices-for-in-page-editors\n *\n * @type {string}\n */\nconst whiteSpace = 'pre-wrap';\n\n/**\n * A minimum width of 1px will prevent the rich text container from collapsing\n * to 0 width and hiding the caret. This is useful for inline containers.\n */\nconst minWidth = '1px';\n\nexport function useDefaultStyle() {\n\treturn useCallback( ( element ) => {\n\t\tif ( ! element ) {\n\t\t\treturn;\n\t\t}\n\t\telement.style.whiteSpace = whiteSpace;\n\t\telement.style.minWidth = minWidth;\n\t}, [] );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,WAAW,QAAQ,oBAAoB;;AAEhD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,MAAMC,UAAU,GAAG,UAAU;;AAE7B;AACA;AACA;AACA;AACA,MAAMC,QAAQ,GAAG,KAAK;AAEtB,OAAO,SAASC,eAAeA,CAAA,EAAG;EACjC,OAAOH,WAAW,CAAII,OAAO,IAAM;IAClC,IAAK,CAAEA,OAAO,EAAG;MAChB;IACD;IACAA,OAAO,CAACC,KAAK,CAACJ,UAAU,GAAGA,UAAU;IACrCG,OAAO,CAACC,KAAK,CAACH,QAAQ,GAAGA,QAAQ;EAClC,CAAC,EAAE,EAAG,CAAC;AACR","ignoreList":[]}

View File

@@ -0,0 +1,37 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
import { create } from './create';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Concats a pair of rich text values. Not that this mutates `a` and does NOT
* normalise formats!
*
* @param {Object} a Value to mutate.
* @param {Object} b Value to add read from.
*
* @return {Object} `a`, mutated.
*/
export function mergePair(a, b) {
a.formats = a.formats.concat(b.formats);
a.replacements = a.replacements.concat(b.replacements);
a.text += b.text;
return a;
}
/**
* Combine all Rich Text values into one. This is similar to
* `String.prototype.concat`.
*
* @param {...RichTextValue} values Objects to combine.
*
* @return {RichTextValue} A new value combining all given records.
*/
export function concat(...values) {
return normaliseFormats(values.reduce(mergePair, create()));
}
//# sourceMappingURL=concat.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["normaliseFormats","create","mergePair","a","b","formats","concat","replacements","text","values","reduce"],"sources":["@wordpress/rich-text/src/concat.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { normaliseFormats } from './normalise-formats';\nimport { create } from './create';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Concats a pair of rich text values. Not that this mutates `a` and does NOT\n * normalise formats!\n *\n * @param {Object} a Value to mutate.\n * @param {Object} b Value to add read from.\n *\n * @return {Object} `a`, mutated.\n */\nexport function mergePair( a, b ) {\n\ta.formats = a.formats.concat( b.formats );\n\ta.replacements = a.replacements.concat( b.replacements );\n\ta.text += b.text;\n\n\treturn a;\n}\n\n/**\n * Combine all Rich Text values into one. This is similar to\n * `String.prototype.concat`.\n *\n * @param {...RichTextValue} values Objects to combine.\n *\n * @return {RichTextValue} A new value combining all given records.\n */\nexport function concat( ...values ) {\n\treturn normaliseFormats( values.reduce( mergePair, create() ) );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,qBAAqB;AACtD,SAASC,MAAM,QAAQ,UAAU;;AAEjC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,SAASA,CAAEC,CAAC,EAAEC,CAAC,EAAG;EACjCD,CAAC,CAACE,OAAO,GAAGF,CAAC,CAACE,OAAO,CAACC,MAAM,CAAEF,CAAC,CAACC,OAAQ,CAAC;EACzCF,CAAC,CAACI,YAAY,GAAGJ,CAAC,CAACI,YAAY,CAACD,MAAM,CAAEF,CAAC,CAACG,YAAa,CAAC;EACxDJ,CAAC,CAACK,IAAI,IAAIJ,CAAC,CAACI,IAAI;EAEhB,OAAOL,CAAC;AACT;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASG,MAAMA,CAAE,GAAGG,MAAM,EAAG;EACnC,OAAOT,gBAAgB,CAAES,MAAM,CAACC,MAAM,CAAER,SAAS,EAAED,MAAM,CAAC,CAAE,CAAE,CAAC;AAChE","ignoreList":[]}

View File

@@ -0,0 +1,26 @@
/**
* Parse the given HTML into a body element.
*
* Note: The current implementation will return a shared reference, reset on
* each call to `createElement`. Therefore, you should not hold a reference to
* the value to operate upon asynchronously, as it may have unexpected results.
*
* @param {HTMLDocument} document The HTML document to use to parse.
* @param {string} html The HTML to parse.
*
* @return {HTMLBodyElement} Body element with parsed HTML.
*/
export function createElement({
implementation
}, html) {
// Because `createHTMLDocument` is an expensive operation, and with this
// function being internal to `rich-text` (full control in avoiding a risk
// of asynchronous operations on the shared reference), a single document
// is reused and reset for each call to the function.
if (!createElement.body) {
createElement.body = implementation.createHTMLDocument('').body;
}
createElement.body.innerHTML = html;
return createElement.body;
}
//# sourceMappingURL=create-element.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["createElement","implementation","html","body","createHTMLDocument","innerHTML"],"sources":["@wordpress/rich-text/src/create-element.js"],"sourcesContent":["/**\n * Parse the given HTML into a body element.\n *\n * Note: The current implementation will return a shared reference, reset on\n * each call to `createElement`. Therefore, you should not hold a reference to\n * the value to operate upon asynchronously, as it may have unexpected results.\n *\n * @param {HTMLDocument} document The HTML document to use to parse.\n * @param {string} html The HTML to parse.\n *\n * @return {HTMLBodyElement} Body element with parsed HTML.\n */\nexport function createElement( { implementation }, html ) {\n\t// Because `createHTMLDocument` is an expensive operation, and with this\n\t// function being internal to `rich-text` (full control in avoiding a risk\n\t// of asynchronous operations on the shared reference), a single document\n\t// is reused and reset for each call to the function.\n\tif ( ! createElement.body ) {\n\t\tcreateElement.body = implementation.createHTMLDocument( '' ).body;\n\t}\n\n\tcreateElement.body.innerHTML = html;\n\n\treturn createElement.body;\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAAE;EAAEC;AAAe,CAAC,EAAEC,IAAI,EAAG;EACzD;EACA;EACA;EACA;EACA,IAAK,CAAEF,aAAa,CAACG,IAAI,EAAG;IAC3BH,aAAa,CAACG,IAAI,GAAGF,cAAc,CAACG,kBAAkB,CAAE,EAAG,CAAC,CAACD,IAAI;EAClE;EAEAH,aAAa,CAACG,IAAI,CAACE,SAAS,GAAGH,IAAI;EAEnC,OAAOF,aAAa,CAACG,IAAI;AAC1B","ignoreList":[]}

View File

@@ -0,0 +1,585 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
import { createElement } from './create-element';
import { mergePair } from './concat';
import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP } from './special-characters';
import { toHTMLString } from './to-html-string';
import { getTextContent } from './get-text-content';
/** @typedef {import('./types').RichTextValue} RichTextValue */
function createEmptyValue() {
return {
formats: [],
replacements: [],
text: ''
};
}
function toFormat({
tagName,
attributes
}) {
let formatType;
if (attributes && attributes.class) {
formatType = select(richTextStore).getFormatTypeForClassName(attributes.class);
if (formatType) {
// Preserve any additional classes.
attributes.class = ` ${attributes.class} `.replace(` ${formatType.className} `, ' ').trim();
if (!attributes.class) {
delete attributes.class;
}
}
}
if (!formatType) {
formatType = select(richTextStore).getFormatTypeForBareElement(tagName);
}
if (!formatType) {
return attributes ? {
type: tagName,
attributes
} : {
type: tagName
};
}
if (formatType.__experimentalCreatePrepareEditableTree && !formatType.__experimentalCreateOnChangeEditableValue) {
return null;
}
if (!attributes) {
return {
formatType,
type: formatType.name,
tagName
};
}
const registeredAttributes = {};
const unregisteredAttributes = {};
const _attributes = {
...attributes
};
for (const key in formatType.attributes) {
const name = formatType.attributes[key];
registeredAttributes[key] = _attributes[name];
// delete the attribute and what's left is considered
// to be unregistered.
delete _attributes[name];
if (typeof registeredAttributes[key] === 'undefined') {
delete registeredAttributes[key];
}
}
for (const name in _attributes) {
unregisteredAttributes[name] = attributes[name];
}
if (formatType.contentEditable === false) {
delete unregisteredAttributes.contenteditable;
}
return {
formatType,
type: formatType.name,
tagName,
attributes: registeredAttributes,
unregisteredAttributes
};
}
/**
* The RichTextData class is used to instantiate a wrapper around rich text
* values, with methods that can be used to transform or manipulate the data.
*
* - Create an empty instance: `new RichTextData()`.
* - Create one from an HTML string: `RichTextData.fromHTMLString(
* '<em>hello</em>' )`.
* - Create one from a wrapper HTMLElement: `RichTextData.fromHTMLElement(
* document.querySelector( 'p' ) )`.
* - Create one from plain text: `RichTextData.fromPlainText( '1\n2' )`.
* - Create one from a rich text value: `new RichTextData( { text: '...',
* formats: [ ... ] } )`.
*
* @todo Add methods to manipulate the data, such as applyFormat, slice etc.
*/
export class RichTextData {
#value;
static empty() {
return new RichTextData();
}
static fromPlainText(text) {
return new RichTextData(create({
text
}));
}
static fromHTMLString(html) {
return new RichTextData(create({
html
}));
}
static fromHTMLElement(htmlElement, options = {}) {
const {
preserveWhiteSpace = false
} = options;
const element = preserveWhiteSpace ? htmlElement : collapseWhiteSpace(htmlElement);
const richTextData = new RichTextData(create({
element
}));
Object.defineProperty(richTextData, 'originalHTML', {
value: htmlElement.innerHTML
});
return richTextData;
}
constructor(init = createEmptyValue()) {
this.#value = init;
}
toPlainText() {
return getTextContent(this.#value);
}
// We could expose `toHTMLElement` at some point as well, but we'd only use
// it internally.
toHTMLString({
preserveWhiteSpace
} = {}) {
return this.originalHTML || toHTMLString({
value: this.#value,
preserveWhiteSpace
});
}
valueOf() {
return this.toHTMLString();
}
toString() {
return this.toHTMLString();
}
toJSON() {
return this.toHTMLString();
}
get length() {
return this.text.length;
}
get formats() {
return this.#value.formats;
}
get replacements() {
return this.#value.replacements;
}
get text() {
return this.#value.text;
}
}
for (const name of Object.getOwnPropertyNames(String.prototype)) {
if (RichTextData.prototype.hasOwnProperty(name)) {
continue;
}
Object.defineProperty(RichTextData.prototype, name, {
value(...args) {
// Should we convert back to RichTextData?
return this.toHTMLString()[name](...args);
}
});
}
/**
* Create a RichText value from an `Element` tree (DOM), an HTML string or a
* plain text string, with optionally a `Range` object to set the selection. If
* called without any input, an empty value will be created. The optional
* functions can be used to filter out content.
*
* A value will have the following shape, which you are strongly encouraged not
* to modify without the use of helper functions:
*
* ```js
* {
* text: string,
* formats: Array,
* replacements: Array,
* ?start: number,
* ?end: number,
* }
* ```
*
* As you can see, text and formatting are separated. `text` holds the text,
* including any replacement characters for objects and lines. `formats`,
* `objects` and `lines` are all sparse arrays of the same length as `text`. It
* holds information about the formatting at the relevant text indices. Finally
* `start` and `end` state which text indices are selected. They are only
* provided if a `Range` was given.
*
* @param {Object} [$1] Optional named arguments.
* @param {Element} [$1.element] Element to create value from.
* @param {string} [$1.text] Text to create value from.
* @param {string} [$1.html] HTML to create value from.
* @param {Range} [$1.range] Range to create value from.
* @param {boolean} [$1.__unstableIsEditableTree]
* @return {RichTextValue} A rich text value.
*/
export function create({
element,
text,
html,
range,
__unstableIsEditableTree: isEditableTree
} = {}) {
if (html instanceof RichTextData) {
return {
text: html.text,
formats: html.formats,
replacements: html.replacements
};
}
if (typeof text === 'string' && text.length > 0) {
return {
formats: Array(text.length),
replacements: Array(text.length),
text
};
}
if (typeof html === 'string' && html.length > 0) {
// It does not matter which document this is, we're just using it to
// parse.
element = createElement(document, html);
}
if (typeof element !== 'object') {
return createEmptyValue();
}
return createFromElement({
element,
range,
isEditableTree
});
}
/**
* Helper to accumulate the value's selection start and end from the current
* node and range.
*
* @param {Object} accumulator Object to accumulate into.
* @param {Node} node Node to create value with.
* @param {Range} range Range to create value with.
* @param {Object} value Value that is being accumulated.
*/
function accumulateSelection(accumulator, node, range, value) {
if (!range) {
return;
}
const {
parentNode
} = node;
const {
startContainer,
startOffset,
endContainer,
endOffset
} = range;
const currentLength = accumulator.text.length;
// Selection can be extracted from value.
if (value.start !== undefined) {
accumulator.start = currentLength + value.start;
// Range indicates that the current node has selection.
} else if (node === startContainer && node.nodeType === node.TEXT_NODE) {
accumulator.start = currentLength + startOffset;
// Range indicates that the current node is selected.
} else if (parentNode === startContainer && node === startContainer.childNodes[startOffset]) {
accumulator.start = currentLength;
// Range indicates that the selection is after the current node.
} else if (parentNode === startContainer && node === startContainer.childNodes[startOffset - 1]) {
accumulator.start = currentLength + value.text.length;
// Fallback if no child inside handled the selection.
} else if (node === startContainer) {
accumulator.start = currentLength;
}
// Selection can be extracted from value.
if (value.end !== undefined) {
accumulator.end = currentLength + value.end;
// Range indicates that the current node has selection.
} else if (node === endContainer && node.nodeType === node.TEXT_NODE) {
accumulator.end = currentLength + endOffset;
// Range indicates that the current node is selected.
} else if (parentNode === endContainer && node === endContainer.childNodes[endOffset - 1]) {
accumulator.end = currentLength + value.text.length;
// Range indicates that the selection is before the current node.
} else if (parentNode === endContainer && node === endContainer.childNodes[endOffset]) {
accumulator.end = currentLength;
// Fallback if no child inside handled the selection.
} else if (node === endContainer) {
accumulator.end = currentLength + endOffset;
}
}
/**
* Adjusts the start and end offsets from a range based on a text filter.
*
* @param {Node} node Node of which the text should be filtered.
* @param {Range} range The range to filter.
* @param {Function} filter Function to use to filter the text.
*
* @return {Object|void} Object containing range properties.
*/
function filterRange(node, range, filter) {
if (!range) {
return;
}
const {
startContainer,
endContainer
} = range;
let {
startOffset,
endOffset
} = range;
if (node === startContainer) {
startOffset = filter(node.nodeValue.slice(0, startOffset)).length;
}
if (node === endContainer) {
endOffset = filter(node.nodeValue.slice(0, endOffset)).length;
}
return {
startContainer,
startOffset,
endContainer,
endOffset
};
}
/**
* Collapse any whitespace used for HTML formatting to one space character,
* because it will also be displayed as such by the browser.
*
* We need to strip it from the content because we use white-space: pre-wrap for
* displaying editable rich text. Without using white-space: pre-wrap, the
* browser will litter the content with non breaking spaces, among other issues.
* See packages/rich-text/src/component/use-default-style.js.
*
* @see
* https://developer.mozilla.org/en-US/docs/Web/CSS/white-space-collapse#collapsing_of_white_space
*
* @param {HTMLElement} element
* @param {boolean} isRoot
*
* @return {HTMLElement} New element with collapsed whitespace.
*/
function collapseWhiteSpace(element, isRoot = true) {
const clone = element.cloneNode(true);
clone.normalize();
Array.from(clone.childNodes).forEach((node, i, nodes) => {
if (node.nodeType === node.TEXT_NODE) {
let newNodeValue = node.nodeValue;
if (/[\n\t\r\f]/.test(newNodeValue)) {
newNodeValue = newNodeValue.replace(/[\n\t\r\f]+/g, ' ');
}
if (newNodeValue.indexOf(' ') !== -1) {
newNodeValue = newNodeValue.replace(/ {2,}/g, ' ');
}
if (i === 0 && newNodeValue.startsWith(' ')) {
newNodeValue = newNodeValue.slice(1);
} else if (isRoot && i === nodes.length - 1 && newNodeValue.endsWith(' ')) {
newNodeValue = newNodeValue.slice(0, -1);
}
node.nodeValue = newNodeValue;
} else if (node.nodeType === node.ELEMENT_NODE) {
collapseWhiteSpace(node, false);
}
});
return clone;
}
/**
* We need to normalise line breaks to `\n` so they are consistent across
* platforms and serialised properly. Not removing \r would cause it to
* linger and result in double line breaks when whitespace is preserved.
*/
const CARRIAGE_RETURN = '\r';
/**
* Removes reserved characters used by rich-text (zero width non breaking spaces
* added by `toTree` and object replacement characters).
*
* @param {string} string
*/
export function removeReservedCharacters(string) {
// with the global flag, note that we should create a new regex each time OR
// reset lastIndex state.
return string.replace(new RegExp(`[${ZWNBSP}${OBJECT_REPLACEMENT_CHARACTER}${CARRIAGE_RETURN}]`, 'gu'), '');
}
/**
* Creates a Rich Text value from a DOM element and range.
*
* @param {Object} $1 Named argements.
* @param {Element} [$1.element] Element to create value from.
* @param {Range} [$1.range] Range to create value from.
* @param {boolean} [$1.isEditableTree]
*
* @return {RichTextValue} A rich text value.
*/
function createFromElement({
element,
range,
isEditableTree
}) {
const accumulator = createEmptyValue();
if (!element) {
return accumulator;
}
if (!element.hasChildNodes()) {
accumulateSelection(accumulator, element, range, createEmptyValue());
return accumulator;
}
const length = element.childNodes.length;
// Optimise for speed.
for (let index = 0; index < length; index++) {
const node = element.childNodes[index];
const tagName = node.nodeName.toLowerCase();
if (node.nodeType === node.TEXT_NODE) {
const text = removeReservedCharacters(node.nodeValue);
range = filterRange(node, range, removeReservedCharacters);
accumulateSelection(accumulator, node, range, {
text
});
// Create a sparse array of the same length as `text`, in which
// formats can be added.
accumulator.formats.length += text.length;
accumulator.replacements.length += text.length;
accumulator.text += text;
continue;
}
if (node.nodeType !== node.ELEMENT_NODE) {
continue;
}
if (isEditableTree &&
// Ignore any line breaks that are not inserted by us.
tagName === 'br' && !node.getAttribute('data-rich-text-line-break')) {
accumulateSelection(accumulator, node, range, createEmptyValue());
continue;
}
if (tagName === 'script') {
const value = {
formats: [,],
replacements: [{
type: tagName,
attributes: {
'data-rich-text-script': node.getAttribute('data-rich-text-script') || encodeURIComponent(node.innerHTML)
}
}],
text: OBJECT_REPLACEMENT_CHARACTER
};
accumulateSelection(accumulator, node, range, value);
mergePair(accumulator, value);
continue;
}
if (tagName === 'br') {
accumulateSelection(accumulator, node, range, createEmptyValue());
mergePair(accumulator, create({
text: '\n'
}));
continue;
}
const format = toFormat({
tagName,
attributes: getAttributes({
element: node
})
});
// When a format type is declared as not editable, replace it with an
// object replacement character and preserve the inner HTML.
if (format?.formatType?.contentEditable === false) {
delete format.formatType;
accumulateSelection(accumulator, node, range, createEmptyValue());
mergePair(accumulator, {
formats: [,],
replacements: [{
...format,
innerHTML: node.innerHTML
}],
text: OBJECT_REPLACEMENT_CHARACTER
});
continue;
}
if (format) {
delete format.formatType;
}
const value = createFromElement({
element: node,
range,
isEditableTree
});
accumulateSelection(accumulator, node, range, value);
// Ignore any placeholders, but keep their content since the browser
// might insert text inside them when the editable element is flex.
if (!format || node.getAttribute('data-rich-text-placeholder')) {
mergePair(accumulator, value);
} else if (value.text.length === 0) {
if (format.attributes) {
mergePair(accumulator, {
formats: [,],
replacements: [format],
text: OBJECT_REPLACEMENT_CHARACTER
});
}
} else {
// Indices should share a reference to the same formats array.
// Only create a new reference if `formats` changes.
function mergeFormats(formats) {
if (mergeFormats.formats === formats) {
return mergeFormats.newFormats;
}
const newFormats = formats ? [format, ...formats] : [format];
mergeFormats.formats = formats;
mergeFormats.newFormats = newFormats;
return newFormats;
}
// Since the formats parameter can be `undefined`, preset
// `mergeFormats` with a new reference.
mergeFormats.newFormats = [format];
mergePair(accumulator, {
...value,
formats: Array.from(value.formats, mergeFormats)
});
}
}
return accumulator;
}
/**
* Gets the attributes of an element in object shape.
*
* @param {Object} $1 Named argements.
* @param {Element} $1.element Element to get attributes from.
*
* @return {Object|void} Attribute object or `undefined` if the element has no
* attributes.
*/
function getAttributes({
element
}) {
if (!element.hasAttributes()) {
return;
}
const length = element.attributes.length;
let accumulator;
// Optimise for speed.
for (let i = 0; i < length; i++) {
const {
name,
value
} = element.attributes[i];
if (name.indexOf('data-rich-text-') === 0) {
continue;
}
const safeName = /^on/i.test(name) ? 'data-disable-rich-text-' + name : name;
accumulator = accumulator || {};
accumulator[safeName] = value;
}
return accumulator;
}
//# sourceMappingURL=create.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,26 @@
/**
* Internal dependencies
*/
import { getActiveFormats } from './get-active-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Gets the format object by type at the start of the selection. This can be
* used to get e.g. the URL of a link format at the current selection, but also
* to check if a format is active at the selection. Returns undefined if there
* is no format at the selection.
*
* @param {RichTextValue} value Value to inspect.
* @param {string} formatType Format type to look for.
*
* @return {RichTextFormat|undefined} Active format object of the specified
* type, or undefined.
*/
export function getActiveFormat(value, formatType) {
return getActiveFormats(value).find(({
type
}) => type === formatType);
}
//# sourceMappingURL=get-active-format.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["getActiveFormats","getActiveFormat","value","formatType","find","type"],"sources":["@wordpress/rich-text/src/get-active-format.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { getActiveFormats } from './get-active-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Gets the format object by type at the start of the selection. This can be\n * used to get e.g. the URL of a link format at the current selection, but also\n * to check if a format is active at the selection. Returns undefined if there\n * is no format at the selection.\n *\n * @param {RichTextValue} value Value to inspect.\n * @param {string} formatType Format type to look for.\n *\n * @return {RichTextFormat|undefined} Active format object of the specified\n * type, or undefined.\n */\nexport function getActiveFormat( value, formatType ) {\n\treturn getActiveFormats( value ).find(\n\t\t( { type } ) => type === formatType\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,gBAAgB,QAAQ,sBAAsB;;AAEvD;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAEC,KAAK,EAAEC,UAAU,EAAG;EACpD,OAAOH,gBAAgB,CAAEE,KAAM,CAAC,CAACE,IAAI,CACpC,CAAE;IAAEC;EAAK,CAAC,KAAMA,IAAI,KAAKF,UAC1B,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,83 @@
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormatList} RichTextFormatList */
/**
* Internal dependencies
*/
import { isFormatEqual } from './is-format-equal';
/**
* Gets the all format objects at the start of the selection.
*
* @param {RichTextValue} value Value to inspect.
* @param {Array} EMPTY_ACTIVE_FORMATS Array to return if there are no
* active formats.
*
* @return {RichTextFormatList} Active format objects.
*/
export function getActiveFormats(value, EMPTY_ACTIVE_FORMATS = []) {
const {
formats,
start,
end,
activeFormats
} = value;
if (start === undefined) {
return EMPTY_ACTIVE_FORMATS;
}
if (start === end) {
// For a collapsed caret, it is possible to override the active formats.
if (activeFormats) {
return activeFormats;
}
const formatsBefore = formats[start - 1] || EMPTY_ACTIVE_FORMATS;
const formatsAfter = formats[start] || EMPTY_ACTIVE_FORMATS;
// By default, select the lowest amount of formats possible (which means
// the caret is positioned outside the format boundary). The user can
// then use arrow keys to define `activeFormats`.
if (formatsBefore.length < formatsAfter.length) {
return formatsBefore;
}
return formatsAfter;
}
// If there's no formats at the start index, there are not active formats.
if (!formats[start]) {
return EMPTY_ACTIVE_FORMATS;
}
const selectedFormats = formats.slice(start, end);
// Clone the formats so we're not mutating the live value.
const _activeFormats = [...selectedFormats[0]];
let i = selectedFormats.length;
// For performance reasons, start from the end where it's much quicker to
// realise that there are no active formats.
while (i--) {
const formatsAtIndex = selectedFormats[i];
// If we run into any index without formats, we're sure that there's no
// active formats.
if (!formatsAtIndex) {
return EMPTY_ACTIVE_FORMATS;
}
let ii = _activeFormats.length;
// Loop over the active formats and remove any that are not present at
// the current index.
while (ii--) {
const format = _activeFormats[ii];
if (!formatsAtIndex.find(_format => isFormatEqual(format, _format))) {
_activeFormats.splice(ii, 1);
}
}
// If there are no active formats, we can stop.
if (_activeFormats.length === 0) {
return EMPTY_ACTIVE_FORMATS;
}
}
return _activeFormats || EMPTY_ACTIVE_FORMATS;
}
//# sourceMappingURL=get-active-formats.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","getActiveFormats","value","EMPTY_ACTIVE_FORMATS","formats","start","end","activeFormats","undefined","formatsBefore","formatsAfter","length","selectedFormats","slice","_activeFormats","i","formatsAtIndex","ii","format","find","_format","splice"],"sources":["@wordpress/rich-text/src/get-active-formats.js"],"sourcesContent":["/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormatList} RichTextFormatList */\n\n/**\n * Internal dependencies\n */\nimport { isFormatEqual } from './is-format-equal';\n\n/**\n * Gets the all format objects at the start of the selection.\n *\n * @param {RichTextValue} value Value to inspect.\n * @param {Array} EMPTY_ACTIVE_FORMATS Array to return if there are no\n * active formats.\n *\n * @return {RichTextFormatList} Active format objects.\n */\nexport function getActiveFormats( value, EMPTY_ACTIVE_FORMATS = [] ) {\n\tconst { formats, start, end, activeFormats } = value;\n\tif ( start === undefined ) {\n\t\treturn EMPTY_ACTIVE_FORMATS;\n\t}\n\n\tif ( start === end ) {\n\t\t// For a collapsed caret, it is possible to override the active formats.\n\t\tif ( activeFormats ) {\n\t\t\treturn activeFormats;\n\t\t}\n\n\t\tconst formatsBefore = formats[ start - 1 ] || EMPTY_ACTIVE_FORMATS;\n\t\tconst formatsAfter = formats[ start ] || EMPTY_ACTIVE_FORMATS;\n\n\t\t// By default, select the lowest amount of formats possible (which means\n\t\t// the caret is positioned outside the format boundary). The user can\n\t\t// then use arrow keys to define `activeFormats`.\n\t\tif ( formatsBefore.length < formatsAfter.length ) {\n\t\t\treturn formatsBefore;\n\t\t}\n\n\t\treturn formatsAfter;\n\t}\n\n\t// If there's no formats at the start index, there are not active formats.\n\tif ( ! formats[ start ] ) {\n\t\treturn EMPTY_ACTIVE_FORMATS;\n\t}\n\n\tconst selectedFormats = formats.slice( start, end );\n\n\t// Clone the formats so we're not mutating the live value.\n\tconst _activeFormats = [ ...selectedFormats[ 0 ] ];\n\tlet i = selectedFormats.length;\n\n\t// For performance reasons, start from the end where it's much quicker to\n\t// realise that there are no active formats.\n\twhile ( i-- ) {\n\t\tconst formatsAtIndex = selectedFormats[ i ];\n\n\t\t// If we run into any index without formats, we're sure that there's no\n\t\t// active formats.\n\t\tif ( ! formatsAtIndex ) {\n\t\t\treturn EMPTY_ACTIVE_FORMATS;\n\t\t}\n\n\t\tlet ii = _activeFormats.length;\n\n\t\t// Loop over the active formats and remove any that are not present at\n\t\t// the current index.\n\t\twhile ( ii-- ) {\n\t\t\tconst format = _activeFormats[ ii ];\n\n\t\t\tif (\n\t\t\t\t! formatsAtIndex.find( ( _format ) =>\n\t\t\t\t\tisFormatEqual( format, _format )\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\t_activeFormats.splice( ii, 1 );\n\t\t\t}\n\t\t}\n\n\t\t// If there are no active formats, we can stop.\n\t\tif ( _activeFormats.length === 0 ) {\n\t\t\treturn EMPTY_ACTIVE_FORMATS;\n\t\t}\n\t}\n\n\treturn _activeFormats || EMPTY_ACTIVE_FORMATS;\n}\n"],"mappings":"AAAA;AACA;;AAEA;AACA;AACA;AACA,SAASA,aAAa,QAAQ,mBAAmB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAEC,KAAK,EAAEC,oBAAoB,GAAG,EAAE,EAAG;EACpE,MAAM;IAAEC,OAAO;IAAEC,KAAK;IAAEC,GAAG;IAAEC;EAAc,CAAC,GAAGL,KAAK;EACpD,IAAKG,KAAK,KAAKG,SAAS,EAAG;IAC1B,OAAOL,oBAAoB;EAC5B;EAEA,IAAKE,KAAK,KAAKC,GAAG,EAAG;IACpB;IACA,IAAKC,aAAa,EAAG;MACpB,OAAOA,aAAa;IACrB;IAEA,MAAME,aAAa,GAAGL,OAAO,CAAEC,KAAK,GAAG,CAAC,CAAE,IAAIF,oBAAoB;IAClE,MAAMO,YAAY,GAAGN,OAAO,CAAEC,KAAK,CAAE,IAAIF,oBAAoB;;IAE7D;IACA;IACA;IACA,IAAKM,aAAa,CAACE,MAAM,GAAGD,YAAY,CAACC,MAAM,EAAG;MACjD,OAAOF,aAAa;IACrB;IAEA,OAAOC,YAAY;EACpB;;EAEA;EACA,IAAK,CAAEN,OAAO,CAAEC,KAAK,CAAE,EAAG;IACzB,OAAOF,oBAAoB;EAC5B;EAEA,MAAMS,eAAe,GAAGR,OAAO,CAACS,KAAK,CAAER,KAAK,EAAEC,GAAI,CAAC;;EAEnD;EACA,MAAMQ,cAAc,GAAG,CAAE,GAAGF,eAAe,CAAE,CAAC,CAAE,CAAE;EAClD,IAAIG,CAAC,GAAGH,eAAe,CAACD,MAAM;;EAE9B;EACA;EACA,OAAQI,CAAC,EAAE,EAAG;IACb,MAAMC,cAAc,GAAGJ,eAAe,CAAEG,CAAC,CAAE;;IAE3C;IACA;IACA,IAAK,CAAEC,cAAc,EAAG;MACvB,OAAOb,oBAAoB;IAC5B;IAEA,IAAIc,EAAE,GAAGH,cAAc,CAACH,MAAM;;IAE9B;IACA;IACA,OAAQM,EAAE,EAAE,EAAG;MACd,MAAMC,MAAM,GAAGJ,cAAc,CAAEG,EAAE,CAAE;MAEnC,IACC,CAAED,cAAc,CAACG,IAAI,CAAIC,OAAO,IAC/BpB,aAAa,CAAEkB,MAAM,EAAEE,OAAQ,CAChC,CAAC,EACA;QACDN,cAAc,CAACO,MAAM,CAAEJ,EAAE,EAAE,CAAE,CAAC;MAC/B;IACD;;IAEA;IACA,IAAKH,cAAc,CAACH,MAAM,KAAK,CAAC,EAAG;MAClC,OAAOR,oBAAoB;IAC5B;EACD;EAEA,OAAOW,cAAc,IAAIX,oBAAoB;AAC9C","ignoreList":[]}

View File

@@ -0,0 +1,28 @@
/**
* Internal dependencies
*/
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Gets the active object, if there is any.
*
* @param {RichTextValue} value Value to inspect.
*
* @return {RichTextFormat|void} Active object, or undefined.
*/
export function getActiveObject({
start,
end,
replacements,
text
}) {
if (start + 1 !== end || text[start] !== OBJECT_REPLACEMENT_CHARACTER) {
return;
}
return replacements[start];
}
//# sourceMappingURL=get-active-object.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["OBJECT_REPLACEMENT_CHARACTER","getActiveObject","start","end","replacements","text"],"sources":["@wordpress/rich-text/src/get-active-object.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Gets the active object, if there is any.\n *\n * @param {RichTextValue} value Value to inspect.\n *\n * @return {RichTextFormat|void} Active object, or undefined.\n */\nexport function getActiveObject( { start, end, replacements, text } ) {\n\tif ( start + 1 !== end || text[ start ] !== OBJECT_REPLACEMENT_CHARACTER ) {\n\t\treturn;\n\t}\n\n\treturn replacements[ start ];\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,4BAA4B,QAAQ,sBAAsB;;AAEnE;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,eAAeA,CAAE;EAAEC,KAAK;EAAEC,GAAG;EAAEC,YAAY;EAAEC;AAAK,CAAC,EAAG;EACrE,IAAKH,KAAK,GAAG,CAAC,KAAKC,GAAG,IAAIE,IAAI,CAAEH,KAAK,CAAE,KAAKF,4BAA4B,EAAG;IAC1E;EACD;EAEA,OAAOI,YAAY,CAAEF,KAAK,CAAE;AAC7B","ignoreList":[]}

View File

@@ -0,0 +1,22 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */
/**
* Returns a registered format type.
*
* @param {string} name Format name.
*
* @return {RichTextFormatType|undefined} Format type.
*/
export function getFormatType(name) {
return select(richTextStore).getFormatType(name);
}
//# sourceMappingURL=get-format-type.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["select","store","richTextStore","getFormatType","name"],"sources":["@wordpress/rich-text/src/get-format-type.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\n/**\n * Internal dependencies\n */\nimport { store as richTextStore } from './store';\n\n/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */\n\n/**\n * Returns a registered format type.\n *\n * @param {string} name Format name.\n *\n * @return {RichTextFormatType|undefined} Format type.\n */\nexport function getFormatType( name ) {\n\treturn select( richTextStore ).getFormatType( name );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,MAAM,QAAQ,iBAAiB;AACxC;AACA;AACA;AACA,SAASC,KAAK,IAAIC,aAAa,QAAQ,SAAS;;AAEhD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,aAAaA,CAAEC,IAAI,EAAG;EACrC,OAAOJ,MAAM,CAAEE,aAAc,CAAC,CAACC,aAAa,CAAEC,IAAK,CAAC;AACrD","ignoreList":[]}

View File

@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */
/**
* Returns all registered formats.
*
* @return {Array<RichTextFormatType>} Format settings.
*/
export function getFormatTypes() {
return select(richTextStore).getFormatTypes();
}
//# sourceMappingURL=get-format-types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["select","store","richTextStore","getFormatTypes"],"sources":["@wordpress/rich-text/src/get-format-types.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { select } from '@wordpress/data';\n/**\n * Internal dependencies\n */\nimport { store as richTextStore } from './store';\n\n/** @typedef {import('./register-format-type').RichTextFormatType} RichTextFormatType */\n\n/**\n * Returns all registered formats.\n *\n * @return {Array<RichTextFormatType>} Format settings.\n */\nexport function getFormatTypes() {\n\treturn select( richTextStore ).getFormatTypes();\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,MAAM,QAAQ,iBAAiB;AACxC;AACA;AACA;AACA,SAASC,KAAK,IAAIC,aAAa,QAAQ,SAAS;;AAEhD;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAAA,EAAG;EAChC,OAAOH,MAAM,CAAEE,aAAc,CAAC,CAACC,cAAc,CAAC,CAAC;AAChD","ignoreList":[]}

View File

@@ -0,0 +1,21 @@
/**
* Internal dependencies
*/
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Get the textual content of a Rich Text value. This is similar to
* `Element.textContent`.
*
* @param {RichTextValue} value Value to use.
*
* @return {string} The text content.
*/
export function getTextContent({
text
}) {
return text.replace(OBJECT_REPLACEMENT_CHARACTER, '');
}
//# sourceMappingURL=get-text-content.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["OBJECT_REPLACEMENT_CHARACTER","getTextContent","text","replace"],"sources":["@wordpress/rich-text/src/get-text-content.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Get the textual content of a Rich Text value. This is similar to\n * `Element.textContent`.\n *\n * @param {RichTextValue} value Value to use.\n *\n * @return {string} The text content.\n */\nexport function getTextContent( { text } ) {\n\treturn text.replace( OBJECT_REPLACEMENT_CHARACTER, '' );\n}\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,4BAA4B,QAAQ,sBAAsB;;AAEnE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,cAAcA,CAAE;EAAEC;AAAK,CAAC,EAAG;EAC1C,OAAOA,IAAI,CAACC,OAAO,CAAEH,4BAA4B,EAAE,EAAG,CAAC;AACxD","ignoreList":[]}

View File

@@ -0,0 +1,33 @@
export { store } from './store';
export { applyFormat } from './apply-format';
export { concat } from './concat';
export { RichTextData, create } from './create';
export { getActiveFormat } from './get-active-format';
export { getActiveFormats } from './get-active-formats';
export { getActiveObject } from './get-active-object';
export { getTextContent } from './get-text-content';
export { isCollapsed } from './is-collapsed';
export { isEmpty } from './is-empty';
export { join } from './join';
export { registerFormatType } from './register-format-type';
export { removeFormat } from './remove-format';
export { remove } from './remove';
export { replace } from './replace';
export { insert } from './insert';
export { insertObject } from './insert-object';
export { slice } from './slice';
export { split } from './split';
export { toDom as __unstableToDom } from './to-dom';
export { toHTMLString } from './to-html-string';
export { toggleFormat } from './toggle-format';
export { unregisterFormatType } from './unregister-format-type';
export { createElement as __unstableCreateElement } from './create-element';
export { useAnchorRef } from './component/use-anchor-ref';
export { useAnchor } from './component/use-anchor';
export { default as __experimentalRichText, useRichText as __unstableUseRichText } from './component';
/**
* An object which represents a formatted string. See main `@wordpress/rich-text`
* documentation for more information.
*/
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["store","applyFormat","concat","RichTextData","create","getActiveFormat","getActiveFormats","getActiveObject","getTextContent","isCollapsed","isEmpty","join","registerFormatType","removeFormat","remove","replace","insert","insertObject","slice","split","toDom","__unstableToDom","toHTMLString","toggleFormat","unregisterFormatType","createElement","__unstableCreateElement","useAnchorRef","useAnchor","default","__experimentalRichText","useRichText","__unstableUseRichText"],"sources":["@wordpress/rich-text/src/index.ts"],"sourcesContent":["export { store } from './store';\nexport { applyFormat } from './apply-format';\nexport { concat } from './concat';\nexport { RichTextData, create } from './create';\nexport { getActiveFormat } from './get-active-format';\nexport { getActiveFormats } from './get-active-formats';\nexport { getActiveObject } from './get-active-object';\nexport { getTextContent } from './get-text-content';\nexport { isCollapsed } from './is-collapsed';\nexport { isEmpty } from './is-empty';\nexport { join } from './join';\nexport { registerFormatType } from './register-format-type';\nexport { removeFormat } from './remove-format';\nexport { remove } from './remove';\nexport { replace } from './replace';\nexport { insert } from './insert';\nexport { insertObject } from './insert-object';\nexport { slice } from './slice';\nexport { split } from './split';\nexport { toDom as __unstableToDom } from './to-dom';\nexport { toHTMLString } from './to-html-string';\nexport { toggleFormat } from './toggle-format';\nexport { unregisterFormatType } from './unregister-format-type';\nexport { createElement as __unstableCreateElement } from './create-element';\n\nexport { useAnchorRef } from './component/use-anchor-ref';\nexport { useAnchor } from './component/use-anchor';\n\nexport {\n\tdefault as __experimentalRichText,\n\tuseRichText as __unstableUseRichText,\n} from './component';\n\n/**\n * An object which represents a formatted string. See main `@wordpress/rich-text`\n * documentation for more information.\n */\nexport type { RichTextValue } from './types';\n"],"mappings":"AAAA,SAASA,KAAK,QAAQ,SAAS;AAC/B,SAASC,WAAW,QAAQ,gBAAgB;AAC5C,SAASC,MAAM,QAAQ,UAAU;AACjC,SAASC,YAAY,EAAEC,MAAM,QAAQ,UAAU;AAC/C,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,gBAAgB,QAAQ,sBAAsB;AACvD,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,cAAc,QAAQ,oBAAoB;AACnD,SAASC,WAAW,QAAQ,gBAAgB;AAC5C,SAASC,OAAO,QAAQ,YAAY;AACpC,SAASC,IAAI,QAAQ,QAAQ;AAC7B,SAASC,kBAAkB,QAAQ,wBAAwB;AAC3D,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,MAAM,QAAQ,UAAU;AACjC,SAASC,OAAO,QAAQ,WAAW;AACnC,SAASC,MAAM,QAAQ,UAAU;AACjC,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,KAAK,QAAQ,SAAS;AAC/B,SAASC,KAAK,QAAQ,SAAS;AAC/B,SAASC,KAAK,IAAIC,eAAe,QAAQ,UAAU;AACnD,SAASC,YAAY,QAAQ,kBAAkB;AAC/C,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,oBAAoB,QAAQ,0BAA0B;AAC/D,SAASC,aAAa,IAAIC,uBAAuB,QAAQ,kBAAkB;AAE3E,SAASC,YAAY,QAAQ,4BAA4B;AACzD,SAASC,SAAS,QAAQ,wBAAwB;AAElD,SACCC,OAAO,IAAIC,sBAAsB,EACjCC,WAAW,IAAIC,qBAAqB,QAC9B,aAAa;;AAEpB;AACA;AACA;AACA","ignoreList":[]}

View File

@@ -0,0 +1,31 @@
/**
* Internal dependencies
*/
import { insert } from './insert';
import { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Insert a format as an object into a Rich Text value at the given
* `startIndex`. Any content between `startIndex` and `endIndex` will be
* removed. Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} formatToInsert Format to insert as object.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the object inserted.
*/
export function insertObject(value, formatToInsert, startIndex, endIndex) {
const valueToInsert = {
formats: [,],
replacements: [formatToInsert],
text: OBJECT_REPLACEMENT_CHARACTER
};
return insert(value, valueToInsert, startIndex, endIndex);
}
//# sourceMappingURL=insert-object.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["insert","OBJECT_REPLACEMENT_CHARACTER","insertObject","value","formatToInsert","startIndex","endIndex","valueToInsert","formats","replacements","text"],"sources":["@wordpress/rich-text/src/insert-object.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { insert } from './insert';\nimport { OBJECT_REPLACEMENT_CHARACTER } from './special-characters';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Insert a format as an object into a Rich Text value at the given\n * `startIndex`. Any content between `startIndex` and `endIndex` will be\n * removed. Indices are retrieved from the selection if none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {RichTextFormat} formatToInsert Format to insert as object.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the object inserted.\n */\nexport function insertObject( value, formatToInsert, startIndex, endIndex ) {\n\tconst valueToInsert = {\n\t\tformats: [ , ],\n\t\treplacements: [ formatToInsert ],\n\t\ttext: OBJECT_REPLACEMENT_CHARACTER,\n\t};\n\n\treturn insert( value, valueToInsert, startIndex, endIndex );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,4BAA4B,QAAQ,sBAAsB;;AAEnE;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAEC,KAAK,EAAEC,cAAc,EAAEC,UAAU,EAAEC,QAAQ,EAAG;EAC3E,MAAMC,aAAa,GAAG;IACrBC,OAAO,EAAE,GAAK;IACdC,YAAY,EAAE,CAAEL,cAAc,CAAE;IAChCM,IAAI,EAAET;EACP,CAAC;EAED,OAAOD,MAAM,CAAEG,KAAK,EAAEI,aAAa,EAAEF,UAAU,EAAEC,QAAS,CAAC;AAC5D","ignoreList":[]}

View File

@@ -0,0 +1,43 @@
/**
* Internal dependencies
*/
import { create } from './create';
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Insert a Rich Text value, an HTML string, or a plain text string, into a
* Rich Text value at the given `startIndex`. Any content between `startIndex`
* and `endIndex` will be removed. Indices are retrieved from the selection if
* none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextValue|string} valueToInsert Value to insert.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the value inserted.
*/
export function insert(value, valueToInsert, startIndex = value.start, endIndex = value.end) {
const {
formats,
replacements,
text
} = value;
if (typeof valueToInsert === 'string') {
valueToInsert = create({
text: valueToInsert
});
}
const index = startIndex + valueToInsert.text.length;
return normaliseFormats({
formats: formats.slice(0, startIndex).concat(valueToInsert.formats, formats.slice(endIndex)),
replacements: replacements.slice(0, startIndex).concat(valueToInsert.replacements, replacements.slice(endIndex)),
text: text.slice(0, startIndex) + valueToInsert.text + text.slice(endIndex),
start: index,
end: index
});
}
//# sourceMappingURL=insert.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["create","normaliseFormats","insert","value","valueToInsert","startIndex","start","endIndex","end","formats","replacements","text","index","length","slice","concat"],"sources":["@wordpress/rich-text/src/insert.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { create } from './create';\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Insert a Rich Text value, an HTML string, or a plain text string, into a\n * Rich Text value at the given `startIndex`. Any content between `startIndex`\n * and `endIndex` will be removed. Indices are retrieved from the selection if\n * none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {RichTextValue|string} valueToInsert Value to insert.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the value inserted.\n */\nexport function insert(\n\tvalue,\n\tvalueToInsert,\n\tstartIndex = value.start,\n\tendIndex = value.end\n) {\n\tconst { formats, replacements, text } = value;\n\n\tif ( typeof valueToInsert === 'string' ) {\n\t\tvalueToInsert = create( { text: valueToInsert } );\n\t}\n\n\tconst index = startIndex + valueToInsert.text.length;\n\n\treturn normaliseFormats( {\n\t\tformats: formats\n\t\t\t.slice( 0, startIndex )\n\t\t\t.concat( valueToInsert.formats, formats.slice( endIndex ) ),\n\t\treplacements: replacements\n\t\t\t.slice( 0, startIndex )\n\t\t\t.concat(\n\t\t\t\tvalueToInsert.replacements,\n\t\t\t\treplacements.slice( endIndex )\n\t\t\t),\n\t\ttext:\n\t\t\ttext.slice( 0, startIndex ) +\n\t\t\tvalueToInsert.text +\n\t\t\ttext.slice( endIndex ),\n\t\tstart: index,\n\t\tend: index,\n\t} );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CACrBC,KAAK,EACLC,aAAa,EACbC,UAAU,GAAGF,KAAK,CAACG,KAAK,EACxBC,QAAQ,GAAGJ,KAAK,CAACK,GAAG,EACnB;EACD,MAAM;IAAEC,OAAO;IAAEC,YAAY;IAAEC;EAAK,CAAC,GAAGR,KAAK;EAE7C,IAAK,OAAOC,aAAa,KAAK,QAAQ,EAAG;IACxCA,aAAa,GAAGJ,MAAM,CAAE;MAAEW,IAAI,EAAEP;IAAc,CAAE,CAAC;EAClD;EAEA,MAAMQ,KAAK,GAAGP,UAAU,GAAGD,aAAa,CAACO,IAAI,CAACE,MAAM;EAEpD,OAAOZ,gBAAgB,CAAE;IACxBQ,OAAO,EAAEA,OAAO,CACdK,KAAK,CAAE,CAAC,EAAET,UAAW,CAAC,CACtBU,MAAM,CAAEX,aAAa,CAACK,OAAO,EAAEA,OAAO,CAACK,KAAK,CAAEP,QAAS,CAAE,CAAC;IAC5DG,YAAY,EAAEA,YAAY,CACxBI,KAAK,CAAE,CAAC,EAAET,UAAW,CAAC,CACtBU,MAAM,CACNX,aAAa,CAACM,YAAY,EAC1BA,YAAY,CAACI,KAAK,CAAEP,QAAS,CAC9B,CAAC;IACFI,IAAI,EACHA,IAAI,CAACG,KAAK,CAAE,CAAC,EAAET,UAAW,CAAC,GAC3BD,aAAa,CAACO,IAAI,GAClBA,IAAI,CAACG,KAAK,CAAEP,QAAS,CAAC;IACvBD,KAAK,EAAEM,KAAK;IACZJ,GAAG,EAAEI;EACN,CAAE,CAAC;AACJ","ignoreList":[]}

View File

@@ -0,0 +1,25 @@
/**
* Internal dependencies
*/
/**
* Check if the selection of a Rich Text value is collapsed or not. Collapsed
* means that no characters are selected, but there is a caret present. If there
* is no selection, `undefined` will be returned. This is similar to
* `window.getSelection().isCollapsed()`.
*
* @param props The rich text value to check.
* @param props.start
* @param props.end
* @return True if the selection is collapsed, false if not, undefined if there is no selection.
*/
export function isCollapsed({
start,
end
}) {
if (start === undefined || end === undefined) {
return;
}
return start === end;
}
//# sourceMappingURL=is-collapsed.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isCollapsed","start","end","undefined"],"sources":["@wordpress/rich-text/src/is-collapsed.ts"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport type { RichTextValue } from './types';\n\n/**\n * Check if the selection of a Rich Text value is collapsed or not. Collapsed\n * means that no characters are selected, but there is a caret present. If there\n * is no selection, `undefined` will be returned. This is similar to\n * `window.getSelection().isCollapsed()`.\n *\n * @param props The rich text value to check.\n * @param props.start\n * @param props.end\n * @return True if the selection is collapsed, false if not, undefined if there is no selection.\n */\nexport function isCollapsed( {\n\tstart,\n\tend,\n}: RichTextValue ): boolean | undefined {\n\tif ( start === undefined || end === undefined ) {\n\t\treturn;\n\t}\n\n\treturn start === end;\n}\n"],"mappings":"AAAA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,WAAWA,CAAE;EAC5BC,KAAK;EACLC;AACc,CAAC,EAAwB;EACvC,IAAKD,KAAK,KAAKE,SAAS,IAAID,GAAG,KAAKC,SAAS,EAAG;IAC/C;EACD;EAEA,OAAOF,KAAK,KAAKC,GAAG;AACrB","ignoreList":[]}

View File

@@ -0,0 +1,16 @@
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Check if a Rich Text value is Empty, meaning it contains no text or any
* objects (such as images).
*
* @param {RichTextValue} value Value to use.
*
* @return {boolean} True if the value is empty, false if not.
*/
export function isEmpty({
text
}) {
return text.length === 0;
}
//# sourceMappingURL=is-empty.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isEmpty","text","length"],"sources":["@wordpress/rich-text/src/is-empty.js"],"sourcesContent":["/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Check if a Rich Text value is Empty, meaning it contains no text or any\n * objects (such as images).\n *\n * @param {RichTextValue} value Value to use.\n *\n * @return {boolean} True if the value is empty, false if not.\n */\nexport function isEmpty( { text } ) {\n\treturn text.length === 0;\n}\n"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,OAAOA,CAAE;EAAEC;AAAK,CAAC,EAAG;EACnC,OAAOA,IAAI,CAACC,MAAM,KAAK,CAAC;AACzB","ignoreList":[]}

View File

@@ -0,0 +1,52 @@
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Optimised equality check for format objects.
*
* @param {?RichTextFormat} format1 Format to compare.
* @param {?RichTextFormat} format2 Format to compare.
*
* @return {boolean} True if formats are equal, false if not.
*/
export function isFormatEqual(format1, format2) {
// Both not defined.
if (format1 === format2) {
return true;
}
// Either not defined.
if (!format1 || !format2) {
return false;
}
if (format1.type !== format2.type) {
return false;
}
const attributes1 = format1.attributes;
const attributes2 = format2.attributes;
// Both not defined.
if (attributes1 === attributes2) {
return true;
}
// Either not defined.
if (!attributes1 || !attributes2) {
return false;
}
const keys1 = Object.keys(attributes1);
const keys2 = Object.keys(attributes2);
if (keys1.length !== keys2.length) {
return false;
}
const length = keys1.length;
// Optimise for speed.
for (let i = 0; i < length; i++) {
const name = keys1[i];
if (attributes1[name] !== attributes2[name]) {
return false;
}
}
return true;
}
//# sourceMappingURL=is-format-equal.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","format1","format2","type","attributes1","attributes","attributes2","keys1","Object","keys","keys2","length","i","name"],"sources":["@wordpress/rich-text/src/is-format-equal.js"],"sourcesContent":["/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Optimised equality check for format objects.\n *\n * @param {?RichTextFormat} format1 Format to compare.\n * @param {?RichTextFormat} format2 Format to compare.\n *\n * @return {boolean} True if formats are equal, false if not.\n */\nexport function isFormatEqual( format1, format2 ) {\n\t// Both not defined.\n\tif ( format1 === format2 ) {\n\t\treturn true;\n\t}\n\n\t// Either not defined.\n\tif ( ! format1 || ! format2 ) {\n\t\treturn false;\n\t}\n\n\tif ( format1.type !== format2.type ) {\n\t\treturn false;\n\t}\n\n\tconst attributes1 = format1.attributes;\n\tconst attributes2 = format2.attributes;\n\n\t// Both not defined.\n\tif ( attributes1 === attributes2 ) {\n\t\treturn true;\n\t}\n\n\t// Either not defined.\n\tif ( ! attributes1 || ! attributes2 ) {\n\t\treturn false;\n\t}\n\n\tconst keys1 = Object.keys( attributes1 );\n\tconst keys2 = Object.keys( attributes2 );\n\n\tif ( keys1.length !== keys2.length ) {\n\t\treturn false;\n\t}\n\n\tconst length = keys1.length;\n\n\t// Optimise for speed.\n\tfor ( let i = 0; i < length; i++ ) {\n\t\tconst name = keys1[ i ];\n\n\t\tif ( attributes1[ name ] !== attributes2[ name ] ) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\treturn true;\n}\n"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,aAAaA,CAAEC,OAAO,EAAEC,OAAO,EAAG;EACjD;EACA,IAAKD,OAAO,KAAKC,OAAO,EAAG;IAC1B,OAAO,IAAI;EACZ;;EAEA;EACA,IAAK,CAAED,OAAO,IAAI,CAAEC,OAAO,EAAG;IAC7B,OAAO,KAAK;EACb;EAEA,IAAKD,OAAO,CAACE,IAAI,KAAKD,OAAO,CAACC,IAAI,EAAG;IACpC,OAAO,KAAK;EACb;EAEA,MAAMC,WAAW,GAAGH,OAAO,CAACI,UAAU;EACtC,MAAMC,WAAW,GAAGJ,OAAO,CAACG,UAAU;;EAEtC;EACA,IAAKD,WAAW,KAAKE,WAAW,EAAG;IAClC,OAAO,IAAI;EACZ;;EAEA;EACA,IAAK,CAAEF,WAAW,IAAI,CAAEE,WAAW,EAAG;IACrC,OAAO,KAAK;EACb;EAEA,MAAMC,KAAK,GAAGC,MAAM,CAACC,IAAI,CAAEL,WAAY,CAAC;EACxC,MAAMM,KAAK,GAAGF,MAAM,CAACC,IAAI,CAAEH,WAAY,CAAC;EAExC,IAAKC,KAAK,CAACI,MAAM,KAAKD,KAAK,CAACC,MAAM,EAAG;IACpC,OAAO,KAAK;EACb;EAEA,MAAMA,MAAM,GAAGJ,KAAK,CAACI,MAAM;;EAE3B;EACA,KAAM,IAAIC,CAAC,GAAG,CAAC,EAAEA,CAAC,GAAGD,MAAM,EAAEC,CAAC,EAAE,EAAG;IAClC,MAAMC,IAAI,GAAGN,KAAK,CAAEK,CAAC,CAAE;IAEvB,IAAKR,WAAW,CAAES,IAAI,CAAE,KAAKP,WAAW,CAAEO,IAAI,CAAE,EAAG;MAClD,OAAO,KAAK;IACb;EACD;EAEA,OAAO,IAAI;AACZ","ignoreList":[]}

View File

@@ -0,0 +1,14 @@
/**
* Returns true if two ranges are equal, or false otherwise. Ranges are
* considered equal if their start and end occur in the same container and
* offset.
*
* @param {Range|null} a First range object to test.
* @param {Range|null} b First range object to test.
*
* @return {boolean} Whether the two ranges are equal.
*/
export function isRangeEqual(a, b) {
return a === b || a && b && a.startContainer === b.startContainer && a.startOffset === b.startOffset && a.endContainer === b.endContainer && a.endOffset === b.endOffset;
}
//# sourceMappingURL=is-range-equal.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isRangeEqual","a","b","startContainer","startOffset","endContainer","endOffset"],"sources":["@wordpress/rich-text/src/is-range-equal.js"],"sourcesContent":["/**\n * Returns true if two ranges are equal, or false otherwise. Ranges are\n * considered equal if their start and end occur in the same container and\n * offset.\n *\n * @param {Range|null} a First range object to test.\n * @param {Range|null} b First range object to test.\n *\n * @return {boolean} Whether the two ranges are equal.\n */\nexport function isRangeEqual( a, b ) {\n\treturn (\n\t\ta === b ||\n\t\t( a &&\n\t\t\tb &&\n\t\t\ta.startContainer === b.startContainer &&\n\t\t\ta.startOffset === b.startOffset &&\n\t\t\ta.endContainer === b.endContainer &&\n\t\t\ta.endOffset === b.endOffset )\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,YAAYA,CAAEC,CAAC,EAAEC,CAAC,EAAG;EACpC,OACCD,CAAC,KAAKC,CAAC,IACLD,CAAC,IACFC,CAAC,IACDD,CAAC,CAACE,cAAc,KAAKD,CAAC,CAACC,cAAc,IACrCF,CAAC,CAACG,WAAW,KAAKF,CAAC,CAACE,WAAW,IAC/BH,CAAC,CAACI,YAAY,KAAKH,CAAC,CAACG,YAAY,IACjCJ,CAAC,CAACK,SAAS,KAAKJ,CAAC,CAACI,SAAW;AAEhC","ignoreList":[]}

36
node_modules/@wordpress/rich-text/build-module/join.js generated vendored Normal file
View File

@@ -0,0 +1,36 @@
/**
* Internal dependencies
*/
import { create } from './create';
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Combine an array of Rich Text values into one, optionally separated by
* `separator`, which can be a Rich Text value, HTML string, or plain text
* string. This is similar to `Array.prototype.join`.
*
* @param {Array<RichTextValue>} values An array of values to join.
* @param {string|RichTextValue} [separator] Separator string or value.
*
* @return {RichTextValue} A new combined value.
*/
export function join(values, separator = '') {
if (typeof separator === 'string') {
separator = create({
text: separator
});
}
return normaliseFormats(values.reduce((accumlator, {
formats,
replacements,
text
}) => ({
formats: accumlator.formats.concat(separator.formats, formats),
replacements: accumlator.replacements.concat(separator.replacements, replacements),
text: accumlator.text + separator.text + text
})));
}
//# sourceMappingURL=join.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["create","normaliseFormats","join","values","separator","text","reduce","accumlator","formats","replacements","concat"],"sources":["@wordpress/rich-text/src/join.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { create } from './create';\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Combine an array of Rich Text values into one, optionally separated by\n * `separator`, which can be a Rich Text value, HTML string, or plain text\n * string. This is similar to `Array.prototype.join`.\n *\n * @param {Array<RichTextValue>} values An array of values to join.\n * @param {string|RichTextValue} [separator] Separator string or value.\n *\n * @return {RichTextValue} A new combined value.\n */\nexport function join( values, separator = '' ) {\n\tif ( typeof separator === 'string' ) {\n\t\tseparator = create( { text: separator } );\n\t}\n\n\treturn normaliseFormats(\n\t\tvalues.reduce( ( accumlator, { formats, replacements, text } ) => ( {\n\t\t\tformats: accumlator.formats.concat( separator.formats, formats ),\n\t\t\treplacements: accumlator.replacements.concat(\n\t\t\t\tseparator.replacements,\n\t\t\t\treplacements\n\t\t\t),\n\t\t\ttext: accumlator.text + separator.text + text,\n\t\t} ) )\n\t);\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,IAAIA,CAAEC,MAAM,EAAEC,SAAS,GAAG,EAAE,EAAG;EAC9C,IAAK,OAAOA,SAAS,KAAK,QAAQ,EAAG;IACpCA,SAAS,GAAGJ,MAAM,CAAE;MAAEK,IAAI,EAAED;IAAU,CAAE,CAAC;EAC1C;EAEA,OAAOH,gBAAgB,CACtBE,MAAM,CAACG,MAAM,CAAE,CAAEC,UAAU,EAAE;IAAEC,OAAO;IAAEC,YAAY;IAAEJ;EAAK,CAAC,MAAQ;IACnEG,OAAO,EAAED,UAAU,CAACC,OAAO,CAACE,MAAM,CAAEN,SAAS,CAACI,OAAO,EAAEA,OAAQ,CAAC;IAChEC,YAAY,EAAEF,UAAU,CAACE,YAAY,CAACC,MAAM,CAC3CN,SAAS,CAACK,YAAY,EACtBA,YACD,CAAC;IACDJ,IAAI,EAAEE,UAAU,CAACF,IAAI,GAAGD,SAAS,CAACC,IAAI,GAAGA;EAC1C,CAAC,CAAG,CACL,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,37 @@
/**
* Internal dependencies
*/
import { isFormatEqual } from './is-format-equal';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Normalises formats: ensures subsequent adjacent equal formats have the same
* reference.
*
* @param {RichTextValue} value Value to normalise formats of.
*
* @return {RichTextValue} New value with normalised formats.
*/
export function normaliseFormats(value) {
const newFormats = value.formats.slice();
newFormats.forEach((formatsAtIndex, index) => {
const formatsAtPreviousIndex = newFormats[index - 1];
if (formatsAtPreviousIndex) {
const newFormatsAtIndex = formatsAtIndex.slice();
newFormatsAtIndex.forEach((format, formatIndex) => {
const previousFormat = formatsAtPreviousIndex[formatIndex];
if (isFormatEqual(format, previousFormat)) {
newFormatsAtIndex[formatIndex] = previousFormat;
}
});
newFormats[index] = newFormatsAtIndex;
}
});
return {
...value,
formats: newFormats
};
}
//# sourceMappingURL=normalise-formats.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["isFormatEqual","normaliseFormats","value","newFormats","formats","slice","forEach","formatsAtIndex","index","formatsAtPreviousIndex","newFormatsAtIndex","format","formatIndex","previousFormat"],"sources":["@wordpress/rich-text/src/normalise-formats.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { isFormatEqual } from './is-format-equal';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Normalises formats: ensures subsequent adjacent equal formats have the same\n * reference.\n *\n * @param {RichTextValue} value Value to normalise formats of.\n *\n * @return {RichTextValue} New value with normalised formats.\n */\nexport function normaliseFormats( value ) {\n\tconst newFormats = value.formats.slice();\n\n\tnewFormats.forEach( ( formatsAtIndex, index ) => {\n\t\tconst formatsAtPreviousIndex = newFormats[ index - 1 ];\n\n\t\tif ( formatsAtPreviousIndex ) {\n\t\t\tconst newFormatsAtIndex = formatsAtIndex.slice();\n\n\t\t\tnewFormatsAtIndex.forEach( ( format, formatIndex ) => {\n\t\t\t\tconst previousFormat = formatsAtPreviousIndex[ formatIndex ];\n\n\t\t\t\tif ( isFormatEqual( format, previousFormat ) ) {\n\t\t\t\t\tnewFormatsAtIndex[ formatIndex ] = previousFormat;\n\t\t\t\t}\n\t\t\t} );\n\n\t\t\tnewFormats[ index ] = newFormatsAtIndex;\n\t\t}\n\t} );\n\n\treturn {\n\t\t...value,\n\t\tformats: newFormats,\n\t};\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,aAAa,QAAQ,mBAAmB;;AAEjD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,gBAAgBA,CAAEC,KAAK,EAAG;EACzC,MAAMC,UAAU,GAAGD,KAAK,CAACE,OAAO,CAACC,KAAK,CAAC,CAAC;EAExCF,UAAU,CAACG,OAAO,CAAE,CAAEC,cAAc,EAAEC,KAAK,KAAM;IAChD,MAAMC,sBAAsB,GAAGN,UAAU,CAAEK,KAAK,GAAG,CAAC,CAAE;IAEtD,IAAKC,sBAAsB,EAAG;MAC7B,MAAMC,iBAAiB,GAAGH,cAAc,CAACF,KAAK,CAAC,CAAC;MAEhDK,iBAAiB,CAACJ,OAAO,CAAE,CAAEK,MAAM,EAAEC,WAAW,KAAM;QACrD,MAAMC,cAAc,GAAGJ,sBAAsB,CAAEG,WAAW,CAAE;QAE5D,IAAKZ,aAAa,CAAEW,MAAM,EAAEE,cAAe,CAAC,EAAG;UAC9CH,iBAAiB,CAAEE,WAAW,CAAE,GAAGC,cAAc;QAClD;MACD,CAAE,CAAC;MAEHV,UAAU,CAAEK,KAAK,CAAE,GAAGE,iBAAiB;IACxC;EACD,CAAE,CAAC;EAEH,OAAO;IACN,GAAGR,KAAK;IACRE,OAAO,EAAED;EACV,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,90 @@
/**
* WordPress dependencies
*/
import { select, dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/**
* @typedef {Object} WPFormat
*
* @property {string} name A string identifying the format. Must be
* unique across all registered formats.
* @property {string} tagName The HTML tag this format will wrap the
* selection with.
* @property {boolean} interactive Whether format makes content interactive or not.
* @property {string | null} [className] A class to match the format.
* @property {string} title Name of the format.
* @property {Function} edit Should return a component for the user to
* interact with the new registered format.
*/
/**
* Registers a new format provided a unique name and an object defining its
* behavior.
*
* @param {string} name Format name.
* @param {WPFormat} settings Format settings.
*
* @return {WPFormat|undefined} The format, if it has been successfully
* registered; otherwise `undefined`.
*/
export function registerFormatType(name, settings) {
settings = {
name,
...settings
};
if (typeof settings.name !== 'string') {
window.console.error('Format names must be strings.');
return;
}
if (!/^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test(settings.name)) {
window.console.error('Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format');
return;
}
if (select(richTextStore).getFormatType(settings.name)) {
window.console.error('Format "' + settings.name + '" is already registered.');
return;
}
if (typeof settings.tagName !== 'string' || settings.tagName === '') {
window.console.error('Format tag names must be a string.');
return;
}
if ((typeof settings.className !== 'string' || settings.className === '') && settings.className !== null) {
window.console.error('Format class names must be a string, or null to handle bare elements.');
return;
}
if (!/^[_a-zA-Z]+[a-zA-Z0-9_-]*$/.test(settings.className)) {
window.console.error('A class name must begin with a letter, followed by any number of hyphens, underscores, letters, or numbers.');
return;
}
if (settings.className === null) {
const formatTypeForBareElement = select(richTextStore).getFormatTypeForBareElement(settings.tagName);
if (formatTypeForBareElement && formatTypeForBareElement.name !== 'core/unknown') {
window.console.error(`Format "${formatTypeForBareElement.name}" is already registered to handle bare tag name "${settings.tagName}".`);
return;
}
} else {
const formatTypeForClassName = select(richTextStore).getFormatTypeForClassName(settings.className);
if (formatTypeForClassName) {
window.console.error(`Format "${formatTypeForClassName.name}" is already registered to handle class name "${settings.className}".`);
return;
}
}
if (!('title' in settings) || settings.title === '') {
window.console.error('The format "' + settings.name + '" must have a title.');
return;
}
if ('keywords' in settings && settings.keywords.length > 3) {
window.console.error('The format "' + settings.name + '" can have a maximum of 3 keywords.');
return;
}
if (typeof settings.title !== 'string') {
window.console.error('Format titles must be strings.');
return;
}
dispatch(richTextStore).addFormatTypes(settings);
return settings;
}
//# sourceMappingURL=register-format-type.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,70 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Remove any format object from a Rich Text value by type from the given
* `startIndex` to the given `endIndex`. Indices are retrieved from the
* selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {string} formatType Format type to remove.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the format applied.
*/
export function removeFormat(value, formatType, startIndex = value.start, endIndex = value.end) {
const {
formats,
activeFormats
} = value;
const newFormats = formats.slice();
// If the selection is collapsed, expand start and end to the edges of the
// format.
if (startIndex === endIndex) {
const format = newFormats[startIndex]?.find(({
type
}) => type === formatType);
if (format) {
while (newFormats[startIndex]?.find(newFormat => newFormat === format)) {
filterFormats(newFormats, startIndex, formatType);
startIndex--;
}
endIndex++;
while (newFormats[endIndex]?.find(newFormat => newFormat === format)) {
filterFormats(newFormats, endIndex, formatType);
endIndex++;
}
}
} else {
for (let i = startIndex; i < endIndex; i++) {
if (newFormats[i]) {
filterFormats(newFormats, i, formatType);
}
}
}
return normaliseFormats({
...value,
formats: newFormats,
activeFormats: activeFormats?.filter(({
type
}) => type !== formatType) || []
});
}
function filterFormats(formats, index, formatType) {
const newFormats = formats[index].filter(({
type
}) => type !== formatType);
if (newFormats.length) {
formats[index] = newFormats;
} else {
delete formats[index];
}
}
//# sourceMappingURL=remove-format.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["normaliseFormats","removeFormat","value","formatType","startIndex","start","endIndex","end","formats","activeFormats","newFormats","slice","format","find","type","newFormat","filterFormats","i","filter","index","length"],"sources":["@wordpress/rich-text/src/remove-format.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Remove any format object from a Rich Text value by type from the given\n * `startIndex` to the given `endIndex`. Indices are retrieved from the\n * selection if none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {string} formatType Format type to remove.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the format applied.\n */\nexport function removeFormat(\n\tvalue,\n\tformatType,\n\tstartIndex = value.start,\n\tendIndex = value.end\n) {\n\tconst { formats, activeFormats } = value;\n\tconst newFormats = formats.slice();\n\n\t// If the selection is collapsed, expand start and end to the edges of the\n\t// format.\n\tif ( startIndex === endIndex ) {\n\t\tconst format = newFormats[ startIndex ]?.find(\n\t\t\t( { type } ) => type === formatType\n\t\t);\n\n\t\tif ( format ) {\n\t\t\twhile (\n\t\t\t\tnewFormats[ startIndex ]?.find(\n\t\t\t\t\t( newFormat ) => newFormat === format\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tfilterFormats( newFormats, startIndex, formatType );\n\t\t\t\tstartIndex--;\n\t\t\t}\n\n\t\t\tendIndex++;\n\n\t\t\twhile (\n\t\t\t\tnewFormats[ endIndex ]?.find(\n\t\t\t\t\t( newFormat ) => newFormat === format\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tfilterFormats( newFormats, endIndex, formatType );\n\t\t\t\tendIndex++;\n\t\t\t}\n\t\t}\n\t} else {\n\t\tfor ( let i = startIndex; i < endIndex; i++ ) {\n\t\t\tif ( newFormats[ i ] ) {\n\t\t\t\tfilterFormats( newFormats, i, formatType );\n\t\t\t}\n\t\t}\n\t}\n\n\treturn normaliseFormats( {\n\t\t...value,\n\t\tformats: newFormats,\n\t\tactiveFormats:\n\t\t\tactiveFormats?.filter( ( { type } ) => type !== formatType ) || [],\n\t} );\n}\n\nfunction filterFormats( formats, index, formatType ) {\n\tconst newFormats = formats[ index ].filter(\n\t\t( { type } ) => type !== formatType\n\t);\n\n\tif ( newFormats.length ) {\n\t\tformats[ index ] = newFormats;\n\t} else {\n\t\tdelete formats[ index ];\n\t}\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAC3BC,KAAK,EACLC,UAAU,EACVC,UAAU,GAAGF,KAAK,CAACG,KAAK,EACxBC,QAAQ,GAAGJ,KAAK,CAACK,GAAG,EACnB;EACD,MAAM;IAAEC,OAAO;IAAEC;EAAc,CAAC,GAAGP,KAAK;EACxC,MAAMQ,UAAU,GAAGF,OAAO,CAACG,KAAK,CAAC,CAAC;;EAElC;EACA;EACA,IAAKP,UAAU,KAAKE,QAAQ,EAAG;IAC9B,MAAMM,MAAM,GAAGF,UAAU,CAAEN,UAAU,CAAE,EAAES,IAAI,CAC5C,CAAE;MAAEC;IAAK,CAAC,KAAMA,IAAI,KAAKX,UAC1B,CAAC;IAED,IAAKS,MAAM,EAAG;MACb,OACCF,UAAU,CAAEN,UAAU,CAAE,EAAES,IAAI,CAC3BE,SAAS,IAAMA,SAAS,KAAKH,MAChC,CAAC,EACA;QACDI,aAAa,CAAEN,UAAU,EAAEN,UAAU,EAAED,UAAW,CAAC;QACnDC,UAAU,EAAE;MACb;MAEAE,QAAQ,EAAE;MAEV,OACCI,UAAU,CAAEJ,QAAQ,CAAE,EAAEO,IAAI,CACzBE,SAAS,IAAMA,SAAS,KAAKH,MAChC,CAAC,EACA;QACDI,aAAa,CAAEN,UAAU,EAAEJ,QAAQ,EAAEH,UAAW,CAAC;QACjDG,QAAQ,EAAE;MACX;IACD;EACD,CAAC,MAAM;IACN,KAAM,IAAIW,CAAC,GAAGb,UAAU,EAAEa,CAAC,GAAGX,QAAQ,EAAEW,CAAC,EAAE,EAAG;MAC7C,IAAKP,UAAU,CAAEO,CAAC,CAAE,EAAG;QACtBD,aAAa,CAAEN,UAAU,EAAEO,CAAC,EAAEd,UAAW,CAAC;MAC3C;IACD;EACD;EAEA,OAAOH,gBAAgB,CAAE;IACxB,GAAGE,KAAK;IACRM,OAAO,EAAEE,UAAU;IACnBD,aAAa,EACZA,aAAa,EAAES,MAAM,CAAE,CAAE;MAAEJ;IAAK,CAAC,KAAMA,IAAI,KAAKX,UAAW,CAAC,IAAI;EAClE,CAAE,CAAC;AACJ;AAEA,SAASa,aAAaA,CAAER,OAAO,EAAEW,KAAK,EAAEhB,UAAU,EAAG;EACpD,MAAMO,UAAU,GAAGF,OAAO,CAAEW,KAAK,CAAE,CAACD,MAAM,CACzC,CAAE;IAAEJ;EAAK,CAAC,KAAMA,IAAI,KAAKX,UAC1B,CAAC;EAED,IAAKO,UAAU,CAACU,MAAM,EAAG;IACxBZ,OAAO,CAAEW,KAAK,CAAE,GAAGT,UAAU;EAC9B,CAAC,MAAM;IACN,OAAOF,OAAO,CAAEW,KAAK,CAAE;EACxB;AACD","ignoreList":[]}

View File

@@ -0,0 +1,2 @@
//# sourceMappingURL=remove-unregistered-formatting.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":[],"sources":["@wordpress/rich-text/src/remove-unregistered-formatting.js"],"sourcesContent":[""],"mappings":"","ignoreList":[]}

View File

@@ -0,0 +1,23 @@
/**
* Internal dependencies
*/
import { insert } from './insert';
import { create } from './create';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Remove content from a Rich Text value between the given `startIndex` and
* `endIndex`. Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value Value to modify.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new value with the content removed.
*/
export function remove(value, startIndex, endIndex) {
return insert(value, create(), startIndex, endIndex);
}
//# sourceMappingURL=remove.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["insert","create","remove","value","startIndex","endIndex"],"sources":["@wordpress/rich-text/src/remove.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { insert } from './insert';\nimport { create } from './create';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Remove content from a Rich Text value between the given `startIndex` and\n * `endIndex`. Indices are retrieved from the selection if none are provided.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new value with the content removed.\n */\nexport function remove( value, startIndex, endIndex ) {\n\treturn insert( value, create(), startIndex, endIndex );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,MAAM,QAAQ,UAAU;AACjC,SAASC,MAAM,QAAQ,UAAU;;AAEjC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,MAAMA,CAAEC,KAAK,EAAEC,UAAU,EAAEC,QAAQ,EAAG;EACrD,OAAOL,MAAM,CAAEG,KAAK,EAAEF,MAAM,CAAC,CAAC,EAAEG,UAAU,EAAEC,QAAS,CAAC;AACvD","ignoreList":[]}

View File

@@ -0,0 +1,66 @@
/**
* Internal dependencies
*/
import { normaliseFormats } from './normalise-formats';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Search a Rich Text value and replace the match(es) with `replacement`. This
* is similar to `String.prototype.replace`.
*
* @param {RichTextValue} value The value to modify.
* @param {RegExp|string} pattern A RegExp object or literal. Can also be
* a string. It is treated as a verbatim
* string and is not interpreted as a
* regular expression. Only the first
* occurrence will be replaced.
* @param {Function|string} replacement The match or matches are replaced with
* the specified or the value returned by
* the specified function.
*
* @return {RichTextValue} A new value with replacements applied.
*/
export function replace({
formats,
replacements,
text,
start,
end
}, pattern, replacement) {
text = text.replace(pattern, (match, ...rest) => {
const offset = rest[rest.length - 2];
let newText = replacement;
let newFormats;
let newReplacements;
if (typeof newText === 'function') {
newText = replacement(match, ...rest);
}
if (typeof newText === 'object') {
newFormats = newText.formats;
newReplacements = newText.replacements;
newText = newText.text;
} else {
newFormats = Array(newText.length);
newReplacements = Array(newText.length);
if (formats[offset]) {
newFormats = newFormats.fill(formats[offset]);
}
}
formats = formats.slice(0, offset).concat(newFormats, formats.slice(offset + match.length));
replacements = replacements.slice(0, offset).concat(newReplacements, replacements.slice(offset + match.length));
if (start) {
start = end = offset + newText.length;
}
return newText;
});
return normaliseFormats({
formats,
replacements,
text,
start,
end
});
}
//# sourceMappingURL=replace.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["normaliseFormats","replace","formats","replacements","text","start","end","pattern","replacement","match","rest","offset","length","newText","newFormats","newReplacements","Array","fill","slice","concat"],"sources":["@wordpress/rich-text/src/replace.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\nimport { normaliseFormats } from './normalise-formats';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Search a Rich Text value and replace the match(es) with `replacement`. This\n * is similar to `String.prototype.replace`.\n *\n * @param {RichTextValue} value The value to modify.\n * @param {RegExp|string} pattern A RegExp object or literal. Can also be\n * a string. It is treated as a verbatim\n * string and is not interpreted as a\n * regular expression. Only the first\n * occurrence will be replaced.\n * @param {Function|string} replacement The match or matches are replaced with\n * the specified or the value returned by\n * the specified function.\n *\n * @return {RichTextValue} A new value with replacements applied.\n */\nexport function replace(\n\t{ formats, replacements, text, start, end },\n\tpattern,\n\treplacement\n) {\n\ttext = text.replace( pattern, ( match, ...rest ) => {\n\t\tconst offset = rest[ rest.length - 2 ];\n\t\tlet newText = replacement;\n\t\tlet newFormats;\n\t\tlet newReplacements;\n\n\t\tif ( typeof newText === 'function' ) {\n\t\t\tnewText = replacement( match, ...rest );\n\t\t}\n\n\t\tif ( typeof newText === 'object' ) {\n\t\t\tnewFormats = newText.formats;\n\t\t\tnewReplacements = newText.replacements;\n\t\t\tnewText = newText.text;\n\t\t} else {\n\t\t\tnewFormats = Array( newText.length );\n\t\t\tnewReplacements = Array( newText.length );\n\n\t\t\tif ( formats[ offset ] ) {\n\t\t\t\tnewFormats = newFormats.fill( formats[ offset ] );\n\t\t\t}\n\t\t}\n\n\t\tformats = formats\n\t\t\t.slice( 0, offset )\n\t\t\t.concat( newFormats, formats.slice( offset + match.length ) );\n\t\treplacements = replacements\n\t\t\t.slice( 0, offset )\n\t\t\t.concat(\n\t\t\t\tnewReplacements,\n\t\t\t\treplacements.slice( offset + match.length )\n\t\t\t);\n\n\t\tif ( start ) {\n\t\t\tstart = end = offset + newText.length;\n\t\t}\n\n\t\treturn newText;\n\t} );\n\n\treturn normaliseFormats( { formats, replacements, text, start, end } );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,gBAAgB,QAAQ,qBAAqB;;AAEtD;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,OAAOA,CACtB;EAAEC,OAAO;EAAEC,YAAY;EAAEC,IAAI;EAAEC,KAAK;EAAEC;AAAI,CAAC,EAC3CC,OAAO,EACPC,WAAW,EACV;EACDJ,IAAI,GAAGA,IAAI,CAACH,OAAO,CAAEM,OAAO,EAAE,CAAEE,KAAK,EAAE,GAAGC,IAAI,KAAM;IACnD,MAAMC,MAAM,GAAGD,IAAI,CAAEA,IAAI,CAACE,MAAM,GAAG,CAAC,CAAE;IACtC,IAAIC,OAAO,GAAGL,WAAW;IACzB,IAAIM,UAAU;IACd,IAAIC,eAAe;IAEnB,IAAK,OAAOF,OAAO,KAAK,UAAU,EAAG;MACpCA,OAAO,GAAGL,WAAW,CAAEC,KAAK,EAAE,GAAGC,IAAK,CAAC;IACxC;IAEA,IAAK,OAAOG,OAAO,KAAK,QAAQ,EAAG;MAClCC,UAAU,GAAGD,OAAO,CAACX,OAAO;MAC5Ba,eAAe,GAAGF,OAAO,CAACV,YAAY;MACtCU,OAAO,GAAGA,OAAO,CAACT,IAAI;IACvB,CAAC,MAAM;MACNU,UAAU,GAAGE,KAAK,CAAEH,OAAO,CAACD,MAAO,CAAC;MACpCG,eAAe,GAAGC,KAAK,CAAEH,OAAO,CAACD,MAAO,CAAC;MAEzC,IAAKV,OAAO,CAAES,MAAM,CAAE,EAAG;QACxBG,UAAU,GAAGA,UAAU,CAACG,IAAI,CAAEf,OAAO,CAAES,MAAM,CAAG,CAAC;MAClD;IACD;IAEAT,OAAO,GAAGA,OAAO,CACfgB,KAAK,CAAE,CAAC,EAAEP,MAAO,CAAC,CAClBQ,MAAM,CAAEL,UAAU,EAAEZ,OAAO,CAACgB,KAAK,CAAEP,MAAM,GAAGF,KAAK,CAACG,MAAO,CAAE,CAAC;IAC9DT,YAAY,GAAGA,YAAY,CACzBe,KAAK,CAAE,CAAC,EAAEP,MAAO,CAAC,CAClBQ,MAAM,CACNJ,eAAe,EACfZ,YAAY,CAACe,KAAK,CAAEP,MAAM,GAAGF,KAAK,CAACG,MAAO,CAC3C,CAAC;IAEF,IAAKP,KAAK,EAAG;MACZA,KAAK,GAAGC,GAAG,GAAGK,MAAM,GAAGE,OAAO,CAACD,MAAM;IACtC;IAEA,OAAOC,OAAO;EACf,CAAE,CAAC;EAEH,OAAOb,gBAAgB,CAAE;IAAEE,OAAO;IAAEC,YAAY;IAAEC,IAAI;IAAEC,KAAK;IAAEC;EAAI,CAAE,CAAC;AACvE","ignoreList":[]}

View File

@@ -0,0 +1,31 @@
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Slice a Rich Text value from `startIndex` to `endIndex`. Indices are
* retrieved from the selection if none are provided. This is similar to
* `String.prototype.slice`.
*
* @param {RichTextValue} value Value to modify.
* @param {number} [startIndex] Start index.
* @param {number} [endIndex] End index.
*
* @return {RichTextValue} A new extracted value.
*/
export function slice(value, startIndex = value.start, endIndex = value.end) {
const {
formats,
replacements,
text
} = value;
if (startIndex === undefined || endIndex === undefined) {
return {
...value
};
}
return {
formats: formats.slice(startIndex, endIndex),
replacements: replacements.slice(startIndex, endIndex),
text: text.slice(startIndex, endIndex)
};
}
//# sourceMappingURL=slice.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["slice","value","startIndex","start","endIndex","end","formats","replacements","text","undefined"],"sources":["@wordpress/rich-text/src/slice.js"],"sourcesContent":["/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Slice a Rich Text value from `startIndex` to `endIndex`. Indices are\n * retrieved from the selection if none are provided. This is similar to\n * `String.prototype.slice`.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {number} [startIndex] Start index.\n * @param {number} [endIndex] End index.\n *\n * @return {RichTextValue} A new extracted value.\n */\nexport function slice( value, startIndex = value.start, endIndex = value.end ) {\n\tconst { formats, replacements, text } = value;\n\n\tif ( startIndex === undefined || endIndex === undefined ) {\n\t\treturn { ...value };\n\t}\n\n\treturn {\n\t\tformats: formats.slice( startIndex, endIndex ),\n\t\treplacements: replacements.slice( startIndex, endIndex ),\n\t\ttext: text.slice( startIndex, endIndex ),\n\t};\n}\n"],"mappings":"AAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,KAAKA,CAAEC,KAAK,EAAEC,UAAU,GAAGD,KAAK,CAACE,KAAK,EAAEC,QAAQ,GAAGH,KAAK,CAACI,GAAG,EAAG;EAC9E,MAAM;IAAEC,OAAO;IAAEC,YAAY;IAAEC;EAAK,CAAC,GAAGP,KAAK;EAE7C,IAAKC,UAAU,KAAKO,SAAS,IAAIL,QAAQ,KAAKK,SAAS,EAAG;IACzD,OAAO;MAAE,GAAGR;IAAM,CAAC;EACpB;EAEA,OAAO;IACNK,OAAO,EAAEA,OAAO,CAACN,KAAK,CAAEE,UAAU,EAAEE,QAAS,CAAC;IAC9CG,YAAY,EAAEA,YAAY,CAACP,KAAK,CAAEE,UAAU,EAAEE,QAAS,CAAC;IACxDI,IAAI,EAAEA,IAAI,CAACR,KAAK,CAAEE,UAAU,EAAEE,QAAS;EACxC,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,11 @@
/**
* Object replacement character, used as a placeholder for objects.
*/
export const OBJECT_REPLACEMENT_CHARACTER = '\ufffc';
/**
* Zero width non-breaking space, used as padding in the editable DOM tree when
* it is empty otherwise.
*/
export const ZWNBSP = '\ufeff';
//# sourceMappingURL=special-characters.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["OBJECT_REPLACEMENT_CHARACTER","ZWNBSP"],"sources":["@wordpress/rich-text/src/special-characters.js"],"sourcesContent":["/**\n * Object replacement character, used as a placeholder for objects.\n */\nexport const OBJECT_REPLACEMENT_CHARACTER = '\\ufffc';\n\n/**\n * Zero width non-breaking space, used as padding in the editable DOM tree when\n * it is empty otherwise.\n */\nexport const ZWNBSP = '\\ufeff';\n"],"mappings":"AAAA;AACA;AACA;AACA,OAAO,MAAMA,4BAA4B,GAAG,QAAQ;;AAEpD;AACA;AACA;AACA;AACA,OAAO,MAAMC,MAAM,GAAG,QAAQ","ignoreList":[]}

View File

@@ -0,0 +1,75 @@
/**
* Internal dependencies
*/
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Split a Rich Text value in two at the given `startIndex` and `endIndex`, or
* split at the given separator. This is similar to `String.prototype.split`.
* Indices are retrieved from the selection if none are provided.
*
* @param {RichTextValue} value
* @param {number|string} [string] Start index, or string at which to split.
*
* @return {Array<RichTextValue>|undefined} An array of new values.
*/
export function split({
formats,
replacements,
text,
start,
end
}, string) {
if (typeof string !== 'string') {
return splitAtSelection(...arguments);
}
let nextStart = 0;
return text.split(string).map(substring => {
const startIndex = nextStart;
const value = {
formats: formats.slice(startIndex, startIndex + substring.length),
replacements: replacements.slice(startIndex, startIndex + substring.length),
text: substring
};
nextStart += string.length + substring.length;
if (start !== undefined && end !== undefined) {
if (start >= startIndex && start < nextStart) {
value.start = start - startIndex;
} else if (start < startIndex && end > startIndex) {
value.start = 0;
}
if (end >= startIndex && end < nextStart) {
value.end = end - startIndex;
} else if (start < nextStart && end > nextStart) {
value.end = substring.length;
}
}
return value;
});
}
function splitAtSelection({
formats,
replacements,
text,
start,
end
}, startIndex = start, endIndex = end) {
if (start === undefined || end === undefined) {
return;
}
const before = {
formats: formats.slice(0, startIndex),
replacements: replacements.slice(0, startIndex),
text: text.slice(0, startIndex)
};
const after = {
formats: formats.slice(endIndex),
replacements: replacements.slice(endIndex),
text: text.slice(endIndex),
start: 0,
end: 0
};
return [before, after];
}
//# sourceMappingURL=split.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["split","formats","replacements","text","start","end","string","splitAtSelection","arguments","nextStart","map","substring","startIndex","value","slice","length","undefined","endIndex","before","after"],"sources":["@wordpress/rich-text/src/split.js"],"sourcesContent":["/**\n * Internal dependencies\n */\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n\n/**\n * Split a Rich Text value in two at the given `startIndex` and `endIndex`, or\n * split at the given separator. This is similar to `String.prototype.split`.\n * Indices are retrieved from the selection if none are provided.\n *\n * @param {RichTextValue} value\n * @param {number|string} [string] Start index, or string at which to split.\n *\n * @return {Array<RichTextValue>|undefined} An array of new values.\n */\nexport function split( { formats, replacements, text, start, end }, string ) {\n\tif ( typeof string !== 'string' ) {\n\t\treturn splitAtSelection( ...arguments );\n\t}\n\n\tlet nextStart = 0;\n\n\treturn text.split( string ).map( ( substring ) => {\n\t\tconst startIndex = nextStart;\n\t\tconst value = {\n\t\t\tformats: formats.slice( startIndex, startIndex + substring.length ),\n\t\t\treplacements: replacements.slice(\n\t\t\t\tstartIndex,\n\t\t\t\tstartIndex + substring.length\n\t\t\t),\n\t\t\ttext: substring,\n\t\t};\n\n\t\tnextStart += string.length + substring.length;\n\n\t\tif ( start !== undefined && end !== undefined ) {\n\t\t\tif ( start >= startIndex && start < nextStart ) {\n\t\t\t\tvalue.start = start - startIndex;\n\t\t\t} else if ( start < startIndex && end > startIndex ) {\n\t\t\t\tvalue.start = 0;\n\t\t\t}\n\n\t\t\tif ( end >= startIndex && end < nextStart ) {\n\t\t\t\tvalue.end = end - startIndex;\n\t\t\t} else if ( start < nextStart && end > nextStart ) {\n\t\t\t\tvalue.end = substring.length;\n\t\t\t}\n\t\t}\n\n\t\treturn value;\n\t} );\n}\n\nfunction splitAtSelection(\n\t{ formats, replacements, text, start, end },\n\tstartIndex = start,\n\tendIndex = end\n) {\n\tif ( start === undefined || end === undefined ) {\n\t\treturn;\n\t}\n\n\tconst before = {\n\t\tformats: formats.slice( 0, startIndex ),\n\t\treplacements: replacements.slice( 0, startIndex ),\n\t\ttext: text.slice( 0, startIndex ),\n\t};\n\tconst after = {\n\t\tformats: formats.slice( endIndex ),\n\t\treplacements: replacements.slice( endIndex ),\n\t\ttext: text.slice( endIndex ),\n\t\tstart: 0,\n\t\tend: 0,\n\t};\n\n\treturn [ before, after ];\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,KAAKA,CAAE;EAAEC,OAAO;EAAEC,YAAY;EAAEC,IAAI;EAAEC,KAAK;EAAEC;AAAI,CAAC,EAAEC,MAAM,EAAG;EAC5E,IAAK,OAAOA,MAAM,KAAK,QAAQ,EAAG;IACjC,OAAOC,gBAAgB,CAAE,GAAGC,SAAU,CAAC;EACxC;EAEA,IAAIC,SAAS,GAAG,CAAC;EAEjB,OAAON,IAAI,CAACH,KAAK,CAAEM,MAAO,CAAC,CAACI,GAAG,CAAIC,SAAS,IAAM;IACjD,MAAMC,UAAU,GAAGH,SAAS;IAC5B,MAAMI,KAAK,GAAG;MACbZ,OAAO,EAAEA,OAAO,CAACa,KAAK,CAAEF,UAAU,EAAEA,UAAU,GAAGD,SAAS,CAACI,MAAO,CAAC;MACnEb,YAAY,EAAEA,YAAY,CAACY,KAAK,CAC/BF,UAAU,EACVA,UAAU,GAAGD,SAAS,CAACI,MACxB,CAAC;MACDZ,IAAI,EAAEQ;IACP,CAAC;IAEDF,SAAS,IAAIH,MAAM,CAACS,MAAM,GAAGJ,SAAS,CAACI,MAAM;IAE7C,IAAKX,KAAK,KAAKY,SAAS,IAAIX,GAAG,KAAKW,SAAS,EAAG;MAC/C,IAAKZ,KAAK,IAAIQ,UAAU,IAAIR,KAAK,GAAGK,SAAS,EAAG;QAC/CI,KAAK,CAACT,KAAK,GAAGA,KAAK,GAAGQ,UAAU;MACjC,CAAC,MAAM,IAAKR,KAAK,GAAGQ,UAAU,IAAIP,GAAG,GAAGO,UAAU,EAAG;QACpDC,KAAK,CAACT,KAAK,GAAG,CAAC;MAChB;MAEA,IAAKC,GAAG,IAAIO,UAAU,IAAIP,GAAG,GAAGI,SAAS,EAAG;QAC3CI,KAAK,CAACR,GAAG,GAAGA,GAAG,GAAGO,UAAU;MAC7B,CAAC,MAAM,IAAKR,KAAK,GAAGK,SAAS,IAAIJ,GAAG,GAAGI,SAAS,EAAG;QAClDI,KAAK,CAACR,GAAG,GAAGM,SAAS,CAACI,MAAM;MAC7B;IACD;IAEA,OAAOF,KAAK;EACb,CAAE,CAAC;AACJ;AAEA,SAASN,gBAAgBA,CACxB;EAAEN,OAAO;EAAEC,YAAY;EAAEC,IAAI;EAAEC,KAAK;EAAEC;AAAI,CAAC,EAC3CO,UAAU,GAAGR,KAAK,EAClBa,QAAQ,GAAGZ,GAAG,EACb;EACD,IAAKD,KAAK,KAAKY,SAAS,IAAIX,GAAG,KAAKW,SAAS,EAAG;IAC/C;EACD;EAEA,MAAME,MAAM,GAAG;IACdjB,OAAO,EAAEA,OAAO,CAACa,KAAK,CAAE,CAAC,EAAEF,UAAW,CAAC;IACvCV,YAAY,EAAEA,YAAY,CAACY,KAAK,CAAE,CAAC,EAAEF,UAAW,CAAC;IACjDT,IAAI,EAAEA,IAAI,CAACW,KAAK,CAAE,CAAC,EAAEF,UAAW;EACjC,CAAC;EACD,MAAMO,KAAK,GAAG;IACblB,OAAO,EAAEA,OAAO,CAACa,KAAK,CAAEG,QAAS,CAAC;IAClCf,YAAY,EAAEA,YAAY,CAACY,KAAK,CAAEG,QAAS,CAAC;IAC5Cd,IAAI,EAAEA,IAAI,CAACW,KAAK,CAAEG,QAAS,CAAC;IAC5Bb,KAAK,EAAE,CAAC;IACRC,GAAG,EAAE;EACN,CAAC;EAED,OAAO,CAAEa,MAAM,EAAEC,KAAK,CAAE;AACzB","ignoreList":[]}

View File

@@ -0,0 +1,36 @@
/**
* Returns an action object used in signalling that format types have been
* added.
* Ignored from documentation as registerFormatType should be used instead from @wordpress/rich-text
*
* @ignore
*
* @param {Array|Object} formatTypes Format types received.
*
* @return {Object} Action object.
*/
export function addFormatTypes(formatTypes) {
return {
type: 'ADD_FORMAT_TYPES',
formatTypes: Array.isArray(formatTypes) ? formatTypes : [formatTypes]
};
}
/**
* Returns an action object used to remove a registered format type.
*
* Ignored from documentation as unregisterFormatType should be used instead from @wordpress/rich-text
*
* @ignore
*
* @param {string|Array} names Format name.
*
* @return {Object} Action object.
*/
export function removeFormatTypes(names) {
return {
type: 'REMOVE_FORMAT_TYPES',
names: Array.isArray(names) ? names : [names]
};
}
//# sourceMappingURL=actions.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["addFormatTypes","formatTypes","type","Array","isArray","removeFormatTypes","names"],"sources":["@wordpress/rich-text/src/store/actions.js"],"sourcesContent":["/**\n * Returns an action object used in signalling that format types have been\n * added.\n * Ignored from documentation as registerFormatType should be used instead from @wordpress/rich-text\n *\n * @ignore\n *\n * @param {Array|Object} formatTypes Format types received.\n *\n * @return {Object} Action object.\n */\nexport function addFormatTypes( formatTypes ) {\n\treturn {\n\t\ttype: 'ADD_FORMAT_TYPES',\n\t\tformatTypes: Array.isArray( formatTypes )\n\t\t\t? formatTypes\n\t\t\t: [ formatTypes ],\n\t};\n}\n\n/**\n * Returns an action object used to remove a registered format type.\n *\n * Ignored from documentation as unregisterFormatType should be used instead from @wordpress/rich-text\n *\n * @ignore\n *\n * @param {string|Array} names Format name.\n *\n * @return {Object} Action object.\n */\nexport function removeFormatTypes( names ) {\n\treturn {\n\t\ttype: 'REMOVE_FORMAT_TYPES',\n\t\tnames: Array.isArray( names ) ? names : [ names ],\n\t};\n}\n"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASA,cAAcA,CAAEC,WAAW,EAAG;EAC7C,OAAO;IACNC,IAAI,EAAE,kBAAkB;IACxBD,WAAW,EAAEE,KAAK,CAACC,OAAO,CAAEH,WAAY,CAAC,GACtCA,WAAW,GACX,CAAEA,WAAW;EACjB,CAAC;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASI,iBAAiBA,CAAEC,KAAK,EAAG;EAC1C,OAAO;IACNJ,IAAI,EAAE,qBAAqB;IAC3BI,KAAK,EAAEH,KAAK,CAACC,OAAO,CAAEE,KAAM,CAAC,GAAGA,KAAK,GAAG,CAAEA,KAAK;EAChD,CAAC;AACF","ignoreList":[]}

View File

@@ -0,0 +1,27 @@
/**
* WordPress dependencies
*/
import { createReduxStore, register } from '@wordpress/data';
/**
* Internal dependencies
*/
import reducer from './reducer';
import * as selectors from './selectors';
import * as actions from './actions';
const STORE_NAME = 'core/rich-text';
/**
* Store definition for the rich-text namespace.
*
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
*
* @type {Object}
*/
export const store = createReduxStore(STORE_NAME, {
reducer,
selectors,
actions
});
register(store);
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["createReduxStore","register","reducer","selectors","actions","STORE_NAME","store"],"sources":["@wordpress/rich-text/src/store/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as selectors from './selectors';\nimport * as actions from './actions';\n\nconst STORE_NAME = 'core/rich-text';\n\n/**\n * Store definition for the rich-text namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, {\n\treducer,\n\tselectors,\n\tactions,\n} );\n\nregister( store );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,gBAAgB,EAAEC,QAAQ,QAAQ,iBAAiB;;AAE5D;AACA;AACA;AACA,OAAOC,OAAO,MAAM,WAAW;AAC/B,OAAO,KAAKC,SAAS,MAAM,aAAa;AACxC,OAAO,KAAKC,OAAO,MAAM,WAAW;AAEpC,MAAMC,UAAU,GAAG,gBAAgB;;AAEnC;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,KAAK,GAAGN,gBAAgB,CAAEK,UAAU,EAAE;EAClDH,OAAO;EACPC,SAAS;EACTC;AACD,CAAE,CAAC;AAEHH,QAAQ,CAAEK,KAAM,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,33 @@
/**
* WordPress dependencies
*/
import { combineReducers } from '@wordpress/data';
/**
* Reducer managing the format types
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function formatTypes(state = {}, action) {
switch (action.type) {
case 'ADD_FORMAT_TYPES':
return {
...state,
// Key format types by their name.
...action.formatTypes.reduce((newFormatTypes, type) => ({
...newFormatTypes,
[type.name]: type
}), {})
};
case 'REMOVE_FORMAT_TYPES':
return Object.fromEntries(Object.entries(state).filter(([key]) => !action.names.includes(key)));
}
return state;
}
export default combineReducers({
formatTypes
});
//# sourceMappingURL=reducer.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["combineReducers","formatTypes","state","action","type","reduce","newFormatTypes","name","Object","fromEntries","entries","filter","key","names","includes"],"sources":["@wordpress/rich-text/src/store/reducer.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer managing the format types\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nexport function formatTypes( state = {}, action ) {\n\tswitch ( action.type ) {\n\t\tcase 'ADD_FORMAT_TYPES':\n\t\t\treturn {\n\t\t\t\t...state,\n\t\t\t\t// Key format types by their name.\n\t\t\t\t...action.formatTypes.reduce(\n\t\t\t\t\t( newFormatTypes, type ) => ( {\n\t\t\t\t\t\t...newFormatTypes,\n\t\t\t\t\t\t[ type.name ]: type,\n\t\t\t\t\t} ),\n\t\t\t\t\t{}\n\t\t\t\t),\n\t\t\t};\n\t\tcase 'REMOVE_FORMAT_TYPES':\n\t\t\treturn Object.fromEntries(\n\t\t\t\tObject.entries( state ).filter(\n\t\t\t\t\t( [ key ] ) => ! action.names.includes( key )\n\t\t\t\t)\n\t\t\t);\n\t}\n\n\treturn state;\n}\n\nexport default combineReducers( { formatTypes } );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAe,QAAQ,iBAAiB;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,WAAWA,CAAEC,KAAK,GAAG,CAAC,CAAC,EAAEC,MAAM,EAAG;EACjD,QAASA,MAAM,CAACC,IAAI;IACnB,KAAK,kBAAkB;MACtB,OAAO;QACN,GAAGF,KAAK;QACR;QACA,GAAGC,MAAM,CAACF,WAAW,CAACI,MAAM,CAC3B,CAAEC,cAAc,EAAEF,IAAI,MAAQ;UAC7B,GAAGE,cAAc;UACjB,CAAEF,IAAI,CAACG,IAAI,GAAIH;QAChB,CAAC,CAAE,EACH,CAAC,CACF;MACD,CAAC;IACF,KAAK,qBAAqB;MACzB,OAAOI,MAAM,CAACC,WAAW,CACxBD,MAAM,CAACE,OAAO,CAAER,KAAM,CAAC,CAACS,MAAM,CAC7B,CAAE,CAAEC,GAAG,CAAE,KAAM,CAAET,MAAM,CAACU,KAAK,CAACC,QAAQ,CAAEF,GAAI,CAC7C,CACD,CAAC;EACH;EAEA,OAAOV,KAAK;AACb;AAEA,eAAeF,eAAe,CAAE;EAAEC;AAAY,CAAE,CAAC","ignoreList":[]}

View File

@@ -0,0 +1,161 @@
/**
* WordPress dependencies
*/
import { createSelector } from '@wordpress/data';
/**
* Returns all the available format types.
*
* @param {Object} state Data state.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatTypes } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const availableFormats = getFormatTypes();
*
* return availableFormats ? (
* <ul>
* { availableFormats?.map( ( format ) => (
* <li>{ format.name }</li>
* ) ) }
* </ul>
* ) : (
* __( 'No Formats available' )
* );
* };
* ```
*
* @return {Array} Format types.
*/
export const getFormatTypes = createSelector(state => Object.values(state.formatTypes), state => [state.formatTypes]);
/**
* Returns a format type by name.
*
* @param {Object} state Data state.
* @param {string} name Format type name.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatType } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const boldFormat = getFormatType( 'core/bold' );
*
* return boldFormat ? (
* <ul>
* { Object.entries( boldFormat )?.map( ( [ key, value ] ) => (
* <li>
* { key } : { value }
* </li>
* ) ) }
* </ul>
* ) : (
* __( 'Not Found' )
* ;
* };
* ```
*
* @return {Object?} Format type.
*/
export function getFormatType(state, name) {
return state.formatTypes[name];
}
/**
* Gets the format type, if any, that can handle a bare element (without a
* data-format-type attribute), given the tag name of this element.
*
* @param {Object} state Data state.
* @param {string} bareElementTagName The tag name of the element to find a
* format type for.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatTypeForBareElement } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const format = getFormatTypeForBareElement( 'strong' );
*
* return format && <p>{ sprintf( __( 'Format name: %s' ), format.name ) }</p>;
* }
* ```
*
* @return {?Object} Format type.
*/
export function getFormatTypeForBareElement(state, bareElementTagName) {
const formatTypes = getFormatTypes(state);
return formatTypes.find(({
className,
tagName
}) => {
return className === null && bareElementTagName === tagName;
}) || formatTypes.find(({
className,
tagName
}) => {
return className === null && '*' === tagName;
});
}
/**
* Gets the format type, if any, that can handle an element, given its classes.
*
* @param {Object} state Data state.
* @param {string} elementClassName The classes of the element to find a format
* type for.
*
* @example
* ```js
* import { __, sprintf } from '@wordpress/i18n';
* import { store as richTextStore } from '@wordpress/rich-text';
* import { useSelect } from '@wordpress/data';
*
* const ExampleComponent = () => {
* const { getFormatTypeForClassName } = useSelect(
* ( select ) => select( richTextStore ),
* []
* );
*
* const format = getFormatTypeForClassName( 'has-inline-color' );
*
* return format && <p>{ sprintf( __( 'Format name: %s' ), format.name ) }</p>;
* };
* ```
*
* @return {?Object} Format type.
*/
export function getFormatTypeForClassName(state, elementClassName) {
return getFormatTypes(state).find(({
className
}) => {
if (className === null) {
return false;
}
return ` ${elementClassName} `.indexOf(` ${className} `) >= 0;
});
}
//# sourceMappingURL=selectors.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,282 @@
/**
* Internal dependencies
*/
import { toTree } from './to-tree';
import { createElement } from './create-element';
import { isRangeEqual } from './is-range-equal';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Creates a path as an array of indices from the given root node to the given
* node.
*
* @param {Node} node Node to find the path of.
* @param {HTMLElement} rootNode Root node to find the path from.
* @param {Array} path Initial path to build on.
*
* @return {Array} The path from the root node to the node.
*/
function createPathToNode(node, rootNode, path) {
const parentNode = node.parentNode;
let i = 0;
while (node = node.previousSibling) {
i++;
}
path = [i, ...path];
if (parentNode !== rootNode) {
path = createPathToNode(parentNode, rootNode, path);
}
return path;
}
/**
* Gets a node given a path (array of indices) from the given node.
*
* @param {HTMLElement} node Root node to find the wanted node in.
* @param {Array} path Path (indices) to the wanted node.
*
* @return {Object} Object with the found node and the remaining offset (if any).
*/
function getNodeByPath(node, path) {
path = [...path];
while (node && path.length > 1) {
node = node.childNodes[path.shift()];
}
return {
node,
offset: path[0]
};
}
function append(element, child) {
if (child.html !== undefined) {
return element.innerHTML += child.html;
}
if (typeof child === 'string') {
child = element.ownerDocument.createTextNode(child);
}
const {
type,
attributes
} = child;
if (type) {
child = element.ownerDocument.createElement(type);
for (const key in attributes) {
child.setAttribute(key, attributes[key]);
}
}
return element.appendChild(child);
}
function appendText(node, text) {
node.appendData(text);
}
function getLastChild({
lastChild
}) {
return lastChild;
}
function getParent({
parentNode
}) {
return parentNode;
}
function isText(node) {
return node.nodeType === node.TEXT_NODE;
}
function getText({
nodeValue
}) {
return nodeValue;
}
function remove(node) {
return node.parentNode.removeChild(node);
}
export function toDom({
value,
prepareEditableTree,
isEditableTree = true,
placeholder,
doc = document
}) {
let startPath = [];
let endPath = [];
if (prepareEditableTree) {
value = {
...value,
formats: prepareEditableTree(value)
};
}
/**
* Returns a new instance of a DOM tree upon which RichText operations can be
* applied.
*
* Note: The current implementation will return a shared reference, reset on
* each call to `createEmpty`. Therefore, you should not hold a reference to
* the value to operate upon asynchronously, as it may have unexpected results.
*
* @return {Object} RichText tree.
*/
const createEmpty = () => createElement(doc, '');
const tree = toTree({
value,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText,
onStartIndex(body, pointer) {
startPath = createPathToNode(pointer, body, [pointer.nodeValue.length]);
},
onEndIndex(body, pointer) {
endPath = createPathToNode(pointer, body, [pointer.nodeValue.length]);
},
isEditableTree,
placeholder
});
return {
body: tree,
selection: {
startPath,
endPath
}
};
}
/**
* Create an `Element` tree from a Rich Text value and applies the difference to
* the `Element` tree contained by `current`.
*
* @param {Object} $1 Named arguments.
* @param {RichTextValue} $1.value Value to apply.
* @param {HTMLElement} $1.current The live root node to apply the element tree to.
* @param {Function} [$1.prepareEditableTree] Function to filter editorable formats.
* @param {boolean} [$1.__unstableDomOnly] Only apply elements, no selection.
* @param {string} [$1.placeholder] Placeholder text.
*/
export function apply({
value,
current,
prepareEditableTree,
__unstableDomOnly,
placeholder
}) {
// Construct a new element tree in memory.
const {
body,
selection
} = toDom({
value,
prepareEditableTree,
placeholder,
doc: current.ownerDocument
});
applyValue(body, current);
if (value.start !== undefined && !__unstableDomOnly) {
applySelection(selection, current);
}
}
export function applyValue(future, current) {
let i = 0;
let futureChild;
while (futureChild = future.firstChild) {
const currentChild = current.childNodes[i];
if (!currentChild) {
current.appendChild(futureChild);
} else if (!currentChild.isEqualNode(futureChild)) {
if (currentChild.nodeName !== futureChild.nodeName || currentChild.nodeType === currentChild.TEXT_NODE && currentChild.data !== futureChild.data) {
current.replaceChild(futureChild, currentChild);
} else {
const currentAttributes = currentChild.attributes;
const futureAttributes = futureChild.attributes;
if (currentAttributes) {
let ii = currentAttributes.length;
// Reverse loop because `removeAttribute` on `currentChild`
// changes `currentAttributes`.
while (ii--) {
const {
name
} = currentAttributes[ii];
if (!futureChild.getAttribute(name)) {
currentChild.removeAttribute(name);
}
}
}
if (futureAttributes) {
for (let ii = 0; ii < futureAttributes.length; ii++) {
const {
name,
value
} = futureAttributes[ii];
if (currentChild.getAttribute(name) !== value) {
currentChild.setAttribute(name, value);
}
}
}
applyValue(futureChild, currentChild);
future.removeChild(futureChild);
}
} else {
future.removeChild(futureChild);
}
i++;
}
while (current.childNodes[i]) {
current.removeChild(current.childNodes[i]);
}
}
export function applySelection({
startPath,
endPath
}, current) {
const {
node: startContainer,
offset: startOffset
} = getNodeByPath(current, startPath);
const {
node: endContainer,
offset: endOffset
} = getNodeByPath(current, endPath);
const {
ownerDocument
} = current;
const {
defaultView
} = ownerDocument;
const selection = defaultView.getSelection();
const range = ownerDocument.createRange();
range.setStart(startContainer, startOffset);
range.setEnd(endContainer, endOffset);
const {
activeElement
} = ownerDocument;
if (selection.rangeCount > 0) {
// If the to be added range and the live range are the same, there's no
// need to remove the live range and add the equivalent range.
if (isRangeEqual(range, selection.getRangeAt(0))) {
return;
}
selection.removeAllRanges();
}
selection.addRange(range);
// This function is not intended to cause a shift in focus. Since the above
// selection manipulations may shift focus, ensure that focus is restored to
// its previous state.
if (activeElement !== ownerDocument.activeElement) {
// The `instanceof` checks protect against edge cases where the focused
// element is not of the interface HTMLElement (does not have a `focus`
// or `blur` property).
//
// See: https://github.com/Microsoft/TypeScript/issues/5901#issuecomment-431649653
if (activeElement instanceof defaultView.HTMLElement) {
activeElement.focus();
}
}
}
//# sourceMappingURL=to-dom.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,112 @@
/**
* WordPress dependencies
*/
import { escapeEditableHTML, escapeAttribute, isValidAttributeName } from '@wordpress/escape-html';
/**
* Internal dependencies
*/
import { toTree } from './to-tree';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/**
* Create an HTML string from a Rich Text value.
*
* @param {Object} $1 Named argements.
* @param {RichTextValue} $1.value Rich text value.
* @param {boolean} [$1.preserveWhiteSpace] Preserves newlines if true.
*
* @return {string} HTML string.
*/
export function toHTMLString({
value,
preserveWhiteSpace
}) {
const tree = toTree({
value,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText
});
return createChildrenHTML(tree.children);
}
function createEmpty() {
return {};
}
function getLastChild({
children
}) {
return children && children[children.length - 1];
}
function append(parent, object) {
if (typeof object === 'string') {
object = {
text: object
};
}
object.parent = parent;
parent.children = parent.children || [];
parent.children.push(object);
return object;
}
function appendText(object, text) {
object.text += text;
}
function getParent({
parent
}) {
return parent;
}
function isText({
text
}) {
return typeof text === 'string';
}
function getText({
text
}) {
return text;
}
function remove(object) {
const index = object.parent.children.indexOf(object);
if (index !== -1) {
object.parent.children.splice(index, 1);
}
return object;
}
function createElementHTML({
type,
attributes,
object,
children
}) {
let attributeString = '';
for (const key in attributes) {
if (!isValidAttributeName(key)) {
continue;
}
attributeString += ` ${key}="${escapeAttribute(attributes[key])}"`;
}
if (object) {
return `<${type}${attributeString}>`;
}
return `<${type}${attributeString}>${createChildrenHTML(children)}</${type}>`;
}
function createChildrenHTML(children = []) {
return children.map(child => {
if (child.html !== undefined) {
return child.html;
}
return child.text === undefined ? createElementHTML(child) : escapeEditableHTML(child.text);
}).join('');
}
//# sourceMappingURL=to-html-string.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,275 @@
/**
* Internal dependencies
*/
import { getActiveFormats } from './get-active-formats';
import { getFormatType } from './get-format-type';
import { OBJECT_REPLACEMENT_CHARACTER, ZWNBSP } from './special-characters';
function restoreOnAttributes(attributes, isEditableTree) {
if (isEditableTree) {
return attributes;
}
const newAttributes = {};
for (const key in attributes) {
let newKey = key;
if (key.startsWith('data-disable-rich-text-')) {
newKey = key.slice('data-disable-rich-text-'.length);
}
newAttributes[newKey] = attributes[key];
}
return newAttributes;
}
/**
* Converts a format object to information that can be used to create an element
* from (type, attributes and object).
*
* @param {Object} $1 Named parameters.
* @param {string} $1.type The format type.
* @param {string} $1.tagName The tag name.
* @param {Object} $1.attributes The format attributes.
* @param {Object} $1.unregisteredAttributes The unregistered format
* attributes.
* @param {boolean} $1.object Whether or not it is an object
* format.
* @param {boolean} $1.boundaryClass Whether or not to apply a boundary
* class.
* @param {boolean} $1.isEditableTree
*
* @return {Object} Information to be used for element creation.
*/
function fromFormat({
type,
tagName,
attributes,
unregisteredAttributes,
object,
boundaryClass,
isEditableTree
}) {
const formatType = getFormatType(type);
let elementAttributes = {};
if (boundaryClass && isEditableTree) {
elementAttributes['data-rich-text-format-boundary'] = 'true';
}
if (!formatType) {
if (attributes) {
elementAttributes = {
...attributes,
...elementAttributes
};
}
return {
type,
attributes: restoreOnAttributes(elementAttributes, isEditableTree),
object
};
}
elementAttributes = {
...unregisteredAttributes,
...elementAttributes
};
for (const name in attributes) {
const key = formatType.attributes ? formatType.attributes[name] : false;
if (key) {
elementAttributes[key] = attributes[name];
} else {
elementAttributes[name] = attributes[name];
}
}
if (formatType.className) {
if (elementAttributes.class) {
elementAttributes.class = `${formatType.className} ${elementAttributes.class}`;
} else {
elementAttributes.class = formatType.className;
}
}
// When a format is declared as non editable, make it non editable in the
// editor.
if (isEditableTree && formatType.contentEditable === false) {
elementAttributes.contenteditable = 'false';
}
return {
type: tagName || formatType.tagName,
object: formatType.object,
attributes: restoreOnAttributes(elementAttributes, isEditableTree)
};
}
/**
* Checks if both arrays of formats up until a certain index are equal.
*
* @param {Array} a Array of formats to compare.
* @param {Array} b Array of formats to compare.
* @param {number} index Index to check until.
*/
function isEqualUntil(a, b, index) {
do {
if (a[index] !== b[index]) {
return false;
}
} while (index--);
return true;
}
export function toTree({
value,
preserveWhiteSpace,
createEmpty,
append,
getLastChild,
getParent,
isText,
getText,
remove,
appendText,
onStartIndex,
onEndIndex,
isEditableTree,
placeholder
}) {
const {
formats,
replacements,
text,
start,
end
} = value;
const formatsLength = formats.length + 1;
const tree = createEmpty();
const activeFormats = getActiveFormats(value);
const deepestActiveFormat = activeFormats[activeFormats.length - 1];
let lastCharacterFormats;
let lastCharacter;
append(tree, '');
for (let i = 0; i < formatsLength; i++) {
const character = text.charAt(i);
const shouldInsertPadding = isEditableTree && (
// Pad the line if the line is empty.
!lastCharacter ||
// Pad the line if the previous character is a line break, otherwise
// the line break won't be visible.
lastCharacter === '\n');
const characterFormats = formats[i];
let pointer = getLastChild(tree);
if (characterFormats) {
characterFormats.forEach((format, formatIndex) => {
if (pointer && lastCharacterFormats &&
// Reuse the last element if all formats remain the same.
isEqualUntil(characterFormats, lastCharacterFormats, formatIndex)) {
pointer = getLastChild(pointer);
return;
}
const {
type,
tagName,
attributes,
unregisteredAttributes
} = format;
const boundaryClass = isEditableTree && format === deepestActiveFormat;
const parent = getParent(pointer);
const newNode = append(parent, fromFormat({
type,
tagName,
attributes,
unregisteredAttributes,
boundaryClass,
isEditableTree
}));
if (isText(pointer) && getText(pointer).length === 0) {
remove(pointer);
}
pointer = append(newNode, '');
});
}
// If there is selection at 0, handle it before characters are inserted.
if (i === 0) {
if (onStartIndex && start === 0) {
onStartIndex(tree, pointer);
}
if (onEndIndex && end === 0) {
onEndIndex(tree, pointer);
}
}
if (character === OBJECT_REPLACEMENT_CHARACTER) {
const replacement = replacements[i];
if (!replacement) {
continue;
}
const {
type,
attributes,
innerHTML
} = replacement;
const formatType = getFormatType(type);
if (!isEditableTree && type === 'script') {
pointer = append(getParent(pointer), fromFormat({
type: 'script',
isEditableTree
}));
append(pointer, {
html: decodeURIComponent(attributes['data-rich-text-script'])
});
} else if (formatType?.contentEditable === false) {
// For non editable formats, render the stored inner HTML.
pointer = append(getParent(pointer), fromFormat({
...replacement,
isEditableTree,
boundaryClass: start === i && end === i + 1
}));
if (innerHTML) {
append(pointer, {
html: innerHTML
});
}
} else {
pointer = append(getParent(pointer), fromFormat({
...replacement,
object: true,
isEditableTree
}));
}
// Ensure pointer is text node.
pointer = append(getParent(pointer), '');
} else if (!preserveWhiteSpace && character === '\n') {
pointer = append(getParent(pointer), {
type: 'br',
attributes: isEditableTree ? {
'data-rich-text-line-break': 'true'
} : undefined,
object: true
});
// Ensure pointer is text node.
pointer = append(getParent(pointer), '');
} else if (!isText(pointer)) {
pointer = append(getParent(pointer), character);
} else {
appendText(pointer, character);
}
if (onStartIndex && start === i + 1) {
onStartIndex(tree, pointer);
}
if (onEndIndex && end === i + 1) {
onEndIndex(tree, pointer);
}
if (shouldInsertPadding && i === text.length) {
append(getParent(pointer), ZWNBSP);
if (placeholder && text.length === 0) {
append(getParent(pointer), {
type: 'span',
attributes: {
'data-rich-text-placeholder': placeholder,
// Necessary to prevent the placeholder from catching
// selection and being editable.
style: 'pointer-events:none;user-select:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;'
}
});
}
}
lastCharacterFormats = characterFormats;
lastCharacter = character;
}
return tree;
}
//# sourceMappingURL=to-tree.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { speak } from '@wordpress/a11y';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getActiveFormat } from './get-active-format';
import { removeFormat } from './remove-format';
import { applyFormat } from './apply-format';
/** @typedef {import('./types').RichTextValue} RichTextValue */
/** @typedef {import('./types').RichTextFormat} RichTextFormat */
/**
* Toggles a format object to a Rich Text value at the current selection.
*
* @param {RichTextValue} value Value to modify.
* @param {RichTextFormat} format Format to apply or remove.
*
* @return {RichTextValue} A new value with the format applied or removed.
*/
export function toggleFormat(value, format) {
if (getActiveFormat(value, format.type)) {
// For screen readers, will announce if formatting control is disabled.
if (format.title) {
// translators: %s: title of the formatting control
speak(sprintf(__('%s removed.'), format.title), 'assertive');
}
return removeFormat(value, format.type);
}
// For screen readers, will announce if formatting control is enabled.
if (format.title) {
// translators: %s: title of the formatting control
speak(sprintf(__('%s applied.'), format.title), 'assertive');
}
return applyFormat(value, format);
}
//# sourceMappingURL=toggle-format.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":["speak","__","sprintf","getActiveFormat","removeFormat","applyFormat","toggleFormat","value","format","type","title"],"sources":["@wordpress/rich-text/src/toggle-format.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\n\nimport { speak } from '@wordpress/a11y';\nimport { __, sprintf } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\n\nimport { getActiveFormat } from './get-active-format';\nimport { removeFormat } from './remove-format';\nimport { applyFormat } from './apply-format';\n\n/** @typedef {import('./types').RichTextValue} RichTextValue */\n/** @typedef {import('./types').RichTextFormat} RichTextFormat */\n\n/**\n * Toggles a format object to a Rich Text value at the current selection.\n *\n * @param {RichTextValue} value Value to modify.\n * @param {RichTextFormat} format Format to apply or remove.\n *\n * @return {RichTextValue} A new value with the format applied or removed.\n */\nexport function toggleFormat( value, format ) {\n\tif ( getActiveFormat( value, format.type ) ) {\n\t\t// For screen readers, will announce if formatting control is disabled.\n\t\tif ( format.title ) {\n\t\t\t// translators: %s: title of the formatting control\n\t\t\tspeak( sprintf( __( '%s removed.' ), format.title ), 'assertive' );\n\t\t}\n\t\treturn removeFormat( value, format.type );\n\t}\n\t// For screen readers, will announce if formatting control is enabled.\n\tif ( format.title ) {\n\t\t// translators: %s: title of the formatting control\n\t\tspeak( sprintf( __( '%s applied.' ), format.title ), 'assertive' );\n\t}\n\treturn applyFormat( value, format );\n}\n"],"mappings":"AAAA;AACA;AACA;;AAEA,SAASA,KAAK,QAAQ,iBAAiB;AACvC,SAASC,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;;AAE7C;AACA;AACA;;AAEA,SAASC,eAAe,QAAQ,qBAAqB;AACrD,SAASC,YAAY,QAAQ,iBAAiB;AAC9C,SAASC,WAAW,QAAQ,gBAAgB;;AAE5C;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASC,YAAYA,CAAEC,KAAK,EAAEC,MAAM,EAAG;EAC7C,IAAKL,eAAe,CAAEI,KAAK,EAAEC,MAAM,CAACC,IAAK,CAAC,EAAG;IAC5C;IACA,IAAKD,MAAM,CAACE,KAAK,EAAG;MACnB;MACAV,KAAK,CAAEE,OAAO,CAAED,EAAE,CAAE,aAAc,CAAC,EAAEO,MAAM,CAACE,KAAM,CAAC,EAAE,WAAY,CAAC;IACnE;IACA,OAAON,YAAY,CAAEG,KAAK,EAAEC,MAAM,CAACC,IAAK,CAAC;EAC1C;EACA;EACA,IAAKD,MAAM,CAACE,KAAK,EAAG;IACnB;IACAV,KAAK,CAAEE,OAAO,CAAED,EAAE,CAAE,aAAc,CAAC,EAAEO,MAAM,CAACE,KAAM,CAAC,EAAE,WAAY,CAAC;EACnE;EACA,OAAOL,WAAW,CAAEE,KAAK,EAAEC,MAAO,CAAC;AACpC","ignoreList":[]}

View File

@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=types.js.map

View File

@@ -0,0 +1 @@
{"version":3,"names":[],"sources":["@wordpress/rich-text/src/types.ts"],"sourcesContent":["/**\n * Stores the type of a rich text format, such as core/bold.\n */\nexport type RichTextFormat = {\n\ttype:\n\t\t| 'core/bold'\n\t\t| 'core/italic'\n\t\t| 'core/link '\n\t\t| 'core/strikethrough'\n\t\t| 'core/image'\n\t\t| string;\n};\n\n/**\n * A list of rich text format types.\n */\nexport type RichTextFormatList = Array< RichTextFormat >;\n\n/**\n * An object which represents a formatted string. The text property contains the\n * text to be formatted, and the formats property contains an array which indicates\n * the formats that are applied to each character in the text. See the main\n * `@wordpress/rich-text` documentation for more detail.\n */\nexport type RichTextValue = {\n\ttext: string;\n\tformats: Array< RichTextFormatList >;\n\treplacements: Array< RichTextFormat >;\n\tstart: number;\n\tend: number;\n};\n"],"mappings":"","ignoreList":[]}

View File

@@ -0,0 +1,31 @@
/**
* WordPress dependencies
*/
import { select, dispatch } from '@wordpress/data';
/**
* Internal dependencies
*/
import { store as richTextStore } from './store';
/** @typedef {import('./register-format-type').WPFormat} WPFormat */
/**
* Unregisters a format.
*
* @param {string} name Format name.
*
* @return {WPFormat|undefined} The previous format value, if it has
* been successfully unregistered;
* otherwise `undefined`.
*/
export function unregisterFormatType(name) {
const oldFormat = select(richTextStore).getFormatType(name);
if (!oldFormat) {
window.console.error(`Format ${name} is not registered.`);
return;
}
dispatch(richTextStore).removeFormatTypes(name);
return oldFormat;
}
//# sourceMappingURL=unregister-format-type.js.map

Some files were not shown because too many files have changed in this diff Show More