Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
3609f3a
Add config + change files to .ts
miina Sep 2, 2022
be63c46
Commit missing change
miina Sep 2, 2022
e2eede9
Merge main
miina Sep 5, 2022
6c8e09b
util.ts
miina Sep 5, 2022
3e72636
Formatters
miina Sep 5, 2022
da2cf1d
More types
miina Sep 5, 2022
a2b9d5b
More types.
miina Sep 6, 2022
080543e
Merge remote-tracking branch 'origin/main' into add/12179-typescript-…
miina Sep 6, 2022
9592bcc
Type state.
miina Sep 6, 2022
9deb517
Fix text.
miina Sep 7, 2022
05d48b2
Ignore ts
miina Sep 7, 2022
b3fab60
Update provider.ts
miina Sep 7, 2022
e96fb55
More types.
miina Sep 7, 2022
9bd12f2
Update context.ts
miina Sep 7, 2022
ce03a9b
More types
miina Sep 8, 2022
520dd08
Merge remote-tracking branch 'origin/main' into add/12179-typescript-…
miina Sep 8, 2022
fbe6e77
More type error fixes.
miina Sep 8, 2022
7026b32
Use DraftInlineStyle
miina Sep 8, 2022
6c4042b
One more DraftInlineStyle usage
miina Sep 8, 2022
c02797a
Use CSSProperties
miina Sep 8, 2022
298cfc1
Fix import
miina Sep 8, 2022
2775269
Override convertFromHTML type
miina Sep 8, 2022
75bfe9d
Try fixing pasting types.
miina Sep 9, 2022
45ce84a
Use Immutable Map for renderMap
miina Sep 12, 2022
da481f0
Move types for props to the relevant component files.
miina Sep 12, 2022
4664bd5
Use !== null instead of Boolean()
miina Sep 12, 2022
e26c93d
Use CSSProperties
miina Sep 12, 2022
b1f8ee0
Adjust styleMap to be a bit cleaner.
miina Sep 12, 2022
56c273b
Add function overload for useRichText()
miina Sep 12, 2022
14d8ccc
Init empty state with values
miina Sep 12, 2022
da066f2
Add Formatter interface
miina Sep 13, 2022
8b8ce75
Import immutable
miina Sep 13, 2022
8cd600e
Merge remote-tracking branch 'origin/main' into add/12179-typescript-…
miina Sep 13, 2022
fcdcbaa
Fix setter types.
miina Sep 13, 2022
b4ac830
Fix tests.
miina Sep 13, 2022
54e12c6
Use draft-js-export-html fork with built files
swissspidy Sep 13, 2022
7a892df
Adjust function overload for useRichText
miina Sep 13, 2022
68dcde8
Update lock file
swissspidy Sep 13, 2022
69374e7
Merge remote-tracking branch 'origin/main' into add/12179-typescript-…
miina Sep 13, 2022
b4fc5e2
Remove ts-ignore
miina Sep 13, 2022
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
55 changes: 45 additions & 10 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/patterns/src/generatePatternStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function getStopList(stops: Array<ColorStop>, alpha: number) {
function generatePatternStyles(
pattern: Pattern | null = null,
property = 'background'
) {
): Record<string, string> {
if (pattern === null) {
return { [property]: 'transparent' };
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/useContextSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function useContextSelector<T, S>(

const equalityFnCallback = (state: T) => {
const selected = selector(state);
if (equalityFn(ref.current, selected)) {
if (ref.current && equalityFn(ref.current, selected)) {
return ref.current;
}
ref.current = selected;
Expand Down
9 changes: 6 additions & 3 deletions packages/rich-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
},
"customExports": {
".": {
"default": "./src/index.js"
"default": "./src/index.ts"
}
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"source": "src/index.js",
"types": "dist-types/index.d.ts",
"source": "src/index.ts",
"publishConfig": {
"access": "public"
},
Expand All @@ -43,9 +44,11 @@
"draft-js-export-html": "^1.4.1",
"draft-js-import-html": "^1.4.1",
"draftjs-filters": "^3.0.1",
"immutable": "^4.1.0",
"prop-types": "^15.7.2"
},
"devDependencies": {
"@testing-library/react": "^12.1.5"
"@testing-library/react": "^12.1.5",
"@types/draft-js": "^0.11.9"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,26 @@
* External dependencies
*/
import { createContext } from '@googleforcreators/react';
/**
* Internal dependencies
*/
import type { State } from './types';

const RichTextContext = createContext({ state: {}, actions: {} });
const RichTextContext = createContext<State>({
state: {
editorState: null,
hasCurrentEditor: false,
selectionInfo: undefined,
},
actions: {
setStateFromContent: () => undefined,
updateEditorState: () => undefined,
getHandleKeyCommand: () => () => 'not-handled',
handlePastedText: () => 'not-handled',
clearState: () => undefined,
selectionActions: {},
getContentFromState: () => null,
},
});

export default RichTextContext;
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
* External dependencies
*/
import { stateToHTML } from 'draft-js-export-html';
import type { EditorState } from 'draft-js';
import type { RenderConfig } from 'draft-js-export-html';

/**
* Internal dependencies
*/
import formatters from './formatters';

function inlineStyleFn(styles) {
function inlineStyleFn(
styles: Immutable.OrderedSet<string>
): RenderConfig | null {
const inlineCSS = formatters.reduce(
(css, { stylesToCSS }) => ({ ...css, ...stylesToCSS(styles) }),
{}
Expand All @@ -40,7 +44,7 @@ function inlineStyleFn(styles) {
};
}

function exportHTML(editorState) {
function exportHTML(editorState: EditorState) {
if (!editorState) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,25 @@
* External dependencies
*/
import { stateFromHTML } from 'draft-js-import-html';
import type { InlineCreators } from 'draft-js-import-html';

/**
* Internal dependencies
*/
import getValidHTML from './utils/getValidHTML';
import formatters from './formatters';

function customInlineFn(element, { Style }) {
function customInlineFn(element: Element, { Style }: InlineCreators) {
const styleStrings = formatters
.map(({ elementToStyle }) => elementToStyle(element))
.map(({ elementToStyle }) => elementToStyle(element as HTMLElement))
.filter((style) => Boolean(style));

if (styleStrings.length === 0) {
return null;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment -- The defined type is `string` but it actually accepts arrays of strings, too.
// @ts-ignore
return Style(styleStrings);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
* limitations under the License.
*/

/**
* External dependencies
*/
import type { CSSProperties } from 'react';
import type { DraftInlineStyle } from 'draft-js';

/**
* Internal dependencies
*/
import formatters from './formatters';
import { fauxStylesToCSS } from './fauxSelection';

function customInlineDisplay(styles) {
function customInlineDisplay(styles: DraftInlineStyle): CSSProperties {
const stylesToCSSConverters = [
...formatters.map(({ stylesToCSS }) => stylesToCSS),
fauxStylesToCSS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* limitations under the License.
*/

/**
* External dependencies
*/
import type { EditorState } from 'draft-js';

/* Ignore reason: This is lifted from elsewhere - a combo of these basically:
*
* https://github.com/webdeveloperpr/draft-js-custom-styles/blob/f3e6b533905de8eee6da54f9727b5e5803d53fc4/src/index.js#L8-L52
Expand All @@ -39,11 +44,11 @@
* output: [Set("BOLD"), Set("BOLD", "ITALIC"), Set(), Set("UNDERLINE")]
* </example>
*
* @param {Object} editorState The current state of the editor including
* @param editorState The current state of the editor including
* selection
* @return {Array.<Set.<string>>} list of sets of styles as described
* @return list of sets of styles as described
*/
export function getAllStyleSetsInSelection(editorState) {
export function getAllStyleSetsInSelection(editorState: EditorState) {
const styleSets = [];
const contentState = editorState.getCurrentContent();
const selection = editorState.getSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,31 @@
* External dependencies
*/
import { Editor, getDefaultKeyBinding, KeyBindingUtil } from 'draft-js';
import PropTypes from 'prop-types';
import {
useEffect,
useRef,
useImperativeHandle,
forwardRef,
useUnmount,
} from '@googleforcreators/react';
import type { ForwardedRef, KeyboardEvent } from 'react';

/**
* Internal dependencies
*/
import useRichText from './useRichText';
import customInlineDisplay from './customInlineDisplay';

function RichTextEditor({ content, onChange }, ref) {
const editorRef = useRef(null);
export interface RichTextEditorProps {
content: string;
onChange: (content: string | null) => void;
}

function RichTextEditor(
{ content, onChange }: RichTextEditorProps,
ref: ForwardedRef<Partial<HTMLElement>>
) {
const editorRef = useRef<Editor | null>(null);
const {
state: { editorState },
actions: {
Expand All @@ -61,7 +69,7 @@ function RichTextEditor({ content, onChange }, ref) {
onChange(newContent);
}, [onChange, getContentFromState, editorState]);

const hasEditorState = Boolean(editorState);
const hasEditorState = editorState !== null;

// On unmount, clear state in provider
useUnmount(clearState);
Expand All @@ -82,7 +90,7 @@ function RichTextEditor({ content, onChange }, ref) {

const { hasCommandModifier } = KeyBindingUtil;

function bindKeys(e) {
function bindKeys(e: KeyboardEvent) {
if (e.code === 'KeyA' && hasCommandModifier(e)) {
return 'selectall';
}
Expand All @@ -108,9 +116,4 @@ function RichTextEditor({ content, onChange }, ref) {

const RichTextEditorWithRef = forwardRef(RichTextEditor);

RichTextEditor.propTypes = {
content: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
};

export default RichTextEditorWithRef;
Loading