Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEV-3270] Refactoring - Use @prezly/sdk types #23

Merged
merged 11 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions packages/slate-editor/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/slate-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@prezly/events": "*",
"@prezly/linear-partition": "*",
"@prezly/progress-promise": "*",
"@prezly/sdk": "*",
"@prezly/slate-commons": "*",
"@prezly/slate-hyperscript": "*",
"@prezly/slate-lists": "*",
Expand Down Expand Up @@ -55,6 +56,7 @@
"@prezly/events": "^1.0.2",
"@prezly/linear-partition": "^1.0.0",
"@prezly/progress-promise": "^1.0.0",
"@prezly/sdk": "^2.9.2",
"@prezly/slate-commons": "^0.1.4",
"@prezly/slate-hyperscript": "^0.1.4",
"@prezly/slate-lists": "^0.1.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Coverage } from '@prezly/sdk';
import classNames from 'classnames';
import moment from 'moment';
import React, { FunctionComponent } from 'react';

import { MultilineEllipsis } from '../../../../../components';
import { Coverage } from '../../../../../types';

import {
getCoverageDescription,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Coverage } from '@prezly/sdk';
import classNames from 'classnames';
import React, { FunctionComponent, useEffect } from 'react';
import { useAsyncFn } from 'react-use';
Expand All @@ -6,7 +7,6 @@ import { RenderElementProps, useSelected } from 'slate-react';
import { LoadingPlaceholderV2 } from '../../../../components';
import { Coverage as CoverageIcon } from '../../../../icons';
import { ApiError } from '../../../../modules/api';
import { Coverage } from '../../../../types';

import { CoverageElementType } from '../../types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Coverage } from '@prezly/sdk';
import React, { FunctionComponent, ReactNode, RefObject, useRef } from 'react';
import { useSlate } from 'slate-react';

import { FloatingContainer } from '../../../../modules/editor-v4-components';
import { EventsEditor } from '../../../../modules/editor-v4-events';
import { Coverage } from '../../../../types';

import { SearchProps } from '../../types';

Expand Down
3 changes: 1 addition & 2 deletions packages/slate-editor/src/modules/editor-v4-coverage/jsx.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable @typescript-eslint/no-namespace */

import { Coverage } from '@prezly/sdk';
import { PARAGRAPH_TYPE } from '@prezly/slate-commons';
import { createHyperscript } from '@prezly/slate-hyperscript';
import { ReactNode } from 'react';

import { Coverage } from '../../types';

import { COVERAGE_TYPE } from './constants';

declare global {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Coverage } from '@prezly/sdk';
import { v4 as uuidV4 } from 'uuid';

import { Coverage } from '../../../types';

import { COVERAGE_TYPE } from '../constants';
import { CoverageElementType } from '../types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Coverage } from '@prezly/sdk';

import { formatBytes } from '../../../lib';
import { Coverage } from '../../../types';

const getCoverageDescription = (coverage: Coverage): string => {
if (coverage.attachment_oembed && coverage.attachment_oembed.description) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Coverage } from '@prezly/sdk';
import { UploadcareImage } from '@prezly/uploadcare';

import { Coverage } from '../../../types';

const getCoverageImageUrl = (coverage: Coverage, imageHeight: number): string | null => {
if (coverage.attachment_oembed && coverage.attachment_oembed.thumbnail_url) {
return coverage.attachment_oembed.thumbnail_url;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Coverage } from '../../../types';
import { Coverage } from '@prezly/sdk';

const getCoverageTitle = (coverage: Coverage): string => {
if (coverage.attachment_oembed && coverage.attachment_oembed.title) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Coverage } from '../../../types';
import { Coverage } from '@prezly/sdk';

const hasOnlyFileAttachment = (coverage: Coverage): boolean => {
if (coverage.attachment_oembed && coverage.attachment_oembed.description) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Coverage } from '@prezly/sdk';
import { EditorCommands } from '@prezly/slate-commons';
import { Editor } from 'slate';

import { Coverage } from '../../../types';

import createCoverage from './createCoverage';

const insertCoverage = (editor: Editor, coverageId: Coverage['id']): void => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Coverage } from '@prezly/sdk';
import { useSavedSelection } from '@prezly/slate-commons';
import { useState } from 'react';
import { ReactEditor } from 'slate-react';

import { EventsEditor } from '../../../modules/editor-v4-events';
import { Coverage } from '../../../types';

import insertCoverage from './insertCoverage';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Coverage } from '@prezly/sdk';
import { ReactNode, RefObject } from 'react';
import { Element } from 'slate';

import { Coverage } from '../../types';

import { COVERAGE_TYPE } from './constants';

export type CoverageType = typeof COVERAGE_TYPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const EmbedElement: FunctionComponent<Props> = ({
useEffect(() => {
if (!isUsingScreenshots && contentRef.current) {
injectOembedMarkup({
oembed: element.oembed,
html: element.oembed.html,
onError: () => setIsInvalid(true),
target: contentRef.current,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { OEmbedInfo } from '@prezly/sdk';
import { v4 as uuidV4 } from 'uuid';

import { OEmbedInfo } from '../../../types';

import { EMBED_TYPE } from '../constants';
import { EmbedElementType } from '../types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { OEmbedInfo } from '../../../types';

interface Parameters {
oembed: OEmbedInfo;
html: string | undefined;
onError: () => void;
target: HTMLElement;
}
Expand All @@ -14,9 +12,9 @@ function copyScriptAttributes(source: HTMLScriptElement, target: HTMLScriptEleme
target.innerText = source.innerText;
}

export default function injectOembedMarkup({ oembed, onError, target }: Parameters): void {
export default function injectOembedMarkup({ html, onError, target }: Parameters): void {
const container = document.createElement('div');
container.innerHTML = oembed.html || '';
container.innerHTML = html || '';
const embedScripts = Array.from(container.getElementsByTagName('script'));

embedScripts.forEach((embedScript) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { OEmbedInfo, OEmbedInfoType } from '../../../types';
import { OEmbedInfo } from '@prezly/sdk';

const isOEmbedInfo = (oembed: any): oembed is OEmbedInfo =>
typeof oembed === 'object' &&
oembed !== null &&
Object.values(OEmbedInfoType).includes(oembed.type) &&
typeof oembed.url === 'string' &&
oembed.url.length > 0 &&
typeof oembed.version === 'string' &&
oembed.version.length > 0;
const isOEmbedInfo = (value: any): value is OEmbedInfo =>
typeof value === 'object' &&
value !== null &&
['video', 'photo', 'rich', 'link'].includes(value.type) &&
typeof value.url === 'string' &&
value.url.length > 0 &&
typeof value.version === 'string' &&
value.version.length > 0;

export default isOEmbedInfo;
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { EditorCommands } from '@prezly/slate-commons';
import { Editor, NodeEntry } from 'slate';

import { OEmbedInfoType } from '../../../types';

import createEmbed from './createEmbed';
import isEmbedElement from './isEmbedElement';

const ALLOWED_ATTRIBUTES = Object.keys(
createEmbed({ type: OEmbedInfoType.LINK, url: '', version: '1.0' }, ''),
);
const ALLOWED_ATTRIBUTES = Object.keys(createEmbed({ type: 'link', url: '', version: '1.0' }, ''));

const normalizeRedundantEmbedAttributes = (editor: Editor, [node, path]: NodeEntry): boolean => {
if (!isEmbedElement(node)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OEmbedInfo } from '@prezly/sdk';
import { EditorCommands, useSavedSelection } from '@prezly/slate-commons';
import { useState } from 'react';
import { ReactEditor } from 'slate-react';
Expand All @@ -11,7 +12,6 @@ import {
removeLoader,
replaceLoader,
} from '../../../modules/editor-v4-loader';
import { OEmbedInfo } from '../../../types';

import createEmbed from './createEmbed';

Expand Down
3 changes: 1 addition & 2 deletions packages/slate-editor/src/modules/editor-v4-embed/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { OEmbedInfo } from '@prezly/sdk';
import { RefObject } from 'react';
import { Element } from 'slate';

import { OEmbedInfo } from '../../types';

import { EMBED_TYPE } from './constants';

export type EmbedType = typeof EMBED_TYPE;
Expand Down
4 changes: 2 additions & 2 deletions packages/slate-editor/src/modules/editor-v4-events/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Listener } from '@prezly/events';
import { Coverage } from '@prezly/sdk';
import { PressContact } from '@prezly/slate-types';
import { ReactNode } from 'react';

import { Coverage, PressContact } from '../../types';

export type EditorEventMap = {
'attachment-add-clicked': never;
'attachment-added': {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NewsroomRef } from '@prezly/sdk';
import { UploadcareImageStoragePayload } from '@prezly/uploadcare';
import { RefObject } from 'react';
import { Editor, Element } from 'slate';

import { NewsroomRef } from '../../types';

import { GALLERY_TYPE } from './constants';

export type GalleryType = typeof GALLERY_TYPE;
Expand Down
3 changes: 1 addition & 2 deletions packages/slate-editor/src/modules/editor-v4-image/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NewsroomRef } from '@prezly/sdk';
import { UploadcareImageStoragePayload } from '@prezly/uploadcare';
import { RefObject } from 'react';
import { Editor, Element, Text } from 'slate';

import { NewsroomRef } from '../../types';

import { IMAGE_CANDIDATE_TYPE, IMAGE_TYPE } from './constants';

export type ImageType = typeof IMAGE_TYPE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { NewsroomRef } from '@prezly/sdk';
import { UPLOADCARE_FILE_DATA_KEY } from '@prezly/uploadcare';
import { Editor } from 'slate';
import { FilePromise } from 'uploadcare-widget';

import { NewsroomRef } from '../../types';

interface File {
[UPLOADCARE_FILE_DATA_KEY]?: {
caption: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/slate-editor/src/modules/editor-v4/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Coverage, OEmbedInfo } from '@prezly/sdk';
import { UploadcareImageStoragePayload, UploadcareStoragePayload } from '@prezly/uploadcare';

import { Coverage, OEmbedInfo, PressContact } from '../../types';
import { PressContact } from '../../types';

/**
* Document nodes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Coverage } from '@prezly/sdk';
import { createEmptyValue } from '@prezly/slate-commons';

import { createCoverage } from '../../../modules/editor-v4-coverage';
import { Coverage } from '../../../types';

import serialize from './serialize';

Expand Down
2 changes: 1 addition & 1 deletion packages/slate-editor/src/modules/tests/data/coverage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Coverage } from '../../../types';
import { Coverage } from '@prezly/sdk';

import oembedInfo from './oembedInfo';

Expand Down
Loading