@os-design/editor 1.0.204 → 1.0.205

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 (33) hide show
  1. package/package.json +16 -8
  2. package/src/@types/emotion.d.ts +7 -0
  3. package/src/Editor/BlockToolbar.tsx +49 -0
  4. package/src/Editor/StyleToolbar.tsx +80 -0
  5. package/src/Editor/Toolbar.tsx +18 -0
  6. package/src/Editor/ToolbarButton.tsx +65 -0
  7. package/src/Editor/blocks/Figure.tsx +10 -0
  8. package/src/Editor/blocks/FigureCaption.tsx +19 -0
  9. package/src/Editor/blocks/imageBlock.tsx +162 -0
  10. package/src/Editor/blocks/types.ts +21 -0
  11. package/src/Editor/blocks/videoBlock.tsx +83 -0
  12. package/src/Editor/decorators/linkDecorator.tsx +23 -0
  13. package/src/Editor/hooks/useBlockToolbarProps.ts +72 -0
  14. package/src/Editor/hooks/usePastedTextHandler.ts +47 -0
  15. package/src/Editor/hooks/useReturnHandler.ts +37 -0
  16. package/src/Editor/hooks/useStyleToolbarProps.ts +53 -0
  17. package/src/Editor/index.tsx +203 -0
  18. package/src/Editor/styles/defaultDraftJsStyles.ts +179 -0
  19. package/src/Editor/styles/overrideDraftJsStyles.ts +20 -0
  20. package/src/Editor/utils/addNewBlockAt.ts +59 -0
  21. package/src/Editor/utils/changeBlock.ts +20 -0
  22. package/src/Editor/utils/createContentEditorState.ts +7 -0
  23. package/src/Editor/utils/createDecorator.ts +7 -0
  24. package/src/Editor/utils/createEmptyEditorState.ts +7 -0
  25. package/src/Editor/utils/defaultStyleToolbarItems.tsx +30 -0
  26. package/src/Editor/utils/getCurrentBlock.ts +9 -0
  27. package/src/Editor/utils/getSelectedBlockElement.ts +17 -0
  28. package/src/Editor/utils/getSelectionRange.ts +7 -0
  29. package/src/Editor/utils/setLink.ts +22 -0
  30. package/src/Editor/utils/transformers.ts +20 -0
  31. package/src/Editor/utils/unsetLink.ts +11 -0
  32. package/src/EditorSkeleton/index.tsx +21 -0
  33. package/src/index.ts +16 -0
@@ -0,0 +1,17 @@
1
+ import getSelectionRange from './getSelectionRange';
2
+
3
+ const getSelectedBlockElement = (): Element | null => {
4
+ const selectionRange = getSelectionRange();
5
+ if (!selectionRange) return null;
6
+
7
+ let node: Node | null = selectionRange.startContainer;
8
+
9
+ do {
10
+ if (node instanceof Element && node.getAttribute('data-block') === 'true')
11
+ return node;
12
+ node = node.parentNode;
13
+ } while (node != null);
14
+ return null;
15
+ };
16
+
17
+ export default getSelectedBlockElement;
@@ -0,0 +1,7 @@
1
+ const getSelectionRange = (): Range | null => {
2
+ const selection = document.getSelection();
3
+ if (!selection || selection.rangeCount === 0) return null;
4
+ return selection.getRangeAt(0);
5
+ };
6
+
7
+ export default getSelectionRange;
@@ -0,0 +1,22 @@
1
+ import { EditorState, RichUtils } from 'draft-js';
2
+
3
+ const setLink = (editorState: EditorState, url: string): EditorState => {
4
+ const containsLink = RichUtils.currentBlockContainsLink(editorState);
5
+ if (containsLink) return editorState;
6
+
7
+ const contentState = editorState.getCurrentContent();
8
+ const contentStateWithLink = contentState.createEntity('LINK', 'MUTABLE', {
9
+ url,
10
+ });
11
+ const entityKey = contentStateWithLink.getLastCreatedEntityKey();
12
+ const nextEditorState = EditorState.set(editorState, {
13
+ currentContent: contentStateWithLink,
14
+ });
15
+ return RichUtils.toggleLink(
16
+ nextEditorState,
17
+ nextEditorState.getSelection(),
18
+ entityKey
19
+ );
20
+ };
21
+
22
+ export default setLink;
@@ -0,0 +1,20 @@
1
+ import { EditorState } from 'draft-js';
2
+
3
+ export const editorStateToFirstParagraph = (
4
+ editorState: EditorState
5
+ ): string => {
6
+ const firstUnstyledBlock = editorState
7
+ .getCurrentContent()
8
+ .getBlocksAsArray()
9
+ .find((block) => block.getType() === 'unstyled');
10
+
11
+ return firstUnstyledBlock ? firstUnstyledBlock.getText() : '';
12
+ };
13
+
14
+ export const editorStateToMetaDescription = (
15
+ editorState: EditorState
16
+ ): string =>
17
+ editorStateToFirstParagraph(editorState)
18
+ .slice(0, 200)
19
+ .replace(/^(.*[.?!])(.*)/, '$1')
20
+ .trim();
@@ -0,0 +1,11 @@
1
+ import { EditorState, RichUtils } from 'draft-js';
2
+
3
+ const unsetLink = (editorState: EditorState): EditorState => {
4
+ const containsLink = RichUtils.currentBlockContainsLink(editorState);
5
+ if (!containsLink) return editorState;
6
+
7
+ const selectionState = editorState.getSelection();
8
+ return RichUtils.toggleLink(editorState, selectionState, null);
9
+ };
10
+
11
+ export default unsetLink;
@@ -0,0 +1,21 @@
1
+ import styled from '@emotion/styled';
2
+
3
+ import { InputSkeleton, InputSkeletonProps } from '@os-design/core';
4
+ import React, { forwardRef } from 'react';
5
+
6
+ export type EditorSkeletonProps = InputSkeletonProps;
7
+
8
+ const StyledEditorSkeleton = styled(InputSkeleton)`
9
+ height: ${(p) => p.theme.editorMinHeight}em;
10
+ `;
11
+
12
+ /**
13
+ * Provides an editor placeholder while a user waits for the content to load.
14
+ */
15
+ const EditorSkeleton = forwardRef<HTMLDivElement, EditorSkeletonProps>(
16
+ (props, ref) => <StyledEditorSkeleton {...props} ref={ref} />
17
+ );
18
+
19
+ EditorSkeleton.displayName = 'EditorSkeleton';
20
+
21
+ export default EditorSkeleton;
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ export { default as Editor } from './Editor';
2
+ export { default as addNewBlockAt } from './Editor/utils/addNewBlockAt';
3
+ export { default as changeBlock } from './Editor/utils/changeBlock';
4
+ export { default as createContentEditorState } from './Editor/utils/createContentEditorState';
5
+ export { default as createDecorator } from './Editor/utils/createDecorator';
6
+ export { default as createEmptyEditorState } from './Editor/utils/createEmptyEditorState';
7
+ export { default as defaultStyleToolbarItems } from './Editor/utils/defaultStyleToolbarItems';
8
+ export { default as getCurrentBlock } from './Editor/utils/getCurrentBlock';
9
+ export { default as imageBlock } from './Editor/blocks/imageBlock';
10
+ export { default as videoBlock } from './Editor/blocks/videoBlock';
11
+ export { default as EditorSkeleton } from './EditorSkeleton';
12
+
13
+ export * from './Editor';
14
+ export * from './Editor/utils/transformers';
15
+ export * from './Editor/blocks/types';
16
+ export * from './EditorSkeleton';