-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support non-breaking spaces in RTE (#1310)
Adds a new `"non-breaking-space"` "supported thing" to the RTE options.
- Loading branch information
1 parent
2b41033
commit dbdc0f5
Showing
12 changed files
with
167 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
"@comet/admin-icons": minor | ||
"@comet/admin-rte": minor | ||
"@comet/cms-admin": minor | ||
--- | ||
|
||
Add support for non-breaking spaces to RTE | ||
|
||
Add `"non-breaking-space"` to `supports` when creating an RTE: | ||
|
||
```tsx | ||
const [useRteApi] = makeRteApi(); | ||
|
||
export default function MyRte() { | ||
const { editorState, setEditorState } = useRteApi(); | ||
return ( | ||
<Rte | ||
value={editorState} | ||
onChange={setEditorState} | ||
options={{ | ||
supports: [ | ||
// Non-breaking space | ||
"non-breaking-space", | ||
// Other options you may wish to support | ||
"bold", | ||
"italic", | ||
], | ||
}} | ||
/> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
packages/admin/admin-rte/src/core/Controls/SpecialCharactersControls.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ButtonGroup } from "@mui/material"; | ||
import * as React from "react"; | ||
|
||
import NonBreakingSpaceToolbarButton from "../extension/NonBreakingSpace/ToolbarButton"; | ||
import { IControlProps } from "../types"; | ||
|
||
function SpecialCharactersControls(props: IControlProps) { | ||
const { | ||
options: { supports: supportedThings }, | ||
} = props; | ||
|
||
if (!supportedThings.includes("non-breaking-space")) { | ||
return null; | ||
} | ||
|
||
return <ButtonGroup>{supportedThings.includes("non-breaking-space") && <NonBreakingSpaceToolbarButton {...props} />}</ButtonGroup>; | ||
} | ||
|
||
export default SpecialCharactersControls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/admin/admin-rte/src/core/extension/NonBreakingSpace/Decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { DraftDecorator } from "draft-js"; | ||
|
||
import EditorComponent from "./EditorComponent"; | ||
|
||
const NO_BREAK_SPACE_UNICODE_CHAR = /\u00a0/g; | ||
|
||
const Decorator: DraftDecorator = { | ||
strategy: (contentBlock, callback) => { | ||
findWithRegex(NO_BREAK_SPACE_UNICODE_CHAR, contentBlock, callback); | ||
}, | ||
component: EditorComponent, | ||
}; | ||
|
||
function findWithRegex(regex: RegExp, contentBlock: Draft.ContentBlock, callback: (start: number, end: number) => void) { | ||
const text = contentBlock.getText(); | ||
let matchArr, start; | ||
while ((matchArr = regex.exec(text)) !== null) { | ||
start = matchArr.index; | ||
callback(start, start + matchArr[0].length); | ||
} | ||
} | ||
|
||
export default Decorator; |
37 changes: 37 additions & 0 deletions
37
packages/admin/admin-rte/src/core/extension/NonBreakingSpace/EditorComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { RteNonBreakingSpace } from "@comet/admin-icons"; | ||
import { styled } from "@mui/material/styles"; | ||
import { ContentState } from "draft-js"; | ||
import * as React from "react"; | ||
|
||
interface Props { | ||
contentState: ContentState; | ||
entityKey: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
function EditorComponent({ children }: Props): React.ReactElement { | ||
return ( | ||
<Root> | ||
<Icon /> | ||
{children} | ||
</Root> | ||
); | ||
} | ||
|
||
const Root = styled("span")` | ||
position: relative; | ||
// Arbitrary value to make the non-breaking space the same width as the icon | ||
letter-spacing: 0.9em; | ||
`; | ||
|
||
const Icon = styled(RteNonBreakingSpace)` | ||
position: absolute; | ||
// Arbitrary values to make the icon look centered | ||
top: 0.12em; | ||
left: 0.08em; | ||
font-size: inherit; | ||
color: currentcolor; | ||
opacity: 0.5; | ||
`; | ||
|
||
export default EditorComponent; |
43 changes: 43 additions & 0 deletions
43
packages/admin/admin-rte/src/core/extension/NonBreakingSpace/ToolbarButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { RteNonBreakingSpace } from "@comet/admin-icons"; | ||
import Tooltip from "@mui/material/Tooltip"; | ||
import { EditorState, Modifier } from "draft-js"; | ||
import * as React from "react"; | ||
import { FormattedMessage } from "react-intl"; | ||
|
||
import ControlButton from "../../Controls/ControlButton"; | ||
import { IControlProps } from "../../types"; | ||
|
||
const NO_BREAK_SPACE_UNICODE_CHAR = 0x00a0; | ||
|
||
function ToolbarButton({ editorState, setEditorState }: IControlProps): React.ReactElement { | ||
function handleClick(event: React.MouseEvent) { | ||
event.preventDefault(); // Preserve focus in editor | ||
|
||
const currentContent = editorState.getCurrentContent(); | ||
const selection = editorState.getSelection(); | ||
// TODO insert \u00a0 in a way that link-entities dont break when inserted in the middle of a link text | ||
// right now the link is split into 2 link-entities | ||
// works as expected when \u00ad is copied and pasted: https://unicode.flopp.net/c/00A0 | ||
let textWithEntity; | ||
|
||
if (selection.isCollapsed()) { | ||
textWithEntity = Modifier.insertText(currentContent, selection, String.fromCharCode(NO_BREAK_SPACE_UNICODE_CHAR)); | ||
} else { | ||
textWithEntity = Modifier.replaceText(currentContent, selection, String.fromCharCode(NO_BREAK_SPACE_UNICODE_CHAR)); | ||
} | ||
setEditorState(EditorState.push(editorState, textWithEntity, "insert-characters")); | ||
} | ||
|
||
return ( | ||
<Tooltip | ||
title={<FormattedMessage id="comet.rte.extensions.nonBreakingSpace.buttonTooltip" defaultMessage="Insert a non-breaking space" />} | ||
placement="top" | ||
> | ||
<span> | ||
<ControlButton icon={RteNonBreakingSpace} onButtonClick={handleClick} /> | ||
</span> | ||
</Tooltip> | ||
); | ||
} | ||
|
||
export default ToolbarButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters