@atlaskit/editor-plugin-placeholder 6.5.4 → 6.6.1

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 (47) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/dist/cjs/placeholderPlugin.js +19 -568
  3. package/dist/cjs/pm-plugins/adf-builders.js +30 -0
  4. package/dist/cjs/pm-plugins/animation.js +58 -0
  5. package/dist/cjs/pm-plugins/constants.js +18 -0
  6. package/dist/cjs/pm-plugins/decorations.js +96 -0
  7. package/dist/cjs/pm-plugins/main.js +168 -0
  8. package/dist/cjs/pm-plugins/placeholderPluginLegacy.js +617 -0
  9. package/dist/cjs/pm-plugins/types.js +5 -0
  10. package/dist/cjs/pm-plugins/utils.js +243 -0
  11. package/dist/es2019/placeholderPlugin.js +8 -548
  12. package/dist/es2019/pm-plugins/adf-builders.js +22 -0
  13. package/dist/es2019/pm-plugins/animation.js +49 -0
  14. package/dist/es2019/pm-plugins/constants.js +12 -0
  15. package/dist/es2019/pm-plugins/decorations.js +87 -0
  16. package/dist/es2019/pm-plugins/main.js +162 -0
  17. package/dist/es2019/pm-plugins/placeholderPluginLegacy.js +598 -0
  18. package/dist/es2019/pm-plugins/types.js +1 -0
  19. package/dist/es2019/pm-plugins/utils.js +234 -0
  20. package/dist/esm/placeholderPlugin.js +17 -563
  21. package/dist/esm/pm-plugins/adf-builders.js +24 -0
  22. package/dist/esm/pm-plugins/animation.js +52 -0
  23. package/dist/esm/pm-plugins/constants.js +12 -0
  24. package/dist/esm/pm-plugins/decorations.js +90 -0
  25. package/dist/esm/pm-plugins/main.js +162 -0
  26. package/dist/esm/pm-plugins/placeholderPluginLegacy.js +607 -0
  27. package/dist/esm/pm-plugins/types.js +1 -0
  28. package/dist/esm/pm-plugins/utils.js +232 -0
  29. package/dist/types/placeholderPlugin.d.ts +0 -8
  30. package/dist/types/pm-plugins/adf-builders.d.ts +4 -0
  31. package/dist/types/pm-plugins/animation.d.ts +1 -0
  32. package/dist/types/pm-plugins/constants.d.ts +9 -0
  33. package/dist/types/pm-plugins/decorations.d.ts +4 -0
  34. package/dist/types/pm-plugins/main.d.ts +6 -0
  35. package/dist/types/pm-plugins/placeholderPluginLegacy.d.ts +13 -0
  36. package/dist/types/pm-plugins/types.d.ts +37 -0
  37. package/dist/types/pm-plugins/utils.d.ts +27 -0
  38. package/dist/types-ts4.5/placeholderPlugin.d.ts +0 -8
  39. package/dist/types-ts4.5/pm-plugins/adf-builders.d.ts +4 -0
  40. package/dist/types-ts4.5/pm-plugins/animation.d.ts +1 -0
  41. package/dist/types-ts4.5/pm-plugins/constants.d.ts +9 -0
  42. package/dist/types-ts4.5/pm-plugins/decorations.d.ts +4 -0
  43. package/dist/types-ts4.5/pm-plugins/main.d.ts +6 -0
  44. package/dist/types-ts4.5/pm-plugins/placeholderPluginLegacy.d.ts +13 -0
  45. package/dist/types-ts4.5/pm-plugins/types.d.ts +37 -0
  46. package/dist/types-ts4.5/pm-plugins/utils.d.ts +27 -0
  47. package/package.json +6 -9
@@ -0,0 +1,234 @@
1
+ import { placeholderTextMessages as messages } from '@atlaskit/editor-common/messages';
2
+ import { bracketTyped, hasDocAsParent, isEmptyDocument, isEmptyParagraph } from '@atlaskit/editor-common/utils';
3
+ import { findParentNode } from '@atlaskit/editor-prosemirror/utils';
4
+ import { fg } from '@atlaskit/platform-feature-flags';
5
+ import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
6
+ import { pluginKey } from '../placeholderPlugin';
7
+ import { createLongEmptyNodePlaceholderADF, createShortEmptyNodePlaceholderADF } from './adf-builders';
8
+ import { nodeTypesWithLongPlaceholderText, nodeTypesWithShortPlaceholderText, nodeTypesWithSyncBlockPlaceholderText } from './constants';
9
+ export function getPlaceholderState(editorState) {
10
+ return pluginKey.getState(editorState);
11
+ }
12
+ export function setPlaceHolderState({
13
+ placeholderText,
14
+ pos,
15
+ placeholderPrompts,
16
+ typedAndDeleted,
17
+ userHadTyped,
18
+ canShowOnEmptyParagraph,
19
+ showOnEmptyParagraph,
20
+ contextPlaceholderADF
21
+ }) {
22
+ return {
23
+ hasPlaceholder: true,
24
+ placeholderText,
25
+ placeholderPrompts,
26
+ contextPlaceholderADF,
27
+ pos: pos ? pos : 1,
28
+ typedAndDeleted,
29
+ userHadTyped,
30
+ canShowOnEmptyParagraph,
31
+ showOnEmptyParagraph
32
+ };
33
+ }
34
+ export const emptyPlaceholder = ({
35
+ placeholderText,
36
+ placeholderPrompts,
37
+ userHadTyped,
38
+ pos,
39
+ canShowOnEmptyParagraph,
40
+ showOnEmptyParagraph
41
+ }) => ({
42
+ hasPlaceholder: false,
43
+ placeholderText,
44
+ placeholderPrompts,
45
+ userHadTyped,
46
+ typedAndDeleted: false,
47
+ canShowOnEmptyParagraph,
48
+ showOnEmptyParagraph,
49
+ pos
50
+ });
51
+ export function createPlaceHolderStateFrom({
52
+ isInitial,
53
+ isEditorFocused,
54
+ editorState,
55
+ isTypeAheadOpen,
56
+ defaultPlaceholderText,
57
+ intl,
58
+ bracketPlaceholderText,
59
+ emptyLinePlaceholder,
60
+ placeholderADF,
61
+ placeholderPrompts,
62
+ typedAndDeleted,
63
+ userHadTyped,
64
+ isPlaceholderHidden,
65
+ withEmptyParagraph,
66
+ showOnEmptyParagraph
67
+ }) {
68
+ if (isPlaceholderHidden && withEmptyParagraph) {
69
+ return {
70
+ ...emptyPlaceholder({
71
+ placeholderText: defaultPlaceholderText,
72
+ placeholderPrompts,
73
+ userHadTyped
74
+ }),
75
+ isPlaceholderHidden
76
+ };
77
+ }
78
+ if (isTypeAheadOpen !== null && isTypeAheadOpen !== void 0 && isTypeAheadOpen(editorState)) {
79
+ return emptyPlaceholder({
80
+ placeholderText: defaultPlaceholderText,
81
+ placeholderPrompts,
82
+ userHadTyped
83
+ });
84
+ }
85
+ if ((defaultPlaceholderText || placeholderPrompts || placeholderADF) && isEmptyDocument(editorState.doc)) {
86
+ return setPlaceHolderState({
87
+ placeholderText: defaultPlaceholderText,
88
+ pos: 1,
89
+ placeholderPrompts,
90
+ typedAndDeleted,
91
+ userHadTyped
92
+ });
93
+ }
94
+ if (withEmptyParagraph) {
95
+ const {
96
+ from,
97
+ to,
98
+ $to
99
+ } = editorState.selection;
100
+ if ((defaultPlaceholderText || placeholderADF) && withEmptyParagraph && isEditorFocused && !isInitial && !isEmptyDocument(editorState.doc) && from === to && isEmptyParagraph($to.parent) && hasDocAsParent($to)) {
101
+ return showOnEmptyParagraph ? setPlaceHolderState({
102
+ placeholderText: defaultPlaceholderText,
103
+ pos: to,
104
+ placeholderPrompts,
105
+ typedAndDeleted,
106
+ userHadTyped,
107
+ canShowOnEmptyParagraph: true,
108
+ showOnEmptyParagraph: true
109
+ }) : emptyPlaceholder({
110
+ placeholderText: defaultPlaceholderText,
111
+ placeholderPrompts,
112
+ userHadTyped,
113
+ canShowOnEmptyParagraph: true,
114
+ showOnEmptyParagraph: false,
115
+ pos: to
116
+ });
117
+ }
118
+ }
119
+ if (isEditorFocused && editorExperiment('platform_editor_controls', 'variant1')) {
120
+ var _parentNode$firstChil, _parentNode$firstChil2;
121
+ const {
122
+ $from,
123
+ $to
124
+ } = editorState.selection;
125
+ if ($from.pos !== $to.pos) {
126
+ return emptyPlaceholder({
127
+ placeholderText: defaultPlaceholderText,
128
+ placeholderPrompts,
129
+ userHadTyped
130
+ });
131
+ }
132
+ const parentNode = $from.node($from.depth - 1);
133
+ const parentType = parentNode === null || parentNode === void 0 ? void 0 : parentNode.type.name;
134
+ if (emptyLinePlaceholder && parentType === 'doc') {
135
+ const isEmptyLine = isEmptyParagraph($from.parent);
136
+ if (isEmptyLine) {
137
+ return setPlaceHolderState({
138
+ placeholderText: emptyLinePlaceholder,
139
+ pos: $from.pos,
140
+ placeholderPrompts,
141
+ typedAndDeleted,
142
+ userHadTyped
143
+ });
144
+ }
145
+ }
146
+ const isEmptyNode = (parentNode === null || parentNode === void 0 ? void 0 : parentNode.childCount) === 1 && ((_parentNode$firstChil = parentNode.firstChild) === null || _parentNode$firstChil === void 0 ? void 0 : _parentNode$firstChil.content.size) === 0 && ((_parentNode$firstChil2 = parentNode.firstChild) === null || _parentNode$firstChil2 === void 0 ? void 0 : _parentNode$firstChil2.type.name) === 'paragraph';
147
+ if (nodeTypesWithShortPlaceholderText.includes(parentType) && isEmptyNode) {
148
+ var _table$node$firstChil;
149
+ const table = findParentNode(node => node.type === editorState.schema.nodes.table)(editorState.selection);
150
+ if (!table) {
151
+ return emptyPlaceholder({
152
+ placeholderText: defaultPlaceholderText,
153
+ placeholderPrompts,
154
+ userHadTyped
155
+ });
156
+ }
157
+ const isFirstCell = (table === null || table === void 0 ? void 0 : (_table$node$firstChil = table.node.firstChild) === null || _table$node$firstChil === void 0 ? void 0 : _table$node$firstChil.content.firstChild) === parentNode;
158
+ if (isFirstCell) {
159
+ return setPlaceHolderState({
160
+ placeholderText: !fg('platform_editor_ai_aifc_patch_ga') ? intl.formatMessage(messages.shortEmptyNodePlaceholderText) : undefined,
161
+ contextPlaceholderADF: fg('platform_editor_ai_aifc_patch_ga') ? createShortEmptyNodePlaceholderADF(intl) : undefined,
162
+ pos: $from.pos,
163
+ placeholderPrompts,
164
+ typedAndDeleted,
165
+ userHadTyped
166
+ });
167
+ }
168
+ }
169
+ if (nodeTypesWithLongPlaceholderText.includes(parentType) && isEmptyNode) {
170
+ return setPlaceHolderState({
171
+ placeholderText: !fg('platform_editor_ai_aifc_patch_ga') ? intl.formatMessage(messages.longEmptyNodePlaceholderText) : undefined,
172
+ contextPlaceholderADF: fg('platform_editor_ai_aifc_patch_ga') ? createLongEmptyNodePlaceholderADF(intl) : undefined,
173
+ pos: $from.pos,
174
+ placeholderPrompts,
175
+ typedAndDeleted,
176
+ userHadTyped
177
+ });
178
+ }
179
+ if (nodeTypesWithSyncBlockPlaceholderText.includes(parentType) && isEmptyNode && editorExperiment('platform_synced_block', true)) {
180
+ return setPlaceHolderState({
181
+ placeholderText: intl.formatMessage(messages.syncBlockPlaceholderText),
182
+ pos: $from.pos,
183
+ placeholderPrompts,
184
+ typedAndDeleted,
185
+ userHadTyped
186
+ });
187
+ }
188
+ return emptyPlaceholder({
189
+ placeholderText: defaultPlaceholderText,
190
+ placeholderPrompts,
191
+ userHadTyped
192
+ });
193
+ }
194
+ if (bracketPlaceholderText && bracketTyped(editorState) && isEditorFocused) {
195
+ const {
196
+ $from
197
+ } = editorState.selection;
198
+ // Space is to account for positioning of the bracket
199
+ const bracketHint = ' ' + bracketPlaceholderText;
200
+ return setPlaceHolderState({
201
+ placeholderText: bracketHint,
202
+ pos: $from.pos - 1,
203
+ placeholderPrompts,
204
+ typedAndDeleted,
205
+ userHadTyped
206
+ });
207
+ }
208
+ return emptyPlaceholder({
209
+ placeholderText: defaultPlaceholderText,
210
+ placeholderPrompts,
211
+ userHadTyped
212
+ });
213
+ }
214
+ export function calculateUserInteractionState({
215
+ placeholderState,
216
+ oldEditorState,
217
+ newEditorState
218
+ }) {
219
+ const wasEmpty = oldEditorState ? isEmptyDocument(oldEditorState.doc) : true;
220
+ const isEmpty = isEmptyDocument(newEditorState.doc);
221
+ const hasEverTyped = Boolean(placeholderState === null || placeholderState === void 0 ? void 0 : placeholderState.userHadTyped) ||
222
+ // Previously typed
223
+ !wasEmpty ||
224
+ // Had content before
225
+ wasEmpty && !isEmpty; // Just added content
226
+ const justDeletedAll = hasEverTyped && isEmpty && !wasEmpty;
227
+ const isInTypedAndDeletedState = justDeletedAll || Boolean(placeholderState === null || placeholderState === void 0 ? void 0 : placeholderState.typedAndDeleted) && isEmpty;
228
+ // Only reset user interaction tracking when editor is cleanly empty
229
+ const shouldResetInteraction = isEmpty && !isInTypedAndDeletedState;
230
+ return {
231
+ userHadTyped: shouldResetInteraction ? false : hasEverTyped,
232
+ typedAndDeleted: isInTypedAndDeletedState
233
+ };
234
+ }