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

Adds template variables to RichTextEditor #1105

Merged
merged 9 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@tiptap/extension-text": "^2.0.3",
"@tiptap/pm": "^2.0.3",
"@tiptap/react": "^2.0.3",
"@tiptap/suggestion": "^2.1.13",
"@types/testing-library__jest-dom": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^2",
"@typescript-eslint/parser": "^2",
Expand All @@ -29,6 +30,7 @@
"react-toggle": "4.1.1",
"react-transition-group": "^4.3.0",
"sanitize-html": "^2.11.0",
"tippy.js": "^6.3.7",
"uuid": "^7.0.2"
},
"scripts": {
Expand Down Expand Up @@ -190,4 +192,4 @@
"readme": "https://github.com/user-interviews/ui-design-system#readme",
"homepage": "https://github.com/user-interviews/ui-design-system",
"_id": "@user-interviews/ui-design-system@1.32.0"
}
}
kyleshike marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 20 additions & 4 deletions src/RichTextEditor/RichTextEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Text from '@tiptap/extension-text';
import sanitizeHtml from 'sanitize-html';

import { LoadingSkeleton } from 'src/LoadingSkeleton';
import { TemplateVariable, buildSuggestions } from './TemplateVariable';

import RichTextEditorMenuBar from './RichTextEditorMenuBar';

Expand Down Expand Up @@ -55,6 +56,7 @@ const RichTextEditor = ({
isOneLine,
onChange,
placeholder,
templateVariables,
}) => {
const oneLineExtension = isOneLine ? [OneLineLimit] : [];

Expand All @@ -73,6 +75,14 @@ const RichTextEditor = ({
}),
];

const templateVariablesExtension = templateVariables.length > 0 ?
[TemplateVariable.configure({
HTMLAttributes: {
class: 'RichTextEditor__TemplateVariable',
},
suggestion: buildSuggestions(templateVariables),
})] : [];
Comment on lines +78 to +84
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When templateVariables are provided toe the editor, we automatically enable the extension


const optionalExtensions = [
{
name: RichTextEditorActions.BOLD,
Expand Down Expand Up @@ -100,6 +110,7 @@ const RichTextEditor = ({
const extensions = [
...oneLineExtension,
...requiredExtensions,
...templateVariablesExtension,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this tip tap stuff is cool!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, right?

...optionalExtensions,
];

Expand Down Expand Up @@ -135,15 +146,18 @@ const RichTextEditor = ({
className="RichTextEditor"
id={id}
>
<RichTextEditorMenuBar
availableActions={availableActions}
editor={editor}
/>
{availableActions.length > 0 && (
<RichTextEditorMenuBar
availableActions={availableActions}
editor={editor}
/>
)}
<EditorContent
className={classNames(
className,
'RichTextEditor__field',
{ 'RichTextEditor__field--error': hasErrors },
{ 'RichTextEditor__field--without-menu-bar': availableActions.length === 0 },
Comment on lines +149 to +160
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When rendered with explicitly no available actions, hide the menu bar and apply a border radius on all corners of the editor:

Screenshot 2023-12-28 at 1 27 20 PM

)}
editor={editor}
role="textbox"
Expand Down Expand Up @@ -200,6 +214,7 @@ RichTextEditor.propTypes = {
initialValue: propTypes.string,
isOneLine: propTypes.bool,
placeholder: propTypes.string,
templateVariables: propTypes.arrayOf(propTypes.string),
/**
Callback function to call with sanitized HTML when editor state changes
*/
Expand All @@ -217,6 +232,7 @@ RichTextEditor.defaultProps = {
initialValue: undefined,
isOneLine: false,
placeholder: undefined,
templateVariables: [],
};

export default RichTextEditor;
4 changes: 4 additions & 0 deletions src/RichTextEditor/RichTextEditor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@
pointer-events: none;
}
}

.RichTextEditor__field--without-menu-bar .ProseMirror {
border-radius: $ux-border-radius $ux-border-radius;
kyleshike marked this conversation as resolved.
Show resolved Hide resolved
}
9 changes: 9 additions & 0 deletions src/RichTextEditor/RichTextEditor.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export const OneLine = () => (
/>
);

export const TemplateVariables = () => (
<RichTextEditor
id="text-editor"
placeholder="Enter / to view available variables "
kyleshike marked this conversation as resolved.
Show resolved Hide resolved
templateVariables={['howdy', 'ho']}
onChange={() => null}
/>
);

export const Error = () => (
<RichTextEditor
hasErrors
Expand Down
27 changes: 27 additions & 0 deletions src/RichTextEditor/TemplateVariable.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@import '../../scss/theme';

.RichTextEditor__TemplateVariables__Items {
border: 1px solid $synth-div-stroke-neutral;
background-color: $ux-white;
border-radius: $ux-border-radius;
padding: 0.5rem 0;
}

.RichTextEditor__TemplateVariables__Item {
@include synth-font-type-30;
background: #fff;
text-align: left;
width: 100%;
border: 0;
outline: 0;
color: $ux-gray-900;
padding: 4px 16px;
}

.RichTextEditor__TemplateVariables__Item:active,
.RichTextEditor__TemplateVariables__Item:hover,
.RichTextEditor__TemplateVariables__Item:focus,
.RichTextEditor__TemplateVariables__Item--selected {
background-color: $synth-hover-state;
text-decoration: none;
}
Loading
Loading