@guardian/stand 0.0.0 → 0.0.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.
Files changed (39) hide show
  1. package/README.md +19 -0
  2. package/dist/byline/Byline.cjs +375 -0
  3. package/dist/byline/Byline.js +273 -0
  4. package/dist/byline/Preview.cjs +52 -0
  5. package/dist/byline/Preview.js +26 -0
  6. package/dist/byline/lib.cjs +240 -0
  7. package/dist/byline/lib.js +181 -0
  8. package/dist/byline/placeholder.cjs +29 -0
  9. package/dist/byline/placeholder.js +27 -0
  10. package/dist/byline/plugins.cjs +144 -0
  11. package/dist/byline/plugins.js +123 -0
  12. package/dist/byline/schema.cjs +66 -0
  13. package/dist/byline/schema.js +59 -0
  14. package/dist/byline/styles.cjs +244 -0
  15. package/dist/byline/styles.js +234 -0
  16. package/dist/index.cjs +4 -4
  17. package/dist/index.js +1 -5
  18. package/dist/types/.storybook/main.d.ts +3 -0
  19. package/dist/types/.storybook/preview.d.ts +3 -0
  20. package/dist/types/jest-setup-after-env.d.ts +1 -0
  21. package/dist/types/playwright/byline.mock.d.ts +3 -0
  22. package/dist/types/playwright/byline.spec.d.ts +1 -0
  23. package/dist/types/playwright-ct.config.d.ts +5 -0
  24. package/dist/types/src/byline/Byline.d.ts +17 -0
  25. package/dist/types/src/byline/Byline.stories.d.ts +206 -0
  26. package/dist/types/src/byline/Preview.d.ts +4 -0
  27. package/dist/types/src/byline/contributors-fixture.d.ts +1 -0
  28. package/dist/types/src/byline/lib.d.ts +48 -0
  29. package/dist/types/src/byline/lib.test.d.ts +1 -0
  30. package/dist/types/src/byline/placeholder.d.ts +2 -0
  31. package/dist/types/src/byline/plugins.d.ts +4 -0
  32. package/dist/types/src/byline/schema.d.ts +2 -0
  33. package/dist/types/src/byline/styles.d.ts +11 -0
  34. package/dist/types/src/byline/theme.d.ts +44 -0
  35. package/dist/types/src/byline/util.d.ts +3 -0
  36. package/dist/types/src/index.d.ts +2 -0
  37. package/package.json +60 -126
  38. package/LICENSE +0 -201
  39. package/dist/index.d.ts +0 -3
@@ -0,0 +1,123 @@
1
+ import { redo, undo } from 'prosemirror-history';
2
+ import { keymap } from 'prosemirror-keymap';
3
+ import { Slice } from 'prosemirror-model';
4
+ import { Plugin, NodeSelection } from 'prosemirror-state';
5
+ import { bylineEditorSchema } from './schema.js';
6
+
7
+ const serializeNode = (node, output) => {
8
+ if (node.isText) {
9
+ output.push(node.text ?? "");
10
+ } else if (node.type.name === "chip") {
11
+ output.push(node.attrs.label);
12
+ } else if (node.isInline) {
13
+ node.content.forEach((child) => serializeNode(child, output));
14
+ } else {
15
+ node.content.forEach((child) => serializeNode(child, output));
16
+ output.push("\n");
17
+ }
18
+ };
19
+ const transformPastedNode = (slice, allowUntaggedContributors, contributorLimit) => {
20
+ let contributorCount = 0;
21
+ const output = [];
22
+ const convertChipToPlaintext = (node, output2) => {
23
+ const label = node.attrs.label;
24
+ if (label) {
25
+ const textNode = bylineEditorSchema.text(label);
26
+ output2.push(textNode);
27
+ }
28
+ };
29
+ slice.content.forEach((node) => {
30
+ if (node.type.name === "chip") {
31
+ const type = node.attrs.type;
32
+ if (contributorLimit !== void 0 && contributorCount >= contributorLimit) {
33
+ convertChipToPlaintext(node, output);
34
+ return;
35
+ }
36
+ if (type === "untagged" && !allowUntaggedContributors) {
37
+ convertChipToPlaintext(node, output);
38
+ } else {
39
+ output.push(node);
40
+ contributorCount++;
41
+ }
42
+ } else if (node.isText) {
43
+ output.push(node);
44
+ }
45
+ });
46
+ return output;
47
+ };
48
+ const clipboardPlugin = (allowUntaggedContributors, contributorLimit) => {
49
+ return new Plugin({
50
+ props: {
51
+ // Custom serializer for copying content from the editor
52
+ clipboardTextSerializer: (slice) => {
53
+ const parts = [];
54
+ slice.content.forEach((node) => {
55
+ serializeNode(node, parts);
56
+ });
57
+ return parts.join("");
58
+ },
59
+ // Transform pasted content after ProseMirror's default parsing
60
+ transformPasted: (slice) => {
61
+ const transformedNodes = transformPastedNode(slice, allowUntaggedContributors, contributorLimit);
62
+ if (transformedNodes.length > 0) {
63
+ const newFragment = bylineEditorSchema.nodes.doc.create({}, transformedNodes).content;
64
+ return new Slice(newFragment, slice.openStart, slice.openEnd);
65
+ }
66
+ return slice;
67
+ }
68
+ }
69
+ });
70
+ };
71
+ const deleteSelectedChip = (state, dispatch) => {
72
+ const { selection } = state;
73
+ if (selection instanceof NodeSelection && selection.node.type.name === "chip") {
74
+ if (dispatch) {
75
+ const tr = state.tr.deleteSelection();
76
+ dispatch(tr);
77
+ }
78
+ return true;
79
+ }
80
+ return false;
81
+ };
82
+ const keybindings = () => keymap({
83
+ "Mod-z": undo,
84
+ "Mod-y": redo,
85
+ "Mod-shift-z": redo,
86
+ Backspace: deleteSelectedChip,
87
+ Delete: deleteSelectedChip
88
+ });
89
+ const bylinePlugin = () => {
90
+ return new Plugin({
91
+ props: {
92
+ nodeViews: {
93
+ chip: (node, view, getPos) => {
94
+ const dom = document.createElement("chip");
95
+ dom.setAttribute("data-label", node.attrs.label);
96
+ dom.setAttribute("data-type", node.attrs.type);
97
+ dom.setAttribute("data-tag-id", node.attrs.tagId);
98
+ dom.setAttribute("data-path", node.attrs.path);
99
+ if (node.attrs.meta) {
100
+ dom.setAttribute("data-meta", JSON.stringify(node.attrs.meta));
101
+ }
102
+ dom.textContent = node.attrs.label;
103
+ const deleteHandle = document.createElement("span");
104
+ deleteHandle.innerHTML = "\xD7";
105
+ deleteHandle.title = `Delete ${node.attrs.label}`;
106
+ deleteHandle.addEventListener("click", (event) => {
107
+ event.stopPropagation();
108
+ const pos = getPos();
109
+ if (pos === void 0) {
110
+ return;
111
+ }
112
+ const tr = view.state.tr.deleteRange(pos, pos + node.nodeSize);
113
+ view.dispatch(tr);
114
+ });
115
+ dom.appendChild(deleteHandle);
116
+ return { dom };
117
+ }
118
+ }
119
+ }
120
+ });
121
+ };
122
+
123
+ export { bylinePlugin, clipboardPlugin, keybindings };
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ var prosemirrorModel = require('prosemirror-model');
4
+
5
+ const bylineEditorSchema = new prosemirrorModel.Schema({
6
+ nodes: {
7
+ doc: {
8
+ content: "inline*"
9
+ },
10
+ chip: {
11
+ group: "inline",
12
+ inline: true,
13
+ atom: false,
14
+ selectable: true,
15
+ // allows selection for deletion/drag and drop
16
+ attrs: {
17
+ label: { default: "" },
18
+ type: { default: "untagged" },
19
+ tagId: { default: "" },
20
+ // tagId is optional for untagged contributors
21
+ path: { default: "" },
22
+ // path is optional for untagged contributors
23
+ meta: { default: {} }
24
+ // meta is optional and can hold any additional data
25
+ },
26
+ parseDOM: [
27
+ {
28
+ tag: "chip[data-label][data-type][data-tag-id][data-path][data-meta]",
29
+ getAttrs(dom) {
30
+ return {
31
+ label: dom.getAttribute("data-label"),
32
+ type: dom.getAttribute("data-type"),
33
+ tagId: dom.getAttribute("data-tag-id") ?? "",
34
+ path: dom.getAttribute("data-path") ?? "",
35
+ meta: dom.getAttribute("data-meta") ? JSON.parse(
36
+ dom.getAttribute("data-meta")
37
+ ) : {}
38
+ };
39
+ }
40
+ }
41
+ ],
42
+ toDOM(node) {
43
+ return [
44
+ "chip",
45
+ {
46
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- label exists as string on chip node
47
+ "data-label": node.attrs.label,
48
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- type exists as string on chip node
49
+ "data-type": node.attrs.type,
50
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- tagId exists as string on chip node
51
+ "data-tag-id": node.attrs.tagId,
52
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- path exists as string on chip node
53
+ "data-path": node.attrs.path,
54
+ "data-meta": node.attrs.meta ? JSON.stringify(node.attrs.meta) : {}
55
+ },
56
+ node.attrs.label
57
+ ];
58
+ }
59
+ },
60
+ text: {
61
+ group: "inline"
62
+ }
63
+ }
64
+ });
65
+
66
+ exports.bylineEditorSchema = bylineEditorSchema;
@@ -0,0 +1,59 @@
1
+ import { Schema } from 'prosemirror-model';
2
+
3
+ const bylineEditorSchema = new Schema({
4
+ nodes: {
5
+ doc: {
6
+ content: "inline*"
7
+ },
8
+ chip: {
9
+ group: "inline",
10
+ inline: true,
11
+ atom: false,
12
+ selectable: true,
13
+ attrs: {
14
+ label: { default: "" },
15
+ type: { default: "untagged" },
16
+ tagId: { default: "" },
17
+ path: { default: "" },
18
+ meta: { default: {} }
19
+ // meta is optional and can hold any additional data
20
+ },
21
+ parseDOM: [
22
+ {
23
+ tag: "chip[data-label][data-type][data-tag-id][data-path][data-meta]",
24
+ getAttrs(dom) {
25
+ return {
26
+ label: dom.getAttribute("data-label"),
27
+ type: dom.getAttribute("data-type"),
28
+ tagId: dom.getAttribute("data-tag-id") ?? "",
29
+ path: dom.getAttribute("data-path") ?? "",
30
+ meta: dom.getAttribute("data-meta") ? JSON.parse(dom.getAttribute("data-meta")) : {}
31
+ };
32
+ }
33
+ }
34
+ ],
35
+ toDOM(node) {
36
+ return [
37
+ "chip",
38
+ {
39
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- label exists as string on chip node
40
+ "data-label": node.attrs.label,
41
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- type exists as string on chip node
42
+ "data-type": node.attrs.type,
43
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- tagId exists as string on chip node
44
+ "data-tag-id": node.attrs.tagId,
45
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- path exists as string on chip node
46
+ "data-path": node.attrs.path,
47
+ "data-meta": node.attrs.meta ? JSON.stringify(node.attrs.meta) : {}
48
+ },
49
+ node.attrs.label
50
+ ];
51
+ }
52
+ },
53
+ text: {
54
+ group: "inline"
55
+ }
56
+ }
57
+ });
58
+
59
+ export { bylineEditorSchema };
@@ -0,0 +1,244 @@
1
+ 'use strict';
2
+
3
+ var react = require('@emotion/react');
4
+
5
+ const bylineContainerStyles = react.css`
6
+ position: relative;
7
+ width: 100%;
8
+ `;
9
+ const bylineEditorStyles = (theme) => react.css`
10
+ border: ${theme?.border ?? "1px black solid"};
11
+ background-color: ${theme?.background ?? "white"};
12
+ color: ${theme?.color ?? "inherit"};
13
+
14
+ /* ProseMirror styles from prosemirror-view/styles/prosemirror.css */
15
+ .ProseMirror {
16
+ padding: 4px 4px;
17
+ word-wrap: break-word;
18
+ white-space: pre-wrap;
19
+ white-space: break-spaces;
20
+ -webkit-font-variant-ligatures: none;
21
+ font-variant-ligatures: none;
22
+ font-feature-settings: 'liga' 0; /* the above doesn't seem to work in Edge */
23
+ line-height: ${theme?.lineHeight ?? "1.8"};
24
+ }
25
+
26
+ .ProseMirror[contenteditable='false'] {
27
+ background: ${theme?.readOnlyBackground ?? "#dcdcdc"};
28
+ cursor: 'not-allowed';
29
+ }
30
+
31
+ .ProseMirror pre {
32
+ white-space: pre-wrap;
33
+ }
34
+
35
+ .ProseMirror li {
36
+ position: relative;
37
+ }
38
+
39
+ .ProseMirror-hideselection *::selection {
40
+ background: transparent;
41
+ }
42
+ .ProseMirror-hideselection *::-moz-selection {
43
+ background: transparent;
44
+ }
45
+ .ProseMirror-hideselection {
46
+ caret-color: transparent;
47
+ }
48
+
49
+ /* See https://github.com/ProseMirror/prosemirror/issues/1421#issuecomment-1759320191 */
50
+ .ProseMirror [draggable][contenteditable='false'] {
51
+ user-select: text;
52
+ }
53
+
54
+ .ProseMirror-selectednode {
55
+ outline: ${theme?.chip?.selected?.border ?? "1px solid #b4d9ff"};
56
+ }
57
+
58
+ /* Make sure li selections wrap around markers */
59
+
60
+ li.ProseMirror-selectednode {
61
+ outline: none;
62
+ }
63
+
64
+ li.ProseMirror-selectednode:after {
65
+ content: '';
66
+ position: absolute;
67
+ left: -32px;
68
+ right: -2px;
69
+ top: -2px;
70
+ bottom: -2px;
71
+ border: ${theme?.chip?.selected?.border ?? "1px solid #b4d9ff"};
72
+ pointer-events: none;
73
+ }
74
+
75
+ /* Protect against generic img rules */
76
+
77
+ img.ProseMirror-separator {
78
+ display: inline !important;
79
+ border: none !important;
80
+ margin: 0 !important;
81
+ }
82
+
83
+ .ProseMirror:focus {
84
+ outline: none;
85
+ }
86
+
87
+ /* Invisible styles from @guardian/prosemirror-invisibles/dist/style.css */
88
+
89
+ .invisible {
90
+ /* Chrome in particular dislikes doing the right thing
91
+ * with carets and inline elements when contenteditable
92
+ * is 'false'. See e.g. https://github.com/ProseMirror/prosemirror/issues/1061
93
+ */
94
+ display: inline;
95
+ position: relative;
96
+ pointer-events: none;
97
+ }
98
+
99
+ .invisible:before {
100
+ position: relative;
101
+ caret-color: inherit;
102
+ color: ${theme?.invisibles?.color ?? "gray"};
103
+ display: inline-block;
104
+ font-weight: 400;
105
+ font-style: normal;
106
+ line-height: initial;
107
+ width: 0;
108
+ top: 0;
109
+ left: 0;
110
+ z-index: 1;
111
+ }
112
+
113
+ .invisible__selected-marker {
114
+ position: absolute;
115
+ caret-color: inherit;
116
+ background-color: #dcdcdc;
117
+ display: inline-block;
118
+ font-weight: 400;
119
+ font-style: normal;
120
+ line-height: initial;
121
+ top: 0;
122
+ left: 0;
123
+ width: 10px;
124
+ height: 100%;
125
+ z-index: 0;
126
+ }
127
+
128
+ .ProseMirror-focused .invisible__selected-marker {
129
+ background-color: #b4d9ff;
130
+ }
131
+
132
+ .ProseMirror-focused .invisible--is-selected::before {
133
+ background-color: #b4d8ff;
134
+ }
135
+
136
+ .invisible--is-selected::before {
137
+ background-color: #dcdcdc;
138
+ }
139
+
140
+ .invisible--space:before {
141
+ content: '·';
142
+ }
143
+
144
+ .invisible--nb-space {
145
+ vertical-align: text-bottom;
146
+ }
147
+
148
+ .invisible--nb-space:before {
149
+ font-size: 15px;
150
+ content: '^';
151
+ position: absolute;
152
+ top: 9px;
153
+ left: -1px;
154
+ }
155
+
156
+ chip {
157
+ border: ${theme?.chip?.border ?? "2px solid lightgrey"};
158
+ border-radius: ${theme?.chip?.borderRadius ?? "8px"};
159
+ padding: ${theme?.chip?.padding ?? "2px 4px"};
160
+ color: ${theme?.chip?.color ?? "inherit"};
161
+
162
+ cursor: default;
163
+
164
+ &[data-type='tagged'] {
165
+ background-color: ${theme?.chip?.taggedBackground ?? "lightgrey"};
166
+ }
167
+
168
+ &[data-type='untagged'] {
169
+ color: ${theme?.chip?.untagged?.color ?? "inherit"};
170
+ }
171
+
172
+ ::after {
173
+ content: '';
174
+ display: inline-block;
175
+ }
176
+
177
+ span {
178
+ cursor: pointer;
179
+ margin-left: 5px;
180
+ }
181
+ }
182
+
183
+ /* Leave space between subsequent chips */
184
+ chip + chip {
185
+ margin-left: 3px;
186
+ }
187
+
188
+ .placeholder {
189
+ display: inline-block;
190
+ height: 0;
191
+ width: 0;
192
+ white-space: nowrap;
193
+ color: ${theme?.placeholder?.color ?? "#777575"};
194
+ pointer-events: none;
195
+ cursor: text;
196
+ }
197
+ `;
198
+ const dropdownContainerStyles = (showDropdown, theme) => react.css`
199
+ position: absolute;
200
+ box-sizing: border-box;
201
+ width: 100%;
202
+ z-index: 1000;
203
+ border: ${theme?.border ?? "1px solid #ccc"};
204
+ background-color: ${theme?.background ?? "rgba(255, 255, 255, 0.8)"};
205
+ display: ${showDropdown ? "block" : "none"};
206
+ max-height: ${theme?.maxHeight ?? "320px"};
207
+ overflow-y: auto;
208
+ `;
209
+ const dropdownUlStyles = react.css`
210
+ margin: 0;
211
+ padding: 0;
212
+ list-style-type: none;
213
+ `;
214
+ const dropdownLiStyles = (theme) => react.css`
215
+ cursor: pointer;
216
+ padding: 5px;
217
+ border-bottom: ${theme?.dropdown?.li?.borderBottom ?? "1px solid #ccc"};
218
+ color: ${theme?.dropdown?.li?.color ?? "inherit"};
219
+ `;
220
+ const selectedDropdownLiStyles = (theme) => react.css`
221
+ background-color: ${theme?.dropdown?.li?.selected?.background ?? "cadetblue"};
222
+ color: ${theme?.dropdown?.li?.selected?.color ?? "white"};
223
+ `;
224
+ const previewStyles = react.css`
225
+ margin-top: 5px;
226
+ white-space: pre-wrap;
227
+ `;
228
+ const previewFreeTextStyles = react.css`
229
+ font-style: italic;
230
+ `;
231
+ const previewContributorStyles = (node) => react.css`
232
+ ${node.attrs.type === "tagged" ? "text-decoration: underline;" : ""}
233
+ color: inherit;
234
+ `;
235
+
236
+ exports.bylineContainerStyles = bylineContainerStyles;
237
+ exports.bylineEditorStyles = bylineEditorStyles;
238
+ exports.dropdownContainerStyles = dropdownContainerStyles;
239
+ exports.dropdownLiStyles = dropdownLiStyles;
240
+ exports.dropdownUlStyles = dropdownUlStyles;
241
+ exports.previewContributorStyles = previewContributorStyles;
242
+ exports.previewFreeTextStyles = previewFreeTextStyles;
243
+ exports.previewStyles = previewStyles;
244
+ exports.selectedDropdownLiStyles = selectedDropdownLiStyles;