@rathkit/rte-core 0.0.0-stage → 0.1.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +4 -0
- package/README.md +44 -2
- package/dist/autoformat/autoformat.d.ts +12 -0
- package/dist/autoformat/autoformat.js +100 -0
- package/dist/autoformat/index.d.ts +2 -0
- package/dist/autoformat/index.js +2 -0
- package/dist/autoformat/plugin.d.ts +10 -0
- package/dist/autoformat/plugin.js +15 -0
- package/dist/base64.d.ts +2 -0
- package/dist/base64.js +32 -0
- package/dist/behaviors.d.ts +22 -0
- package/dist/behaviors.js +1 -0
- package/dist/blocks.d.ts +47 -0
- package/dist/blocks.js +225 -0
- package/dist/cellborder.d.ts +16 -0
- package/dist/cellborder.js +56 -0
- package/dist/commands-core.d.ts +9 -0
- package/dist/commands-core.js +238 -0
- package/dist/commands.d.ts +66 -0
- package/dist/commands.js +89 -0
- package/dist/core.d.ts +2 -0
- package/dist/core.js +12 -0
- package/dist/dom/imageview.d.ts +21 -0
- package/dist/dom/imageview.js +122 -0
- package/dist/dom/index.d.ts +2 -0
- package/dist/dom/index.js +2 -0
- package/dist/dom/render.d.ts +40 -0
- package/dist/dom/render.js +397 -0
- package/dist/dom/tablegrips.d.ts +22 -0
- package/dist/dom/tablegrips.js +198 -0
- package/dist/dom/tableview.d.ts +22 -0
- package/dist/dom/tableview.js +158 -0
- package/dist/dom/view.d.ts +139 -0
- package/dist/dom/view.js +578 -0
- package/dist/edit.d.ts +57 -0
- package/dist/edit.js +527 -0
- package/dist/engine.d.ts +149 -0
- package/dist/engine.js +320 -0
- package/dist/export.d.ts +4 -0
- package/dist/export.js +119 -0
- package/dist/find/find.d.ts +24 -0
- package/dist/find/find.js +68 -0
- package/dist/find/index.d.ts +2 -0
- package/dist/find/index.js +2 -0
- package/dist/find/plugin.d.ts +18 -0
- package/dist/find/plugin.js +117 -0
- package/dist/html/entities.d.ts +3 -0
- package/dist/html/entities.js +29 -0
- package/dist/html/parse.d.ts +14 -0
- package/dist/html/parse.js +142 -0
- package/dist/images/images.d.ts +18 -0
- package/dist/images/images.js +94 -0
- package/dist/images/index.d.ts +2 -0
- package/dist/images/index.js +2 -0
- package/dist/images/plugin.d.ts +23 -0
- package/dist/images/plugin.js +169 -0
- package/dist/imageselect.d.ts +12 -0
- package/dist/imageselect.js +13 -0
- package/dist/import.d.ts +65 -0
- package/dist/import.js +820 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +20 -0
- package/dist/keymap.d.ts +52 -0
- package/dist/keymap.js +88 -0
- package/dist/links.d.ts +11 -0
- package/dist/links.js +90 -0
- package/dist/lists/commands.d.ts +8 -0
- package/dist/lists/commands.js +73 -0
- package/dist/lists/definitions.d.ts +29 -0
- package/dist/lists/definitions.js +164 -0
- package/dist/lists/index.d.ts +3 -0
- package/dist/lists/index.js +3 -0
- package/dist/lists/lists.d.ts +91 -0
- package/dist/lists/lists.js +449 -0
- package/dist/lists/plugin.d.ts +10 -0
- package/dist/lists/plugin.js +41 -0
- package/dist/model.d.ts +141 -0
- package/dist/model.js +31 -0
- package/dist/ops.d.ts +79 -0
- package/dist/ops.js +22 -0
- package/dist/paste.d.ts +21 -0
- package/dist/paste.js +33 -0
- package/dist/path.d.ts +60 -0
- package/dist/path.js +274 -0
- package/dist/plugin.d.ts +56 -0
- package/dist/plugin.js +19 -0
- package/dist/standard/index.d.ts +25 -0
- package/dist/standard/index.js +32 -0
- package/dist/stats.d.ts +15 -0
- package/dist/stats.js +27 -0
- package/dist/svg/index.d.ts +2 -0
- package/dist/svg/index.js +2 -0
- package/dist/svg/plugin.d.ts +2 -0
- package/dist/svg/plugin.js +10 -0
- package/dist/svg/svg.d.ts +16 -0
- package/dist/svg/svg.js +216 -0
- package/dist/tablefill.d.ts +6 -0
- package/dist/tablefill.js +74 -0
- package/dist/tablegrid.d.ts +44 -0
- package/dist/tablegrid.js +107 -0
- package/dist/tables/commands.d.ts +6 -0
- package/dist/tables/commands.js +85 -0
- package/dist/tables/index.d.ts +2 -0
- package/dist/tables/index.js +2 -0
- package/dist/tables/plugin.d.ts +6 -0
- package/dist/tables/plugin.js +67 -0
- package/dist/tables/tables.d.ts +90 -0
- package/dist/tables/tables.js +498 -0
- package/dist/ui.d.ts +91 -0
- package/dist/ui.js +45 -0
- package/dist/word/index.d.ts +1 -0
- package/dist/word/index.js +1 -0
- package/dist/word/plugin.d.ts +2 -0
- package/dist/word/plugin.js +10 -0
- package/dist/word/recognizer.d.ts +15 -0
- package/dist/word/recognizer.js +109 -0
- package/package.json +155 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rathkit/rte-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> **Alpha** (0.1.0-alpha.0). Not for production: the APIs will change between releases. Published to the `next` tag only.
|
|
4
|
+
|
|
5
|
+
The editor itself: the document model, HTML import and export, editing operations, commands and keys, the plugin host, the browser view, and the free plugins (lists, tables, find and replace, word-processor lists, SVG, images). The model does not touch the DOM, so it also runs in Node.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @rathkit/rte-core@next
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
No dependencies, and nothing runs when it is installed.
|
|
14
|
+
|
|
15
|
+
## Entry points
|
|
16
|
+
|
|
17
|
+
- `@rathkit/rte-core`: model, import and export, engine (no DOM)
|
|
18
|
+
- `@rathkit/rte-core/dom`: the browser view (`Editor`)
|
|
19
|
+
- `@rathkit/rte-core/standard`: the editor with the free plugins already added (`createEditor`)
|
|
20
|
+
- `@rathkit/rte-core/lists`, `/tables`, `/find`, `/word`, `/svg`, `/images`: the plugins, one entry point each
|
|
21
|
+
|
|
22
|
+
## Example
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { createEditor } from "@rathkit/rte-core/standard";
|
|
26
|
+
|
|
27
|
+
const surface = document.createElement("div");
|
|
28
|
+
surface.setAttribute("aria-label", "Body"); // an unnamed editor is announced as just "text box"
|
|
29
|
+
document.body.append(surface);
|
|
30
|
+
|
|
31
|
+
const editor = createEditor({ element: surface, html: "<p>Hello</p>" });
|
|
32
|
+
|
|
33
|
+
editor.commands.run("format.bold"); // every action is a named command
|
|
34
|
+
editor.subscribe(() => console.log(editor.getHtml()));
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Give the element an `aria-label` (or `aria-labelledby`): an unnamed editor is announced as just "text box".
|
|
38
|
+
|
|
39
|
+
## More
|
|
40
|
+
|
|
41
|
+
Documentation: <https://rathkit.com>. Source and issues: <https://github.com/rathkit/rte>. What is checked: [SUPPORT.md](https://github.com/rathkit/rte/blob/main/docs/SUPPORT.md). Changes: [CHANGELOG.md](https://github.com/rathkit/rte/blob/main/CHANGELOG.md). How a release is verified: [VERIFY.md](https://github.com/rathkit/rte/blob/main/docs/VERIFY.md).
|
|
42
|
+
|
|
43
|
+
## License
|
|
44
|
+
|
|
45
|
+
[Apache-2.0](LICENSE)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Doc } from "../model.ts";
|
|
2
|
+
import type { TriggerResult } from "../behaviors.ts";
|
|
3
|
+
import { type EditResult, type Selection } from "../path.ts";
|
|
4
|
+
/** `#` to `######` and a space make a heading of that level; `>` and a space a quote. The space is not inserted. */
|
|
5
|
+
export declare function blockShortcut(doc: Doc, sel: Selection, typed: string): EditResult | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Typing the last marker of `**bold**`, `~~strike~~`, `*italic*`, `_italic_` or `` `code` `` removes the markers and marks what was
|
|
8
|
+
* between them. The caret ends up after the text, and the next characters typed are plain. The closing marker typed is not inserted.
|
|
9
|
+
*/
|
|
10
|
+
export declare function spanShortcut(doc: Doc, sel: Selection, typed: string): TriggerResult | undefined;
|
|
11
|
+
/** A web address before the caret, then a space: the address becomes a link and the space is typed after it. */
|
|
12
|
+
export declare function addressShortcut(doc: Doc, sel: Selection, typed: string): EditResult | undefined;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { inlineText } from "../model.js";
|
|
2
|
+
import { caret, getBlock, getContent, isCollapsed, kindAt, setContent } from "../path.js";
|
|
3
|
+
import { insertText, sliceInline } from "../edit.js";
|
|
4
|
+
import { setBlockType, toggleCodeBlock, toggleQuote, insertRule } from "../blocks.js";
|
|
5
|
+
import { setLink } from "../links.js";
|
|
6
|
+
import { normalizeInline } from "../model.js";
|
|
7
|
+
function lonePrefix(doc, sel) {
|
|
8
|
+
if (!isCollapsed(sel))
|
|
9
|
+
return undefined;
|
|
10
|
+
const path = sel.anchor.path;
|
|
11
|
+
const block = getBlock(doc, path);
|
|
12
|
+
if (block?.type !== "p" || kindAt(doc, path) !== "p")
|
|
13
|
+
return undefined;
|
|
14
|
+
const text = inlineText(block.content);
|
|
15
|
+
if (sel.anchor.offset !== text.length || block.content.some((n) => n.type !== "text" || n.marks.length > 0))
|
|
16
|
+
return undefined;
|
|
17
|
+
return { text, emptied: setContent(doc, path, []) };
|
|
18
|
+
}
|
|
19
|
+
export function blockShortcut(doc, sel, typed) {
|
|
20
|
+
const lone = typed === " " || typed === "`" || typed === "-" || typed === "*" || typed === "_" ? lonePrefix(doc, sel) : undefined;
|
|
21
|
+
if (!lone)
|
|
22
|
+
return undefined;
|
|
23
|
+
const at = caret({ path: sel.anchor.path, offset: 0 });
|
|
24
|
+
if (typed === " ") {
|
|
25
|
+
if (/^#{1,6}$/.test(lone.text))
|
|
26
|
+
return setBlockType(lone.emptied, at, { type: "h", level: lone.text.length });
|
|
27
|
+
if (lone.text === ">")
|
|
28
|
+
return toggleQuote(lone.emptied, at);
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
if (typed === "`" && lone.text === "``")
|
|
32
|
+
return toggleCodeBlock(lone.emptied, at);
|
|
33
|
+
if ((typed === "-" || typed === "*" || typed === "_") && lone.text === typed + typed)
|
|
34
|
+
return insertRule(lone.emptied, at);
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
const SPANS = [
|
|
38
|
+
{ close: "*", pattern: /(?<![*\w])\*\*(?=\S)([^*\n]+?)(?<=\S)\*$/, mark: { type: "bold" }, length: 2 },
|
|
39
|
+
{ close: "~", pattern: /(?<![~\w])~~(?=\S)([^~\n]+?)(?<=\S)~$/, mark: { type: "strike" }, length: 2 },
|
|
40
|
+
{ close: "*", pattern: /(?<![*\w])\*(?=[^\s*])([^*\n]+?)(?<=[^\s*])$/, mark: { type: "italic" }, length: 1 },
|
|
41
|
+
{ close: "_", pattern: /(?<![_\w])_(?=[^\s_])([^_\n]+?)(?<=[^\s_])$/, mark: { type: "italic" }, length: 1 },
|
|
42
|
+
{ close: "`", pattern: /(?<![`\w])`(?=[^\s`])([^`\n]+?)(?<=[^\s`])$/, mark: { type: "code" }, length: 1 },
|
|
43
|
+
];
|
|
44
|
+
export function spanShortcut(doc, sel, typed) {
|
|
45
|
+
if (!isCollapsed(sel) || typed.length !== 1)
|
|
46
|
+
return undefined;
|
|
47
|
+
const path = sel.anchor.path;
|
|
48
|
+
const kind = kindAt(doc, path);
|
|
49
|
+
if (kind === "pre")
|
|
50
|
+
return undefined;
|
|
51
|
+
const content = getContent(doc, path);
|
|
52
|
+
const offset = sel.anchor.offset;
|
|
53
|
+
const before = inlineText(sliceInline(content, 0, offset));
|
|
54
|
+
for (const span of SPANS) {
|
|
55
|
+
if (span.close !== typed)
|
|
56
|
+
continue;
|
|
57
|
+
const m = span.pattern.exec(before);
|
|
58
|
+
if (!m)
|
|
59
|
+
continue;
|
|
60
|
+
const start = m.index;
|
|
61
|
+
const inner = m[1];
|
|
62
|
+
const innerFrom = start + span.length;
|
|
63
|
+
const innerTo = innerFrom + inner.length;
|
|
64
|
+
if (innerTo > offset)
|
|
65
|
+
continue;
|
|
66
|
+
const middle = sliceInline(content, innerFrom, innerTo);
|
|
67
|
+
if (middle.some((n) => n.type !== "text" || n.marks.some((x) => x.type === span.mark.type)))
|
|
68
|
+
continue;
|
|
69
|
+
const marked = middle.map((n) => (n.type === "text" ? { ...n, marks: [...n.marks, span.mark] } : n));
|
|
70
|
+
const next = normalizeInline([...sliceInline(content, 0, start), ...marked, ...sliceInline(content, offset)]);
|
|
71
|
+
const last = sliceInline(content, 0, start).at(-1);
|
|
72
|
+
return { doc: setContent(doc, path, next), selection: caret({ path, offset: start + inner.length }), storedMarks: last?.type === "text" ? last.marks : [] };
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
const ADDRESS = /(?:https?:\/\/|www\.)[^\s<>"]+$/i;
|
|
77
|
+
export function addressShortcut(doc, sel, typed) {
|
|
78
|
+
if (typed !== " " || !isCollapsed(sel))
|
|
79
|
+
return undefined;
|
|
80
|
+
const path = sel.anchor.path;
|
|
81
|
+
if (kindAt(doc, path) === "pre")
|
|
82
|
+
return undefined;
|
|
83
|
+
const content = getContent(doc, path);
|
|
84
|
+
const offset = sel.anchor.offset;
|
|
85
|
+
const before = inlineText(sliceInline(content, 0, offset));
|
|
86
|
+
const m = ADDRESS.exec(before);
|
|
87
|
+
if (!m)
|
|
88
|
+
return undefined;
|
|
89
|
+
const word = m[0].replace(/[.,;:!?)\]'"]+$/, "");
|
|
90
|
+
if (word.replace(/^(?:https?:\/\/|www\.)/i, "").length < 3 || !/[a-z0-9]/i.test(word.replace(/^(?:https?:\/\/|www\.)/i, "")))
|
|
91
|
+
return undefined;
|
|
92
|
+
const start = m.index;
|
|
93
|
+
const end = start + word.length;
|
|
94
|
+
const run = sliceInline(content, start, end);
|
|
95
|
+
if (run.some((n) => n.type !== "text" || n.marks.some((x) => x.type === "link")))
|
|
96
|
+
return undefined;
|
|
97
|
+
const typedSpace = insertText(doc, caret({ path, offset }), " ");
|
|
98
|
+
const linked = setLink(typedSpace.doc, { anchor: { path, offset: start }, focus: { path, offset: end } }, /^www\./i.test(word) ? `https://${word}` : word);
|
|
99
|
+
return linked.doc === typedSpace.doc ? undefined : { doc: linked.doc, selection: typedSpace.selection };
|
|
100
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Plugin } from "../plugin.ts";
|
|
2
|
+
export interface AutoformatOptions {
|
|
3
|
+
/** `# ` heading, `> ` quote, three backticks a code block, three dashes a rule. Default on. */
|
|
4
|
+
readonly blocks?: boolean;
|
|
5
|
+
/** `**bold**`, `*italic*`, `_italic_`, `~~strike~~`, `` `code` ``. Default on. */
|
|
6
|
+
readonly spans?: boolean;
|
|
7
|
+
/** A web address followed by a space becomes a link. Default on. */
|
|
8
|
+
readonly links?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export declare function autoformat(options?: AutoformatOptions): Plugin;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { addressShortcut, blockShortcut, spanShortcut } from "./autoformat.js";
|
|
2
|
+
export function autoformat(options = {}) {
|
|
3
|
+
return {
|
|
4
|
+
id: "rathkit.autoformat",
|
|
5
|
+
api: "1",
|
|
6
|
+
setup(host) {
|
|
7
|
+
if (options.blocks ?? true)
|
|
8
|
+
host.textTrigger(blockShortcut);
|
|
9
|
+
if (options.spans ?? true)
|
|
10
|
+
host.textTrigger(spanShortcut);
|
|
11
|
+
if (options.links ?? true)
|
|
12
|
+
host.textTrigger(addressShortcut);
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
package/dist/base64.d.ts
ADDED
package/dist/base64.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
2
|
+
export function decodeBase64(input) {
|
|
3
|
+
const text = input.replace(/\s+/g, "").replace(/=+$/, "");
|
|
4
|
+
if (/[^A-Za-z0-9+/]/.test(text))
|
|
5
|
+
return undefined;
|
|
6
|
+
const out = new Uint8Array(Math.floor((text.length * 3) / 4));
|
|
7
|
+
let bits = 0;
|
|
8
|
+
let value = 0;
|
|
9
|
+
let at = 0;
|
|
10
|
+
for (const ch of text) {
|
|
11
|
+
value = (value << 6) | B64.indexOf(ch);
|
|
12
|
+
bits += 6;
|
|
13
|
+
if (bits >= 8) {
|
|
14
|
+
bits -= 8;
|
|
15
|
+
out[at++] = (value >> bits) & 0xff;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return out.subarray(0, at);
|
|
19
|
+
}
|
|
20
|
+
export function encodeBase64(bytes) {
|
|
21
|
+
let out = "";
|
|
22
|
+
for (let i = 0; i < bytes.length; i += 3) {
|
|
23
|
+
const a = bytes[i];
|
|
24
|
+
const b = bytes[i + 1];
|
|
25
|
+
const c = bytes[i + 2];
|
|
26
|
+
out += B64[a >> 2];
|
|
27
|
+
out += B64[((a & 3) << 4) | ((b ?? 0) >> 4)];
|
|
28
|
+
out += b === undefined ? "=" : B64[((b & 15) << 2) | ((c ?? 0) >> 6)];
|
|
29
|
+
out += c === undefined ? "=" : B64[c & 63];
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Block, Doc, Inline, Mark } from "./model.ts";
|
|
2
|
+
import type { EditResult, Pos, Selection } from "./path.ts";
|
|
3
|
+
/** The kinds of container a plugin can own. Paragraphs, headings and code blocks belong to the editor itself. */
|
|
4
|
+
export type ContainerKind = "li" | "dt" | "dd";
|
|
5
|
+
export interface ContainerBehavior {
|
|
6
|
+
/** Enter in a container of this kind, with the selection already removed. Undefined: use the default (a line break). */
|
|
7
|
+
split?(doc: Doc, pos: Pos): EditResult | undefined;
|
|
8
|
+
/** Backspace at the very start of a container of this kind. Undefined: use the default (join with the one before). */
|
|
9
|
+
backspaceAtStart?(doc: Doc, pos: Pos): EditResult | undefined;
|
|
10
|
+
/** Several blocks pasted into a container of this kind, with the text before and after the caret. Undefined: use the default. */
|
|
11
|
+
insertBlocks?(doc: Doc, pos: Pos, blocks: readonly Block[], before: Inline[], after: Inline[]): EditResult | undefined;
|
|
12
|
+
}
|
|
13
|
+
export type Behaviors = Partial<Record<ContainerKind, ContainerBehavior>>;
|
|
14
|
+
/**
|
|
15
|
+
* What a trigger returns to take over. `storedMarks` are the marks for the next text typed at the caret: `[]` for plain text, so that
|
|
16
|
+
* text typed after a conversion does not continue the formatting it made.
|
|
17
|
+
*/
|
|
18
|
+
export type TriggerResult = EditResult & {
|
|
19
|
+
readonly storedMarks?: readonly Mark[];
|
|
20
|
+
};
|
|
21
|
+
/** Runs when text is typed, before it is inserted. Return a result to take over (the typed text is then not inserted). */
|
|
22
|
+
export type TextTrigger = (doc: Doc, sel: Selection, typed: string) => TriggerResult | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/blocks.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type Align, type Direction, type Doc } from "./model.ts";
|
|
2
|
+
import { type EditResult, type Selection } from "./path.ts";
|
|
3
|
+
export type BlockKind = {
|
|
4
|
+
readonly type: "p";
|
|
5
|
+
} | {
|
|
6
|
+
readonly type: "h";
|
|
7
|
+
readonly level: 1 | 2 | 3 | 4 | 5 | 6;
|
|
8
|
+
};
|
|
9
|
+
/** The kind of the block where the selection starts: "p", "h" with a level, "pre", or a list item, entry and so on. */
|
|
10
|
+
export declare function blockKindAt(doc: Doc, sel: Selection): {
|
|
11
|
+
type: string;
|
|
12
|
+
level?: number;
|
|
13
|
+
} | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Makes every selected paragraph, heading or code block the given kind. When the first one already is that kind, every
|
|
16
|
+
* selected block goes back to a paragraph, so the same command toggles. List items, entries and tables are left alone.
|
|
17
|
+
* Marks inside a code block do not exist, so a code block turned into text has none.
|
|
18
|
+
*/
|
|
19
|
+
export declare function setBlockType(doc: Doc, sel: Selection, kind: BlockKind): EditResult;
|
|
20
|
+
/** The alignment of the block where the selection starts. Undefined means the default. */
|
|
21
|
+
export declare function alignAt(doc: Doc, sel: Selection): Align | undefined;
|
|
22
|
+
/** Aligns every selected paragraph and heading. `left` is the default and is stored as no alignment at all. */
|
|
23
|
+
export declare function setAlign(doc: Doc, sel: Selection, align: Align): EditResult;
|
|
24
|
+
/** Whether the block where the selection starts is a code block. */
|
|
25
|
+
export declare const inCodeBlock: (doc: Doc, sel: Selection) => boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Makes the selected paragraphs and headings code blocks, or, when the first one already is a code block, turns the selected code blocks
|
|
28
|
+
* back into paragraphs. Marks and alignment do not exist in code, so they are dropped on the way in.
|
|
29
|
+
*/
|
|
30
|
+
export declare function toggleCodeBlock(doc: Doc, sel: Selection): EditResult;
|
|
31
|
+
/** Whether the block where the selection starts sits directly in a quote. */
|
|
32
|
+
export declare function inQuote(doc: Doc, sel: Selection): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Puts the selected blocks (paragraphs, headings, code blocks and lists, side by side) inside a quote, or, when they are in one, takes
|
|
35
|
+
* them out of it (what is left of the quote on either side stays a quote).
|
|
36
|
+
*/
|
|
37
|
+
export declare function toggleQuote(doc: Doc, sel: Selection): EditResult;
|
|
38
|
+
/** Puts a horizontal rule after the block the caret is in (in place of an empty paragraph), with the caret in a paragraph after it. */
|
|
39
|
+
export declare function insertRule(doc: Doc, sel: Selection): EditResult;
|
|
40
|
+
/** The direction of the block where the selection starts. Undefined means it follows its surroundings. */
|
|
41
|
+
export declare function directionAt(doc: Doc, sel: Selection): Direction | undefined;
|
|
42
|
+
/** Sets which way every selected paragraph and heading reads; undefined puts it back to following its surroundings. */
|
|
43
|
+
export declare function setDirection(doc: Doc, sel: Selection, dir: Direction | undefined): EditResult;
|
|
44
|
+
/** How far in the block where the selection starts is moved, in steps (0 for not at all). */
|
|
45
|
+
export declare function indentAt(doc: Doc, sel: Selection): number;
|
|
46
|
+
/** Moves every selected paragraph and heading in (or out, with a negative step) by whole steps, between none and the most. */
|
|
47
|
+
export declare function changeIndent(doc: Doc, sel: Selection, step: number): EditResult;
|