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

added superscript and subscript buttons in playground #1945

Merged
merged 14 commits into from
May 2, 2022
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
10 changes: 4 additions & 6 deletions packages/lexical-playground/__tests__/e2e/Tables.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
pasteFromClipboard,
repeat,
selectCellsFromTableCords,
selectFromAdditionalStylesDropdown,
test,
} from '../utils/index.mjs';

Expand Down Expand Up @@ -766,12 +767,9 @@ test.describe('Tables', () => {
await fillTablePartiallyWithText(page);
await selectCellsFromTableCords(page, {x: 0, y: 0}, {x: 1, y: 1});

await clickSelectors(page, [
'.bold',
'.italic',
'.underline',
'.strikethrough',
]);
await clickSelectors(page, ['.bold', '.italic', '.underline']);

await selectFromAdditionalStylesDropdown(page, '.strikethrough');

// Check that the character styles are applied.
await assertHTML(
Expand Down
8 changes: 8 additions & 0 deletions packages/lexical-playground/__tests__/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,14 @@ export function html(partials, ...params) {
return output;
}

export async function selectFromAdditionalStylesDropdown(page, selector) {
await click(
page,
'.toolbar-item[aria-label="Formatting options for additional text styles"]',
);
await click(page, '.dropdown ' + selector);
}

export async function selectFromFormatDropdown(page, selector) {
await click(
page,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions packages/lexical-playground/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ i.strikethrough {
background-image: url(images/icons/type-strikethrough.svg);
}

i.subscript {
background-image: url(images/icons/type-subscript.svg);
}

i.superscript {
background-image: url(images/icons/type-superscript.svg);
}

i.link {
background-image: url(images/icons/link.svg);
}
Expand All @@ -339,6 +347,10 @@ i.horizontal-rule {
background-image: url(images/icons/plus.svg);
}

.icon.dropdown-more {
background-image: url(images/icons/dropdown-more.svg);
}

i.image {
background-image: url(images/icons/file-image.svg);
}
Expand Down Expand Up @@ -1539,3 +1551,11 @@ button.action-button:disabled {
.spacer {
letter-spacing: -2px;
}

button.item.dropdown-item-active {
background-color: rgba(223, 232, 250, 0.3);
}

button.item.dropdown-item-active i {
opacity: 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ function FloatingCharacterStylesEditor({
isUnderline,
isCode,
isStrikethrough,
isSubscript,
isSuperscript,
}: {
editor: LexicalEditor,
isBold: boolean,
isCode: boolean,
isItalic: boolean,
isLink: boolean,
isStrikethrough: boolean,
isSubscript: boolean,
isSuperscript: boolean,
isUnderline: boolean,
}): React$Node {
const popupCharStylesEditorRef = useRef<HTMLElement | null>(null);
Expand Down Expand Up @@ -184,6 +188,24 @@ function FloatingCharacterStylesEditor({
aria-label="Format text with a strikethrough">
<i className="format strikethrough" />
</button>
<button
onClick={() => {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'subscript');
}}
className={'popup-item spaced ' + (isSubscript ? 'active' : '')}
title="Subscript"
aria-label="Format Subscript">
<i className="format subscript" />
</button>
<button
onClick={() => {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'superscript');
}}
className={'popup-item spaced ' + (isSuperscript ? 'active' : '')}
title="Superscript"
aria-label="Format Superscript">
<i className="format superscript" />
</button>
<button
onClick={() => {
editor.dispatchCommand(FORMAT_TEXT_COMMAND, 'code');
Expand Down Expand Up @@ -225,6 +247,8 @@ function useCharacterStylesPopup(editor: LexicalEditor): React$Node {
const [isItalic, setIsItalic] = useState(false);
const [isUnderline, setIsUnderline] = useState(false);
const [isStrikethrough, setIsStrikethrough] = useState(false);
const [isSubscript, setIsSubscript] = useState(false);
const [isSuperscript, setIsSuperscript] = useState(false);
const [isCode, setIsCode] = useState(false);

const updatePopup = useCallback(() => {
Expand All @@ -249,6 +273,8 @@ function useCharacterStylesPopup(editor: LexicalEditor): React$Node {
setIsItalic(selection.hasFormat('italic'));
setIsUnderline(selection.hasFormat('underline'));
setIsStrikethrough(selection.hasFormat('strikethrough'));
setIsSubscript(selection.hasFormat('subscript'));
setIsSuperscript(selection.hasFormat('superscript'));
setIsCode(selection.hasFormat('code'));

// Update links
Expand Down Expand Up @@ -294,6 +320,8 @@ function useCharacterStylesPopup(editor: LexicalEditor): React$Node {
isBold={isBold}
isItalic={isItalic}
isStrikethrough={isStrikethrough}
isSubscript={isSubscript}
isSuperscript={isSuperscript}
isUnderline={isUnderline}
isCode={isCode}
/>,
Expand Down
66 changes: 52 additions & 14 deletions packages/lexical-playground/src/plugins/ToolbarPlugin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ export default function ToolbarPlugin(): React$Node {
const [isItalic, setIsItalic] = useState(false);
const [isUnderline, setIsUnderline] = useState(false);
const [isStrikethrough, setIsStrikethrough] = useState(false);
const [isSubscript, setIsSubscript] = useState(false);
const [isSuperscript, setIsSuperscript] = useState(false);
const [isCode, setIsCode] = useState(false);
const [canUndo, setCanUndo] = useState(false);
const [canRedo, setCanRedo] = useState(false);
Expand All @@ -782,6 +784,8 @@ export default function ToolbarPlugin(): React$Node {
setIsItalic(selection.hasFormat('italic'));
setIsUnderline(selection.hasFormat('underline'));
setIsStrikethrough(selection.hasFormat('strikethrough'));
setIsSubscript(selection.hasFormat('subscript'));
setIsSuperscript(selection.hasFormat('superscript'));
setIsCode(selection.hasFormat('code'));
setIsRTL($isParentElementRTL(selection));

Expand Down Expand Up @@ -1025,20 +1029,6 @@ export default function ToolbarPlugin(): React$Node {
}`}>
<i className="format underline" />
</button>
<button
onClick={() => {
activeEditor.dispatchCommand(
FORMAT_TEXT_COMMAND,
'strikethrough',
);
}}
className={
'toolbar-item spaced ' + (isStrikethrough ? 'active' : '')
}
title="Strikethrough"
aria-label="Format text with a strikethrough">
<i className="format strikethrough" />
</button>
<button
onClick={() => {
activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'code');
Expand All @@ -1060,6 +1050,54 @@ export default function ToolbarPlugin(): React$Node {
<FloatingLinkEditor editor={activeEditor} />,
document.body,
)}
<DropDown
buttonClassName="toolbar-item spaced"
buttonLabel=""
buttonAriaLabel="Formatting options for additional text styles"
buttonIconClassName="icon dropdown-more">
<button
onClick={() => {
activeEditor.dispatchCommand(
FORMAT_TEXT_COMMAND,
'strikethrough',
);
}}
className={
'item ' + (isStrikethrough ? 'active dropdown-item-active' : '')
}
title="Strikethrough"
aria-label="Format text with a strikethrough">
<i className="icon strikethrough" />
<span className="text">Strikethrough</span>
</button>
<button
onClick={() => {
activeEditor.dispatchCommand(FORMAT_TEXT_COMMAND, 'subscript');
}}
className={
'item ' + (isSubscript ? 'active dropdown-item-active' : '')
}
title="Subscript"
aria-label="Format text with a subscript">
<i className="icon subscript" />
<span className="text">Subscript</span>
</button>
<button
onClick={() => {
activeEditor.dispatchCommand(
FORMAT_TEXT_COMMAND,
'superscript',
);
}}
className={
'item ' + (isSuperscript ? 'active dropdown-item-active' : '')
}
title="Superscript"
aria-label="Format text with a superscript">
<i className="icon superscript" />
<span className="text">Superscript</span>
</button>
</DropDown>
<Divider />
<DropDown
buttonClassName="toolbar-item spaced"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
.PlaygroundEditorTheme__textUnderlineStrikethrough {
text-decoration: underline line-through;
}
.PlaygroundEditorTheme__textSubscript {
font-size: 0.8em;
vertical-align: sub !important;
Copy link
Contributor

Choose a reason for hiding this comment

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

why was this necessary? I'm mostly just curious.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just in case both sub and sup buttons are clicked, this will make sure that sub gets the priority. That's how it's handled here

}
.PlaygroundEditorTheme__textSuperscript {
font-size: 0.8em;
vertical-align: super;
}
.PlaygroundEditorTheme__textCode {
background-color: rgb(240, 242, 245);
padding: 1px 0.25rem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ const theme: EditorThemeClasses = {
code: 'PlaygroundEditorTheme__textCode',
italic: 'PlaygroundEditorTheme__textItalic',
strikethrough: 'PlaygroundEditorTheme__textStrikethrough',
subscript: 'PlaygroundEditorTheme__textSubscript',
superscript: 'PlaygroundEditorTheme__textSuperscript',
underline: 'PlaygroundEditorTheme__textUnderline',
underlineStrikethrough: 'PlaygroundEditorTheme__textUnderlineStrikethrough',
},
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/Lexical.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ type TextNodeThemeClasses = {
underlineStrikethrough?: EditorThemeClassName;
italic?: EditorThemeClassName;
code?: EditorThemeClassName;
subscript?: EditorThemeClassName;
superscript?: EditorThemeClassName;
};
export type EditorThemeClasses = {
ltr?: EditorThemeClassName;
Expand Down
2 changes: 2 additions & 0 deletions packages/lexical/flow/Lexical.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ type TextNodeThemeClasses = {
underlineStrikethrough?: EditorThemeClassName,
italic?: EditorThemeClassName,
code?: EditorThemeClassName,
subscript?: EditorThemeClassName,
superscript?: EditorThemeClassName,
};
export type EditorThemeClasses = {
ltr?: EditorThemeClassName,
Expand Down