mirror of
https://github.com/drawdb-io/drawdb.git
synced 2026-02-11 02:00:37 +08:00
Add rich text editot
This commit is contained in:
@@ -1,19 +1,137 @@
|
||||
import React, { useState } from "react";
|
||||
import { createEditor } from "slate";
|
||||
import { Slate, Editable, withReact } from "slate-react";
|
||||
import { LexicalComposer } from "@lexical/react/LexicalComposer";
|
||||
import { RichTextPlugin } from "@lexical/react/LexicalRichTextPlugin";
|
||||
import { ContentEditable } from "@lexical/react/LexicalContentEditable";
|
||||
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin";
|
||||
import { AutoFocusPlugin } from "@lexical/react/LexicalAutoFocusPlugin";
|
||||
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary";
|
||||
import { HeadingNode, QuoteNode } from "@lexical/rich-text";
|
||||
import { TableCellNode, TableNode, TableRowNode } from "@lexical/table";
|
||||
import { ListItemNode, ListNode } from "@lexical/list";
|
||||
import { CodeHighlightNode, CodeNode } from "@lexical/code";
|
||||
import { AutoLinkNode, LinkNode } from "@lexical/link";
|
||||
import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin";
|
||||
import { ListPlugin } from "@lexical/react/LexicalListPlugin";
|
||||
import { MarkdownShortcutPlugin } from "@lexical/react/LexicalMarkdownShortcutPlugin";
|
||||
import { TRANSFORMERS } from "@lexical/markdown";
|
||||
|
||||
const initialValue = [
|
||||
{
|
||||
type: "paragraph",
|
||||
children: [{ text: "A line of text in a paragraph." }],
|
||||
import ToolbarPlugin from "../plugins/ToolbarPlugin";
|
||||
import ListMaxIndentLevelPlugin from "../plugins/ListMaxIndentLevelPlugin";
|
||||
import CodeHighlightPlugin from "../plugins/CodeHighlightPlugin";
|
||||
import AutoLinkPlugin from "../plugins/AutoLinkPlugin";
|
||||
|
||||
const exampleTheme = {
|
||||
ltr: "ltr",
|
||||
rtl: "rtl",
|
||||
placeholder: "editor-placeholder",
|
||||
paragraph: "editor-paragraph",
|
||||
quote: "editor-quote",
|
||||
heading: {
|
||||
h1: "editor-heading-h1",
|
||||
h2: "editor-heading-h2",
|
||||
h3: "editor-heading-h3",
|
||||
h4: "editor-heading-h4",
|
||||
h5: "editor-heading-h5",
|
||||
},
|
||||
];
|
||||
list: {
|
||||
nested: {
|
||||
listitem: "editor-nested-listitem",
|
||||
},
|
||||
ol: "editor-list-ol",
|
||||
ul: "editor-list-ul",
|
||||
listitem: "editor-listitem",
|
||||
},
|
||||
image: "editor-image",
|
||||
link: "editor-link",
|
||||
text: {
|
||||
bold: "editor-text-bold",
|
||||
italic: "editor-text-italic",
|
||||
overflowed: "editor-text-overflowed",
|
||||
hashtag: "editor-text-hashtag",
|
||||
underline: "editor-text-underline",
|
||||
strikethrough: "editor-text-strikethrough",
|
||||
underlineStrikethrough: "editor-text-underlineStrikethrough",
|
||||
code: "editor-text-code",
|
||||
},
|
||||
code: "editor-code",
|
||||
codeHighlight: {
|
||||
atrule: "editor-tokenAttr",
|
||||
attr: "editor-tokenAttr",
|
||||
boolean: "editor-tokenProperty",
|
||||
builtin: "editor-tokenSelector",
|
||||
cdata: "editor-tokenComment",
|
||||
char: "editor-tokenSelector",
|
||||
class: "editor-tokenFunction",
|
||||
"class-name": "editor-tokenFunction",
|
||||
comment: "editor-tokenComment",
|
||||
constant: "editor-tokenProperty",
|
||||
deleted: "editor-tokenProperty",
|
||||
doctype: "editor-tokenComment",
|
||||
entity: "editor-tokenOperator",
|
||||
function: "editor-tokenFunction",
|
||||
important: "editor-tokenVariable",
|
||||
inserted: "editor-tokenSelector",
|
||||
keyword: "editor-tokenAttr",
|
||||
namespace: "editor-tokenVariable",
|
||||
number: "editor-tokenProperty",
|
||||
operator: "editor-tokenOperator",
|
||||
prolog: "editor-tokenComment",
|
||||
property: "editor-tokenProperty",
|
||||
punctuation: "editor-tokenPunctuation",
|
||||
regex: "editor-tokenVariable",
|
||||
selector: "editor-tokenSelector",
|
||||
string: "editor-tokenSelector",
|
||||
symbol: "editor-tokenProperty",
|
||||
tag: "editor-tokenProperty",
|
||||
url: "editor-tokenOperator",
|
||||
variable: "editor-tokenVariable",
|
||||
},
|
||||
};
|
||||
|
||||
export default function RichEditor() {
|
||||
const [editor] = useState(() => withReact(createEditor()));
|
||||
function Placeholder() {
|
||||
return <div className="editor-placeholder">Describe the bug</div>;
|
||||
}
|
||||
|
||||
const editorConfig = {
|
||||
theme: exampleTheme,
|
||||
onError(error) {
|
||||
throw error;
|
||||
},
|
||||
nodes: [
|
||||
HeadingNode,
|
||||
ListNode,
|
||||
ListItemNode,
|
||||
QuoteNode,
|
||||
CodeNode,
|
||||
CodeHighlightNode,
|
||||
TableNode,
|
||||
TableCellNode,
|
||||
TableRowNode,
|
||||
AutoLinkNode,
|
||||
LinkNode,
|
||||
],
|
||||
};
|
||||
|
||||
export default function RichEditor(props) {
|
||||
return (
|
||||
<Slate editor={editor} initialValue={initialValue}>
|
||||
<Editable />
|
||||
</Slate>
|
||||
<LexicalComposer initialConfig={editorConfig}>
|
||||
<div className="editor-container">
|
||||
<ToolbarPlugin theme={props.theme} />
|
||||
<div className="editor-inner">
|
||||
<RichTextPlugin
|
||||
contentEditable={<ContentEditable className="editor-input" />}
|
||||
placeholder={<Placeholder />}
|
||||
ErrorBoundary={LexicalErrorBoundary}
|
||||
/>
|
||||
<HistoryPlugin />
|
||||
<AutoFocusPlugin />
|
||||
<CodeHighlightPlugin />
|
||||
<ListPlugin />
|
||||
<LinkPlugin />
|
||||
<AutoLinkPlugin />
|
||||
<ListMaxIndentLevelPlugin maxDepth={7} />
|
||||
<MarkdownShortcutPlugin transformers={TRANSFORMERS} />
|
||||
</div>
|
||||
</div>
|
||||
</LexicalComposer>
|
||||
);
|
||||
}
|
||||
|
||||
746
src/index.css
746
src/index.css
@@ -53,7 +53,7 @@
|
||||
background-color: var(--semi-color-bg-3);
|
||||
}
|
||||
|
||||
.card-theme{
|
||||
.card-theme {
|
||||
color: var(--semi-color-text-1);
|
||||
background-color: rgba(var(--semi-grey-0), 1);
|
||||
}
|
||||
@@ -85,3 +85,747 @@
|
||||
.table-border {
|
||||
border-color: rgba(var(--semi-grey-2), 1);
|
||||
}
|
||||
|
||||
.ltr {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.rtl {
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.editor-container {
|
||||
margin: 20px auto 20px auto;
|
||||
border-radius: 6px;
|
||||
color: var(--semi-color-text-1);
|
||||
background-color: rgba(var(--semi-grey-1), 1);
|
||||
position: relative;
|
||||
line-height: 20px;
|
||||
font-weight: 400;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.editor-inner {
|
||||
background-color: rgba(var(--semi-grey-1), 1);
|
||||
position: relative;
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.editor-input {
|
||||
min-height: 150px;
|
||||
resize: none;
|
||||
font-size: 15px;
|
||||
position: relative;
|
||||
tab-size: 1;
|
||||
outline: 0;
|
||||
padding: 15px 10px;
|
||||
}
|
||||
|
||||
.editor-placeholder {
|
||||
color: #999;
|
||||
position: absolute;
|
||||
text-overflow: ellipsis;
|
||||
top: 15px;
|
||||
left: 10px;
|
||||
font-size: 15px;
|
||||
user-select: none;
|
||||
display: inline-block;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.editor-text-bold {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.editor-text-italic {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.editor-text-underline {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.editor-text-strikethrough {
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
||||
.editor-text-underlineStrikethrough {
|
||||
text-decoration: underline line-through;
|
||||
}
|
||||
|
||||
.editor-text-code {
|
||||
background-color: rgba(var(--semi-grey-2), 1);
|
||||
padding: 1px 0.25rem;
|
||||
font-family: Menlo, Consolas, Monaco, monospace;
|
||||
font-size: 94%;
|
||||
}
|
||||
|
||||
.editor-link {
|
||||
color: rgb(33, 111, 219);
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.editor-code {
|
||||
background-color: rgba(var(--semi-grey-0), 1);
|
||||
font-family: Menlo, Consolas, Monaco, monospace;
|
||||
display: block;
|
||||
padding: 8px 8px 8px 52px;
|
||||
line-height: 1.53;
|
||||
font-size: 13px;
|
||||
margin: 0;
|
||||
margin-top: 8px;
|
||||
margin-bottom: 8px;
|
||||
tab-size: 2;
|
||||
overflow-x: auto;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.editor-code:before {
|
||||
content: attr(data-gutter);
|
||||
position: absolute;
|
||||
background-color: rgba(var(--semi-grey-0), 1);
|
||||
left: 0;
|
||||
top: 0;
|
||||
border-right: 1px solid rgba(var(--semi-grey-3), 1);
|
||||
padding: 8px;
|
||||
color: #777;
|
||||
white-space: pre-wrap;
|
||||
text-align: right;
|
||||
min-width: 25px;
|
||||
}
|
||||
|
||||
.editor-code:after {
|
||||
content: attr(data-highlight-language);
|
||||
top: 0;
|
||||
right: 3px;
|
||||
padding: 3px;
|
||||
font-size: 10px;
|
||||
text-transform: uppercase;
|
||||
position: absolute;
|
||||
color: rgba(var(--semi-text-1), 1);
|
||||
}
|
||||
|
||||
.editor-tokenComment {
|
||||
color: slategray;
|
||||
}
|
||||
|
||||
.editor-tokenPunctuation {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.editor-tokenProperty {
|
||||
color: #905;
|
||||
}
|
||||
|
||||
.editor-tokenSelector {
|
||||
color: #690;
|
||||
}
|
||||
|
||||
.editor-tokenOperator {
|
||||
color: #9a6e3a;
|
||||
}
|
||||
|
||||
.editor-tokenAttr {
|
||||
color: #07a;
|
||||
}
|
||||
|
||||
.editor-tokenVariable {
|
||||
color: #e90;
|
||||
}
|
||||
|
||||
.editor-tokenFunction {
|
||||
color: #dd4a68;
|
||||
}
|
||||
|
||||
.editor-paragraph {
|
||||
margin: 0;
|
||||
margin-bottom: 8px;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.editor-paragraph:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.editor-heading-h1 {
|
||||
font-size: 24px;
|
||||
margin: 0;
|
||||
margin-bottom: 12px;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.editor-heading-h2 {
|
||||
font-size: 16px;
|
||||
margin: 0;
|
||||
margin-top: 10px;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.editor-quote {
|
||||
margin: 0;
|
||||
margin-left: 20px;
|
||||
font-size: 15px;
|
||||
color: rgb(101, 103, 107);
|
||||
border-left-color: rgb(206, 208, 212);
|
||||
border-left-width: 4px;
|
||||
border-left-style: solid;
|
||||
padding-left: 16px;
|
||||
}
|
||||
|
||||
.editor-list-ol {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
margin-left: 16px;
|
||||
list-style-type: decimal;
|
||||
}
|
||||
|
||||
.editor-list-ul {
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
margin-left: 16px;
|
||||
}
|
||||
|
||||
.editor-listitem {
|
||||
margin: 8px 32px 8px 32px;
|
||||
}
|
||||
|
||||
.editor-nested-listitem {
|
||||
list-style-type: none;
|
||||
}
|
||||
|
||||
pre::-webkit-scrollbar {
|
||||
background: transparent;
|
||||
width: 10px;
|
||||
}
|
||||
|
||||
pre::-webkit-scrollbar-thumb {
|
||||
background: #999;
|
||||
}
|
||||
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 1px;
|
||||
background-color: rgba(var(--semi-grey-1), 1);
|
||||
padding: 4px;
|
||||
border-top-left-radius: 10px;
|
||||
border-top-right-radius: 10px;
|
||||
border-bottom: rgba(var(--semi-grey-2), 1) solid 2px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item {
|
||||
border: 0;
|
||||
display: flex;
|
||||
border-radius: 6px;
|
||||
padding: 8px;
|
||||
cursor: pointer;
|
||||
vertical-align: middle;
|
||||
fill: currentColor;
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item:disabled {
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item.spaced {
|
||||
margin-right: 2px;
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item i.format {
|
||||
background-size: contain;
|
||||
display: inline-block;
|
||||
height: 18px;
|
||||
width: 18px;
|
||||
margin-top: 2px;
|
||||
vertical-align: -0.25em;
|
||||
display: flex;
|
||||
opacity: 1;
|
||||
fill: currentColor;
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item:disabled i.format {
|
||||
opacity: 0.4;
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item.active {
|
||||
background-color: rgba(var(--semi-grey-2), 1);
|
||||
}
|
||||
|
||||
.toolbar button.toolbar-item.active i {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.toolbar .toolbar-item:hover:not([disabled]) {
|
||||
background-color: rgba(var(--semi-grey-2), 1);
|
||||
}
|
||||
|
||||
.toolbar .divider {
|
||||
width: 2px;
|
||||
background-color: rgba(var(--semi-grey-2), 1);
|
||||
margin: 4px 4px;
|
||||
}
|
||||
|
||||
.toolbar select.toolbar-item {
|
||||
border-radius: 6px;
|
||||
padding: 8px;
|
||||
vertical-align: middle;
|
||||
appearance: none;
|
||||
-webkit-appearance: none;
|
||||
-moz-appearance: none;
|
||||
font-size: 14px;
|
||||
background-color: rgba(var(--semi-grey-1), 1);
|
||||
text-overflow: ellipsis;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.toolbar select.code-language {
|
||||
text-transform: capitalize;
|
||||
width: 130px;
|
||||
}
|
||||
|
||||
.toolbar .toolbar-item .text {
|
||||
line-height: 20px;
|
||||
width: 200px;
|
||||
vertical-align: middle;
|
||||
font-size: 14px;
|
||||
color: rgba(var(--semi-text-1), 1);
|
||||
text-overflow: ellipsis;
|
||||
width: 70px;
|
||||
overflow: hidden;
|
||||
height: 20px;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.toolbar .toolbar-item .icon {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
user-select: none;
|
||||
margin-right: 8px;
|
||||
line-height: 16px;
|
||||
background-size: contain;
|
||||
}
|
||||
|
||||
.toolbar i.chevron-down {
|
||||
margin-top: 3px;
|
||||
margin-left: 6px;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
user-select: none;
|
||||
background-image: url(/public/images/icons/chevron-down.svg);
|
||||
}
|
||||
|
||||
.toolbar i.chevron-down-dark {
|
||||
margin-top: 3px;
|
||||
margin-left: 6px;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
user-select: none;
|
||||
background-image: url(/public/images/icons/chevron-down-dark.svg);
|
||||
}
|
||||
|
||||
.toolbar i.chevron-down.inside {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin-left: -20px;
|
||||
margin-top: 11px;
|
||||
margin-right: 10px;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.toolbar i.chevron-down-dark.inside {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
margin-left: -20px;
|
||||
margin-top: 11px;
|
||||
margin-right: 10px;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
#block-controls button:focus-visible {
|
||||
border-color: blue;
|
||||
}
|
||||
|
||||
#block-controls span.block-type {
|
||||
background-size: contain;
|
||||
display: block;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
margin: 2px;
|
||||
}
|
||||
|
||||
#block-controls span.block-type.paragraph {
|
||||
background-image: url(/public/images/icons/text-paragraph.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.paragraph-dark {
|
||||
background-image: url(/public/images/icons/text-paragraph-dark.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.h1 {
|
||||
background-image: url(/public/images/icons/type-h1.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.h1-dark {
|
||||
background-image: url(/public/images/icons/type-h1-dark.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.h2 {
|
||||
background-image: url(/public/images/icons/type-h2.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.h2-dark {
|
||||
background-image: url(/public/images/icons/type-h2-dark.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.quote {
|
||||
background-image: url(/public/images/icons/chat-square-quote.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.quote-dark {
|
||||
background-image: url(/public/images/icons/chat-square-quote-dark.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.ul {
|
||||
background-image: url(/public/images/icons/list-ul.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.ul-dark {
|
||||
background-image: url(/public/images/icons/list-ul-dark.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.ol {
|
||||
background-image: url(/public/images/icons/list-ol.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.ol-dark {
|
||||
background-image: url(/public/images/icons/list-ol-dark.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.code {
|
||||
background-image: url(/public/images/icons/code.svg);
|
||||
}
|
||||
|
||||
#block-controls span.block-type.code-dark {
|
||||
background-image: url(/public/images/icons/code-dark.svg);
|
||||
}
|
||||
|
||||
.dropdown {
|
||||
z-index: 5;
|
||||
display: block;
|
||||
position: absolute;
|
||||
box-shadow: 0 12px 28px 0 rgba(0, 0, 0, 0.2), 0 2px 4px 0 rgba(0, 0, 0, 0.1),
|
||||
inset 0 0 0 1px rgba(var(--semi-grey-2), 1);
|
||||
border-radius: 6px;
|
||||
min-width: 240px;
|
||||
min-height: 40px;
|
||||
background-color: rgba(var(--semi-grey-1), 1);
|
||||
}
|
||||
|
||||
.dropdown .item {
|
||||
padding: 6px 16px;
|
||||
width: 100%;
|
||||
color: rgba(var(--semi-text-1), 1);
|
||||
cursor: pointer;
|
||||
line-height: 16px;
|
||||
font-size: 14px;
|
||||
display: flex;
|
||||
align-content: center;
|
||||
flex-direction: row;
|
||||
flex-shrink: 0;
|
||||
justify-content: space-between;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.dropdown .item .active {
|
||||
display: flex;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
}
|
||||
|
||||
.dropdown .item:first-child {
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.dropdown .item:last-child {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.dropdown .item:hover {
|
||||
background-color: rgba(var(--semi-grey-3), 1);
|
||||
}
|
||||
|
||||
.dropdown .item .text {
|
||||
display: flex;
|
||||
line-height: 20px;
|
||||
flex-grow: 1;
|
||||
}
|
||||
|
||||
.dropdown .item .icon {
|
||||
display: flex;
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
user-select: none;
|
||||
margin-right: 12px;
|
||||
line-height: 16px;
|
||||
background-size: contain;
|
||||
}
|
||||
|
||||
.link-editor {
|
||||
position: absolute;
|
||||
z-index: 100;
|
||||
top: -10000px;
|
||||
left: -10000px;
|
||||
margin-top: -6px;
|
||||
max-width: 300px;
|
||||
width: 100%;
|
||||
opacity: 0;
|
||||
background-color: rgba(var(--semi-grey-1), 1);
|
||||
box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.3);
|
||||
border-radius: 6px;
|
||||
transition: opacity 0.5s;
|
||||
}
|
||||
|
||||
.link-editor .link-input {
|
||||
display: block;
|
||||
width: calc(100% - 24px);
|
||||
box-sizing: border-box;
|
||||
margin: 8px 12px;
|
||||
padding: 8px 12px;
|
||||
border-radius: 6px;
|
||||
background-color: rgba(var(--semi-grey-2), 1);
|
||||
font-size: 15px;
|
||||
color: var(--semi-color-text-1);
|
||||
border: 0;
|
||||
outline: 0;
|
||||
position: relative;
|
||||
font-family: inherit;
|
||||
}
|
||||
|
||||
.link-edit {
|
||||
background-image: url(/public/images/icons/pencil-fill.svg);
|
||||
background-size: 16px;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
width: 35px;
|
||||
vertical-align: -0.25em;
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.link-edit-dark {
|
||||
background-image: url(/public/images/icons/pencil-fill-dark.svg);
|
||||
background-size: 16px;
|
||||
background-position: center;
|
||||
background-repeat: no-repeat;
|
||||
width: 35px;
|
||||
vertical-align: -0.25em;
|
||||
position: absolute;
|
||||
right: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.link-editor .link-input a {
|
||||
color: rgb(33, 111, 219);
|
||||
text-decoration: none;
|
||||
display: block;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
margin-right: 30px;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.link-editor .link-input a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.link-editor .button {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
display: inline-block;
|
||||
padding: 6px;
|
||||
border-radius: 8px;
|
||||
cursor: pointer;
|
||||
margin: 0 2px;
|
||||
}
|
||||
|
||||
.link-editor .button.hovered {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
display: inline-block;
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
.link-editor .button i,
|
||||
.actions i {
|
||||
background-size: contain;
|
||||
display: inline-block;
|
||||
height: 20px;
|
||||
width: 20px;
|
||||
vertical-align: -0.25em;
|
||||
}
|
||||
|
||||
i.undo {
|
||||
background-image: url(/public/images/icons/arrow-counterclockwise.svg);
|
||||
}
|
||||
|
||||
i.undo-dark {
|
||||
background-image: url(/public/images/icons/arrow-counterclockwise-dark.svg);
|
||||
}
|
||||
|
||||
i.redo {
|
||||
background-image: url(/public/images/icons/arrow-clockwise.svg);
|
||||
}
|
||||
|
||||
i.redo-dark {
|
||||
background-image: url(/public/images/icons/arrow-clockwise-dark.svg);
|
||||
}
|
||||
|
||||
.icon.paragraph {
|
||||
background-image: url(/public/images/icons/text-paragraph.svg);
|
||||
}
|
||||
|
||||
.icon.paragraph-dark {
|
||||
background-image: url(/public/images/icons/text-paragraph-dark.svg);
|
||||
}
|
||||
|
||||
.icon.large-heading,
|
||||
.icon.h1 {
|
||||
background-image: url(/public/images/icons/type-h1.svg);
|
||||
}
|
||||
|
||||
.icon.large-heading-dark,
|
||||
.icon.h1-dark {
|
||||
background-image: url(/public/images/icons/type-h1-dark.svg);
|
||||
}
|
||||
|
||||
.icon.small-heading,
|
||||
.icon.h2 {
|
||||
background-image: url(/public/images/icons/type-h2.svg);
|
||||
}
|
||||
|
||||
.icon.small-heading-dark,
|
||||
.icon.h2-dark {
|
||||
background-image: url(/public/images/icons/type-h2-dark.svg);
|
||||
}
|
||||
|
||||
.icon.bullet-list,
|
||||
.icon.ul {
|
||||
background-image: url(/public/images/icons/list-ul.svg);
|
||||
}
|
||||
|
||||
.icon.bullet-list-dark,
|
||||
.icon.ul-dark {
|
||||
background-image: url(/public/images/icons/list-ul-dark.svg);
|
||||
}
|
||||
|
||||
.icon.numbered-list,
|
||||
.icon.ol {
|
||||
background-image: url(/public/images/icons/list-ol.svg);
|
||||
}
|
||||
|
||||
.icon.numbered-list-dark,
|
||||
.icon.ol-dark {
|
||||
background-image: url(/public/images/icons/list-ol-dark.svg);
|
||||
}
|
||||
|
||||
.icon.quote {
|
||||
background-image: url(/public/images/icons/chat-square-quote.svg);
|
||||
}
|
||||
|
||||
.icon.quote-dark {
|
||||
background-image: url(/public/images/icons/chat-square-quote-dark.svg);
|
||||
}
|
||||
|
||||
.icon.code {
|
||||
background-image: url(/public/images/icons/code.svg);
|
||||
}
|
||||
|
||||
.icon.code-dark {
|
||||
background-image: url(/public/images/icons/code-dark.svg);
|
||||
}
|
||||
|
||||
i.bold {
|
||||
background-image: url(/public/images/icons/type-bold.svg);
|
||||
}
|
||||
|
||||
i.bold-dark {
|
||||
background-image: url(/public/images/icons/type-bold-dark.svg);
|
||||
}
|
||||
|
||||
i.italic {
|
||||
background-image: url(/public/images/icons/type-italic.svg);
|
||||
}
|
||||
|
||||
i.italic-dark {
|
||||
background-image: url(/public/images/icons/type-italic-dark.svg);
|
||||
}
|
||||
|
||||
i.underline {
|
||||
background-image: url(/public/images/icons/type-underline.svg);
|
||||
}
|
||||
|
||||
i.underline-dark {
|
||||
background-image: url(/public/images/icons/type-underline-dark.svg);
|
||||
}
|
||||
|
||||
i.strikethrough {
|
||||
background-image: url(/public/images/icons/type-strikethrough.svg);
|
||||
}
|
||||
|
||||
i.strikethrough-dark {
|
||||
background-image: url(/public/images/icons/type-strikethrough-dark.svg);
|
||||
}
|
||||
|
||||
i.code {
|
||||
background-image: url(/public/images/icons/code.svg);
|
||||
}
|
||||
|
||||
i.code-dark {
|
||||
background-image: url(/public/images/icons/code-dark.svg);
|
||||
}
|
||||
|
||||
i.link {
|
||||
background-image: url(/public/images/icons/link.svg);
|
||||
}
|
||||
|
||||
i.link-dark {
|
||||
background-image: url(/public/images/icons/link-dark.svg);
|
||||
}
|
||||
|
||||
i.left-align {
|
||||
background-image: url(/public/images/icons/text-left.svg);
|
||||
}
|
||||
|
||||
i.left-align-dark {
|
||||
background-image: url(/public/images/icons/text-left-dark.svg);
|
||||
}
|
||||
|
||||
i.center-align {
|
||||
background-image: url(/public/images/icons/text-center.svg);
|
||||
}
|
||||
|
||||
i.center-align-dark {
|
||||
background-image: url(/public/images/icons/text-center-dark.svg);
|
||||
}
|
||||
|
||||
i.right-align {
|
||||
background-image: url(/public/images/icons/text-right.svg);
|
||||
}
|
||||
|
||||
i.right-align-dark {
|
||||
background-image: url(/public/images/icons/text-right-dark.svg);
|
||||
}
|
||||
|
||||
i.justify-align {
|
||||
background-image: url(/public/images/icons/justify.svg);
|
||||
}
|
||||
|
||||
i.justify-align-dark {
|
||||
background-image: url(/public/images/icons/justify-dark.svg);
|
||||
}
|
||||
|
||||
@@ -80,68 +80,70 @@ export default function BugReport() {
|
||||
} my-1`}
|
||||
/>
|
||||
<div className="grid grid-cols-12 gap-8 my-6 mx-8">
|
||||
<div className="col-span-4 card-theme p-5 rounded-md">
|
||||
<div className="flex items-center">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Describe the bug </div>
|
||||
<div className="col-span-4">
|
||||
<div className="card-theme p-6 rounded-md">
|
||||
<div className="flex items-center">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Describe the bug </div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Please provide a clear and concise description of what the bug is.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Steps to reproduce the bug </div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Please provide the steps of how to reproduce the bug.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Expected behaviour</div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Tell us what you expected to see vs what you saw.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Your browser and device</div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
What web browser and device did you encounter the bug on.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Screenshots</div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Add any relevant images if possible.
|
||||
</div>
|
||||
<div className="flex items-center justify-center my-2">
|
||||
<hr
|
||||
className={`${
|
||||
theme === "dark" ? "border-zinc-700" : "border-zinc-300"
|
||||
} flex-grow`}
|
||||
/>
|
||||
<div className="text-sm font-semibold m-2">Alternatively</div>
|
||||
<hr
|
||||
className={`${
|
||||
theme === "dark" ? "border-zinc-700" : "border-zinc-300"
|
||||
} flex-grow`}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
block
|
||||
icon={<IconGithubLogo />}
|
||||
style={{ backgroundColor: "#239144", color: "white" }}
|
||||
onClick={() => {
|
||||
window.open(
|
||||
"https://github.com/drawdb-io/drawdb-issues/issues",
|
||||
"_self"
|
||||
);
|
||||
}}
|
||||
>
|
||||
Add an issue
|
||||
</Button>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Please provide a clear and concise description of what the bug is.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Steps to reproduce the bug </div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Please provide the steps of how to reproduce the bug.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Expected behaviour</div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Tell us what you expected to see vs what you saw.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Your browser and device</div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
What web browser and device did you encounter the bug on.
|
||||
</div>
|
||||
<div className="flex items-center mt-3">
|
||||
<IconPaperclip />
|
||||
<div className="font-bold ms-1">Screenshots</div>
|
||||
</div>
|
||||
<div className="text-sm mt-1">
|
||||
Add any relevant images if possible.
|
||||
</div>
|
||||
<div className="flex items-center justify-center my-2">
|
||||
<hr
|
||||
className={`${
|
||||
theme === "dark" ? "border-zinc-700" : "border-zinc-300"
|
||||
} flex-grow`}
|
||||
/>
|
||||
<div className="text-sm font-semibold m-2">Alternatively</div>
|
||||
<hr
|
||||
className={`${
|
||||
theme === "dark" ? "border-zinc-700" : "border-zinc-300"
|
||||
} flex-grow`}
|
||||
/>
|
||||
</div>
|
||||
<Button
|
||||
block
|
||||
icon={<IconGithubLogo />}
|
||||
style={{ backgroundColor: "#239144", color: "white" }}
|
||||
onClick={() => {
|
||||
window.open(
|
||||
"https://github.com/drawdb-io/drawdb-issues/issues",
|
||||
"_self"
|
||||
);
|
||||
}}
|
||||
>
|
||||
Add an issue
|
||||
</Button>
|
||||
</div>
|
||||
<div className="col-span-8">
|
||||
<Banner
|
||||
@@ -159,7 +161,7 @@ export default function BugReport() {
|
||||
/>
|
||||
<div className="p-5 mt-6 card-theme rounded-md">
|
||||
<Input placeholder="Title" />
|
||||
<RichEditor />
|
||||
<RichEditor theme={theme} />
|
||||
<Upload
|
||||
action="#"
|
||||
beforeUpload={({ file, fileList }) => {}}
|
||||
|
||||
36
src/plugins/AutoLinkPlugin.js
Normal file
36
src/plugins/AutoLinkPlugin.js
Normal file
@@ -0,0 +1,36 @@
|
||||
import { AutoLinkPlugin } from "@lexical/react/LexicalAutoLinkPlugin";
|
||||
|
||||
const URL_MATCHER =
|
||||
/((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)/;
|
||||
|
||||
const EMAIL_MATCHER =
|
||||
/(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
|
||||
|
||||
const MATCHERS = [
|
||||
(text) => {
|
||||
const match = URL_MATCHER.exec(text);
|
||||
return (
|
||||
match && {
|
||||
index: match.index,
|
||||
length: match[0].length,
|
||||
text: match[0],
|
||||
url: match[0],
|
||||
}
|
||||
);
|
||||
},
|
||||
(text) => {
|
||||
const match = EMAIL_MATCHER.exec(text);
|
||||
return (
|
||||
match && {
|
||||
index: match.index,
|
||||
length: match[0].length,
|
||||
text: match[0],
|
||||
url: `mailto:${match[0]}`,
|
||||
}
|
||||
);
|
||||
},
|
||||
];
|
||||
|
||||
export default function PlaygroundAutoLinkPlugin() {
|
||||
return <AutoLinkPlugin matchers={MATCHERS} />;
|
||||
}
|
||||
11
src/plugins/CodeHighlightPlugin.js
Normal file
11
src/plugins/CodeHighlightPlugin.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { registerCodeHighlighting } from "@lexical/code";
|
||||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||||
import { useEffect } from "react";
|
||||
|
||||
export default function CodeHighlightPlugin() {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
useEffect(() => {
|
||||
return registerCodeHighlighting(editor);
|
||||
}, [editor]);
|
||||
return null;
|
||||
}
|
||||
68
src/plugins/ListMaxIndentLevelPlugin.js
Normal file
68
src/plugins/ListMaxIndentLevelPlugin.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import { $getListDepth, $isListItemNode, $isListNode } from "@lexical/list";
|
||||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||||
import {
|
||||
$getSelection,
|
||||
$isElementNode,
|
||||
$isRangeSelection,
|
||||
INDENT_CONTENT_COMMAND,
|
||||
COMMAND_PRIORITY_HIGH,
|
||||
} from "lexical";
|
||||
import { useEffect } from "react";
|
||||
|
||||
function getElementNodesInSelection(selection) {
|
||||
const nodesInSelection = selection.getNodes();
|
||||
|
||||
if (nodesInSelection.length === 0) {
|
||||
return new Set([
|
||||
selection.anchor.getNode().getParentOrThrow(),
|
||||
selection.focus.getNode().getParentOrThrow(),
|
||||
]);
|
||||
}
|
||||
|
||||
return new Set(
|
||||
nodesInSelection.map((n) => ($isElementNode(n) ? n : n.getParentOrThrow()))
|
||||
);
|
||||
}
|
||||
|
||||
function isIndentPermitted(maxDepth) {
|
||||
const selection = $getSelection();
|
||||
|
||||
if (!$isRangeSelection(selection)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const elementNodesInSelection = getElementNodesInSelection(selection);
|
||||
|
||||
let totalDepth = 0;
|
||||
|
||||
for (const elementNode of elementNodesInSelection) {
|
||||
if ($isListNode(elementNode)) {
|
||||
totalDepth = Math.max($getListDepth(elementNode) + 1, totalDepth);
|
||||
} else if ($isListItemNode(elementNode)) {
|
||||
const parent = elementNode.getParent();
|
||||
if (!$isListNode(parent)) {
|
||||
throw new Error(
|
||||
"ListMaxIndentLevelPlugin: A ListItemNode must have a ListNode for a parent."
|
||||
);
|
||||
}
|
||||
|
||||
totalDepth = Math.max($getListDepth(parent) + 1, totalDepth);
|
||||
}
|
||||
}
|
||||
|
||||
return totalDepth <= maxDepth;
|
||||
}
|
||||
|
||||
export default function ListMaxIndentLevelPlugin({ maxDepth }) {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
|
||||
useEffect(() => {
|
||||
return editor.registerCommand(
|
||||
INDENT_CONTENT_COMMAND,
|
||||
() => !isIndentPermitted(maxDepth ?? 7),
|
||||
COMMAND_PRIORITY_HIGH
|
||||
);
|
||||
}, [editor, maxDepth]);
|
||||
|
||||
return null;
|
||||
}
|
||||
740
src/plugins/ToolbarPlugin.js
Normal file
740
src/plugins/ToolbarPlugin.js
Normal file
@@ -0,0 +1,740 @@
|
||||
import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext";
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||||
import {
|
||||
CAN_REDO_COMMAND,
|
||||
CAN_UNDO_COMMAND,
|
||||
REDO_COMMAND,
|
||||
UNDO_COMMAND,
|
||||
SELECTION_CHANGE_COMMAND,
|
||||
FORMAT_TEXT_COMMAND,
|
||||
FORMAT_ELEMENT_COMMAND,
|
||||
$getSelection,
|
||||
$isRangeSelection,
|
||||
$createParagraphNode,
|
||||
$getNodeByKey,
|
||||
} from "lexical";
|
||||
import { $isLinkNode, TOGGLE_LINK_COMMAND } from "@lexical/link";
|
||||
import {
|
||||
$isParentElementRTL,
|
||||
$wrapNodes,
|
||||
$isAtNodeEnd,
|
||||
} from "@lexical/selection";
|
||||
import { $getNearestNodeOfType, mergeRegister } from "@lexical/utils";
|
||||
import {
|
||||
INSERT_ORDERED_LIST_COMMAND,
|
||||
INSERT_UNORDERED_LIST_COMMAND,
|
||||
REMOVE_LIST_COMMAND,
|
||||
$isListNode,
|
||||
ListNode,
|
||||
} from "@lexical/list";
|
||||
import { createPortal } from "react-dom";
|
||||
import {
|
||||
$createHeadingNode,
|
||||
$createQuoteNode,
|
||||
$isHeadingNode,
|
||||
} from "@lexical/rich-text";
|
||||
import {
|
||||
$createCodeNode,
|
||||
$isCodeNode,
|
||||
getDefaultCodeLanguage,
|
||||
getCodeLanguages,
|
||||
} from "@lexical/code";
|
||||
|
||||
const LowPriority = 1;
|
||||
|
||||
const supportedBlockTypes = new Set([
|
||||
"paragraph",
|
||||
"quote",
|
||||
"code",
|
||||
"h1",
|
||||
"h2",
|
||||
"ul",
|
||||
"ol",
|
||||
]);
|
||||
|
||||
const blockTypeToBlockName = {
|
||||
code: "Code Block",
|
||||
h1: "Large Heading",
|
||||
h2: "Small Heading",
|
||||
h3: "Heading",
|
||||
h4: "Heading",
|
||||
h5: "Heading",
|
||||
ol: "Numbered List",
|
||||
paragraph: "Paragraph",
|
||||
quote: "Quote",
|
||||
ul: "Bulleted List",
|
||||
};
|
||||
|
||||
function Divider() {
|
||||
return <div className="divider" />;
|
||||
}
|
||||
|
||||
function positionEditorElement(editor, rect) {
|
||||
if (rect === null) {
|
||||
editor.style.opacity = "0";
|
||||
editor.style.top = "-1000px";
|
||||
editor.style.left = "-1000px";
|
||||
} else {
|
||||
editor.style.opacity = "1";
|
||||
editor.style.top = `${rect.top + rect.height + window.pageYOffset + 10}px`;
|
||||
editor.style.left = `${
|
||||
rect.left + window.pageXOffset - editor.offsetWidth / 2 + rect.width / 2
|
||||
}px`;
|
||||
}
|
||||
}
|
||||
|
||||
function FloatingLinkEditor({ editor, theme }) {
|
||||
const editorRef = useRef(null);
|
||||
const inputRef = useRef(null);
|
||||
const mouseDownRef = useRef(false);
|
||||
const [linkUrl, setLinkUrl] = useState("");
|
||||
const [isEditMode, setEditMode] = useState(false);
|
||||
const [lastSelection, setLastSelection] = useState(null);
|
||||
|
||||
const updateLinkEditor = useCallback(() => {
|
||||
const selection = $getSelection();
|
||||
if ($isRangeSelection(selection)) {
|
||||
const node = getSelectedNode(selection);
|
||||
const parent = node.getParent();
|
||||
if ($isLinkNode(parent)) {
|
||||
setLinkUrl(parent.getURL());
|
||||
} else if ($isLinkNode(node)) {
|
||||
setLinkUrl(node.getURL());
|
||||
} else {
|
||||
setLinkUrl("");
|
||||
}
|
||||
}
|
||||
const editorElem = editorRef.current;
|
||||
const nativeSelection = window.getSelection();
|
||||
const activeElement = document.activeElement;
|
||||
|
||||
if (editorElem === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
const rootElement = editor.getRootElement();
|
||||
if (
|
||||
selection !== null &&
|
||||
!nativeSelection.isCollapsed &&
|
||||
rootElement !== null &&
|
||||
rootElement.contains(nativeSelection.anchorNode)
|
||||
) {
|
||||
const domRange = nativeSelection.getRangeAt(0);
|
||||
let rect;
|
||||
if (nativeSelection.anchorNode === rootElement) {
|
||||
let inner = rootElement;
|
||||
while (inner.firstElementChild != null) {
|
||||
inner = inner.firstElementChild;
|
||||
}
|
||||
rect = inner.getBoundingClientRect();
|
||||
} else {
|
||||
rect = domRange.getBoundingClientRect();
|
||||
}
|
||||
|
||||
if (!mouseDownRef.current) {
|
||||
positionEditorElement(editorElem, rect);
|
||||
}
|
||||
setLastSelection(selection);
|
||||
} else if (!activeElement || activeElement.className !== "link-input") {
|
||||
positionEditorElement(editorElem, null);
|
||||
setLastSelection(null);
|
||||
setEditMode(false);
|
||||
setLinkUrl("");
|
||||
}
|
||||
|
||||
return true;
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
return mergeRegister(
|
||||
editor.registerUpdateListener(({ editorState }) => {
|
||||
editorState.read(() => {
|
||||
updateLinkEditor();
|
||||
});
|
||||
}),
|
||||
|
||||
editor.registerCommand(
|
||||
SELECTION_CHANGE_COMMAND,
|
||||
() => {
|
||||
updateLinkEditor();
|
||||
return true;
|
||||
},
|
||||
LowPriority
|
||||
)
|
||||
);
|
||||
}, [editor, updateLinkEditor]);
|
||||
|
||||
useEffect(() => {
|
||||
editor.getEditorState().read(() => {
|
||||
updateLinkEditor();
|
||||
});
|
||||
}, [editor, updateLinkEditor]);
|
||||
|
||||
useEffect(() => {
|
||||
if (isEditMode && inputRef.current) {
|
||||
inputRef.current.focus();
|
||||
}
|
||||
}, [isEditMode]);
|
||||
|
||||
return (
|
||||
<div ref={editorRef} className="link-editor">
|
||||
{isEditMode ? (
|
||||
<input
|
||||
ref={inputRef}
|
||||
className="link-input"
|
||||
value={linkUrl}
|
||||
onChange={(event) => {
|
||||
setLinkUrl(event.target.value);
|
||||
}}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
if (lastSelection !== null) {
|
||||
if (linkUrl !== "") {
|
||||
editor.dispatchCommand(TOGGLE_LINK_COMMAND, linkUrl);
|
||||
}
|
||||
setEditMode(false);
|
||||
}
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
setEditMode(false);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<>
|
||||
<div className="link-input">
|
||||
<a href={linkUrl} target="_blank" rel="noopener noreferrer">
|
||||
{linkUrl}
|
||||
</a>
|
||||
<div
|
||||
className={`link-edit${theme === "dark" ? "-dark" : ""}`}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
onMouseDown={(event) => event.preventDefault()}
|
||||
onClick={() => {
|
||||
setEditMode(true);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Select({ onChange, className, options, value }) {
|
||||
return (
|
||||
<select className={className} onChange={onChange} value={value}>
|
||||
<option hidden={true} value="" />
|
||||
{options.map((option) => (
|
||||
<option className="option" key={option} value={option}>
|
||||
{option}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
);
|
||||
}
|
||||
|
||||
function getSelectedNode(selection) {
|
||||
const anchor = selection.anchor;
|
||||
const focus = selection.focus;
|
||||
const anchorNode = selection.anchor.getNode();
|
||||
const focusNode = selection.focus.getNode();
|
||||
if (anchorNode === focusNode) {
|
||||
return anchorNode;
|
||||
}
|
||||
const isBackward = selection.isBackward();
|
||||
if (isBackward) {
|
||||
return $isAtNodeEnd(focus) ? anchorNode : focusNode;
|
||||
} else {
|
||||
return $isAtNodeEnd(anchor) ? focusNode : anchorNode;
|
||||
}
|
||||
}
|
||||
|
||||
function BlockOptionsDropdownList({
|
||||
editor,
|
||||
blockType,
|
||||
toolbarRef,
|
||||
setShowBlockOptionsDropDown,
|
||||
theme,
|
||||
}) {
|
||||
const dropDownRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
const toolbar = toolbarRef.current;
|
||||
const dropDown = dropDownRef.current;
|
||||
|
||||
if (toolbar !== null && dropDown !== null) {
|
||||
const { top, left } = toolbar.getBoundingClientRect();
|
||||
dropDown.style.top = `${top + 40}px`;
|
||||
dropDown.style.left = `${left}px`;
|
||||
}
|
||||
}, [dropDownRef, toolbarRef]);
|
||||
|
||||
useEffect(() => {
|
||||
const dropDown = dropDownRef.current;
|
||||
const toolbar = toolbarRef.current;
|
||||
|
||||
if (dropDown !== null && toolbar !== null) {
|
||||
const handle = (event) => {
|
||||
const target = event.target;
|
||||
|
||||
if (!dropDown.contains(target) && !toolbar.contains(target)) {
|
||||
setShowBlockOptionsDropDown(false);
|
||||
}
|
||||
};
|
||||
document.addEventListener("click", handle);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener("click", handle);
|
||||
};
|
||||
}
|
||||
}, [dropDownRef, setShowBlockOptionsDropDown, toolbarRef]);
|
||||
|
||||
const formatParagraph = () => {
|
||||
if (blockType !== "paragraph") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createParagraphNode());
|
||||
}
|
||||
});
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
const formatLargeHeading = () => {
|
||||
if (blockType !== "h1") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createHeadingNode("h1"));
|
||||
}
|
||||
});
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
const formatSmallHeading = () => {
|
||||
if (blockType !== "h2") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createHeadingNode("h2"));
|
||||
}
|
||||
});
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
const formatBulletList = () => {
|
||||
if (blockType !== "ul") {
|
||||
editor.dispatchCommand(INSERT_UNORDERED_LIST_COMMAND);
|
||||
} else {
|
||||
editor.dispatchCommand(REMOVE_LIST_COMMAND);
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
const formatNumberedList = () => {
|
||||
if (blockType !== "ol") {
|
||||
editor.dispatchCommand(INSERT_ORDERED_LIST_COMMAND);
|
||||
} else {
|
||||
editor.dispatchCommand(REMOVE_LIST_COMMAND);
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
const formatQuote = () => {
|
||||
if (blockType !== "quote") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createQuoteNode());
|
||||
}
|
||||
});
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
const formatCode = () => {
|
||||
if (blockType !== "code") {
|
||||
editor.update(() => {
|
||||
const selection = $getSelection();
|
||||
|
||||
if ($isRangeSelection(selection)) {
|
||||
$wrapNodes(selection, () => $createCodeNode());
|
||||
}
|
||||
});
|
||||
}
|
||||
setShowBlockOptionsDropDown(false);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="dropdown" ref={dropDownRef}>
|
||||
<button className="item" onClick={formatParagraph}>
|
||||
<span className={`icon paragraph${theme === "dark" ? "-dark" : ""}`} />
|
||||
<span className="text">Paragraph</span>
|
||||
{blockType === "paragraph" && <span className="active" />}
|
||||
</button>
|
||||
<button className="item" onClick={formatLargeHeading}>
|
||||
<span
|
||||
className={`icon large-heading${theme === "dark" ? "-dark" : ""}`}
|
||||
/>
|
||||
<span className="text">Large Heading</span>
|
||||
{blockType === "h1" && <span className="active" />}
|
||||
</button>
|
||||
<button className="item" onClick={formatSmallHeading}>
|
||||
<span
|
||||
className={`icon small-heading${theme === "dark" ? "-dark" : ""}`}
|
||||
/>
|
||||
<span className="text">Small Heading</span>
|
||||
{blockType === "h2" && <span className="active" />}
|
||||
</button>
|
||||
<button className="item" onClick={formatBulletList}>
|
||||
<span
|
||||
className={`icon bullet-list${theme === "dark" ? "-dark" : ""}`}
|
||||
/>
|
||||
<span className="text">Bullet List</span>
|
||||
{blockType === "ul" && <span className="active" />}
|
||||
</button>
|
||||
<button className="item" onClick={formatNumberedList}>
|
||||
<span
|
||||
className={`icon numbered-list${theme === "dark" ? "-dark" : ""}`}
|
||||
/>
|
||||
<span className="text">Numbered List</span>
|
||||
{blockType === "ol" && <span className="active" />}
|
||||
</button>
|
||||
<button className="item" onClick={formatQuote}>
|
||||
<span className={`icon quote${theme === "dark" ? "-dark" : ""}`} />
|
||||
<span className="text">Quote</span>
|
||||
{blockType === "quote" && <span className="active" />}
|
||||
</button>
|
||||
<button className="item" onClick={formatCode}>
|
||||
<span className={`icon code${theme === "dark" ? "-dark" : ""}`} />
|
||||
<span className="text">Code Block</span>
|
||||
{blockType === "code" && <span className="active" />}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function ToolbarPlugin(props) {
|
||||
const { theme } = props;
|
||||
const [editor] = useLexicalComposerContext();
|
||||
const toolbarRef = useRef(null);
|
||||
const [canUndo, setCanUndo] = useState(false);
|
||||
const [canRedo, setCanRedo] = useState(false);
|
||||
const [blockType, setBlockType] = useState("paragraph");
|
||||
const [selectedElementKey, setSelectedElementKey] = useState(null);
|
||||
const [showBlockOptionsDropDown, setShowBlockOptionsDropDown] =
|
||||
useState(false);
|
||||
const [codeLanguage, setCodeLanguage] = useState("");
|
||||
const [, setIsRTL] = useState(false);
|
||||
const [isLink, setIsLink] = useState(false);
|
||||
const [isBold, setIsBold] = useState(false);
|
||||
const [isItalic, setIsItalic] = useState(false);
|
||||
const [isUnderline, setIsUnderline] = useState(false);
|
||||
const [isStrikethrough, setIsStrikethrough] = useState(false);
|
||||
const [isCode, setIsCode] = useState(false);
|
||||
|
||||
const updateToolbar = useCallback(() => {
|
||||
const selection = $getSelection();
|
||||
if ($isRangeSelection(selection)) {
|
||||
const anchorNode = selection.anchor.getNode();
|
||||
const element =
|
||||
anchorNode.getKey() === "root"
|
||||
? anchorNode
|
||||
: anchorNode.getTopLevelElementOrThrow();
|
||||
const elementKey = element.getKey();
|
||||
const elementDOM = editor.getElementByKey(elementKey);
|
||||
if (elementDOM !== null) {
|
||||
setSelectedElementKey(elementKey);
|
||||
if ($isListNode(element)) {
|
||||
const parentList = $getNearestNodeOfType(anchorNode, ListNode);
|
||||
const type = parentList ? parentList.getTag() : element.getTag();
|
||||
setBlockType(type);
|
||||
} else {
|
||||
const type = $isHeadingNode(element)
|
||||
? element.getTag()
|
||||
: element.getType();
|
||||
setBlockType(type);
|
||||
if ($isCodeNode(element)) {
|
||||
setCodeLanguage(element.getLanguage() || getDefaultCodeLanguage());
|
||||
}
|
||||
}
|
||||
}
|
||||
// Update text format
|
||||
setIsBold(selection.hasFormat("bold"));
|
||||
setIsItalic(selection.hasFormat("italic"));
|
||||
setIsUnderline(selection.hasFormat("underline"));
|
||||
setIsStrikethrough(selection.hasFormat("strikethrough"));
|
||||
setIsCode(selection.hasFormat("code"));
|
||||
setIsRTL($isParentElementRTL(selection));
|
||||
|
||||
// Update links
|
||||
const node = getSelectedNode(selection);
|
||||
const parent = node.getParent();
|
||||
if ($isLinkNode(parent) || $isLinkNode(node)) {
|
||||
setIsLink(true);
|
||||
} else {
|
||||
setIsLink(false);
|
||||
}
|
||||
}
|
||||
}, [editor]);
|
||||
|
||||
useEffect(() => {
|
||||
return mergeRegister(
|
||||
editor.registerUpdateListener(({ editorState }) => {
|
||||
editorState.read(() => {
|
||||
updateToolbar();
|
||||
});
|
||||
}),
|
||||
editor.registerCommand(
|
||||
SELECTION_CHANGE_COMMAND,
|
||||
(_payload, newEditor) => {
|
||||
updateToolbar();
|
||||
return false;
|
||||
},
|
||||
LowPriority
|
||||
),
|
||||
editor.registerCommand(
|
||||
CAN_UNDO_COMMAND,
|
||||
(payload) => {
|
||||
setCanUndo(payload);
|
||||
return false;
|
||||
},
|
||||
LowPriority
|
||||
),
|
||||
editor.registerCommand(
|
||||
CAN_REDO_COMMAND,
|
||||
(payload) => {
|
||||
setCanRedo(payload);
|
||||
return false;
|
||||
},
|
||||
LowPriority
|
||||
)
|
||||
);
|
||||
}, [editor, updateToolbar]);
|
||||
|
||||
const codeLanguges = useMemo(() => getCodeLanguages(), []);
|
||||
const onCodeLanguageSelect = useCallback(
|
||||
(e) => {
|
||||
editor.update(() => {
|
||||
if (selectedElementKey !== null) {
|
||||
const node = $getNodeByKey(selectedElementKey);
|
||||
if ($isCodeNode(node)) {
|
||||
node.setLanguage(e.target.value);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
[editor, selectedElementKey]
|
||||
);
|
||||
|
||||
const insertLink = useCallback(() => {
|
||||
if (!isLink) {
|
||||
editor.dispatchCommand(TOGGLE_LINK_COMMAND, "https://");
|
||||
} else {
|
||||
editor.dispatchCommand(TOGGLE_LINK_COMMAND, null);
|
||||
}
|
||||
}, [editor, isLink]);
|
||||
|
||||
return (
|
||||
<div className="toolbar" ref={toolbarRef}>
|
||||
<button
|
||||
disabled={!canUndo}
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(UNDO_COMMAND);
|
||||
}}
|
||||
className="toolbar-item spaced"
|
||||
aria-label="Undo"
|
||||
>
|
||||
<i className={`format ${theme === "dark" ? "undo-dark" : "undo"}`} />
|
||||
</button>
|
||||
<button
|
||||
disabled={!canRedo}
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(REDO_COMMAND);
|
||||
}}
|
||||
className="toolbar-item"
|
||||
aria-label="Redo"
|
||||
>
|
||||
<i className={`format ${theme === "dark" ? "redo-dark" : "redo"}`} />
|
||||
</button>
|
||||
<Divider />
|
||||
{supportedBlockTypes.has(blockType) && (
|
||||
<>
|
||||
<button
|
||||
className="toolbar-item block-controls"
|
||||
onClick={() =>
|
||||
setShowBlockOptionsDropDown(!showBlockOptionsDropDown)
|
||||
}
|
||||
aria-label="Formatting Options"
|
||||
>
|
||||
<span
|
||||
className={
|
||||
"icon block-type " +
|
||||
blockType +
|
||||
(theme === "dark" ? "-dark" : "")
|
||||
}
|
||||
/>
|
||||
<span className="text">{blockTypeToBlockName[blockType]}</span>
|
||||
<i className={`chevron-down${theme === "dark" ? "-dark" : ""}`} />
|
||||
</button>
|
||||
{showBlockOptionsDropDown &&
|
||||
createPortal(
|
||||
<BlockOptionsDropdownList
|
||||
editor={editor}
|
||||
blockType={blockType}
|
||||
toolbarRef={toolbarRef}
|
||||
setShowBlockOptionsDropDown={setShowBlockOptionsDropDown}
|
||||
theme={theme}
|
||||
/>,
|
||||
document.body
|
||||
)}
|
||||
<Divider />
|
||||
</>
|
||||
)}
|
||||
{blockType === "code" ? (
|
||||
<>
|
||||
<Select
|
||||
className="toolbar-item code-language"
|
||||
onChange={onCodeLanguageSelect}
|
||||
options={codeLanguges}
|
||||
value={codeLanguage}
|
||||
/>
|
||||
<i
|
||||
className={`chevron-down${theme === "dark" ? "-dark" : ""} inside`}
|
||||
/>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "bold");
|
||||
}}
|
||||
className={"toolbar-item spaced " + (isBold ? "active" : "")}
|
||||
aria-label="Format Bold"
|
||||
>
|
||||
<i className={`format bold${theme === "dark" ? "-dark" : ""}`} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "italic");
|
||||
}}
|
||||
className={"toolbar-item spaced " + (isItalic ? "active" : "")}
|
||||
aria-label="Format Italics"
|
||||
>
|
||||
<i className={`format italic${theme === "dark" ? "-dark" : ""}`} />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "underline");
|
||||
}}
|
||||
className={"toolbar-item spaced " + (isUnderline ? "active" : "")}
|
||||
aria-label="Format Underline"
|
||||
>
|
||||
<i
|
||||
className={`format underline${theme === "dark" ? "-dark" : ""}`}
|
||||
/>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "strikethrough");
|
||||
}}
|
||||
className={
|
||||
"toolbar-item spaced " + (isStrikethrough ? "active" : "")
|
||||
}
|
||||
aria-label="Format Strikethrough"
|
||||
>
|
||||
<i
|
||||
className={`format strikethrough${
|
||||
theme === "dark" ? "-dark" : ""
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_TEXT_COMMAND, "code");
|
||||
}}
|
||||
className={"toolbar-item spaced " + (isCode ? "active" : "")}
|
||||
aria-label="Insert Code"
|
||||
>
|
||||
<i className={`format code${theme === "dark" ? "-dark" : ""}`} />
|
||||
</button>
|
||||
<button
|
||||
onClick={insertLink}
|
||||
className={"toolbar-item spaced " + (isLink ? "active" : "")}
|
||||
aria-label="Insert Link"
|
||||
>
|
||||
<i className={`format link${theme === "dark" ? "-dark" : ""}`} />
|
||||
</button>
|
||||
{isLink &&
|
||||
createPortal(
|
||||
<FloatingLinkEditor editor={editor} theme={theme} />,
|
||||
document.body
|
||||
)}
|
||||
<Divider />
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "left");
|
||||
}}
|
||||
className="toolbar-item spaced"
|
||||
aria-label="Left Align"
|
||||
>
|
||||
<i
|
||||
className={`format ${
|
||||
theme === "dark" ? "left-align-dark" : "left-align"
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "center");
|
||||
}}
|
||||
className="toolbar-item spaced"
|
||||
aria-label="Center Align"
|
||||
>
|
||||
<i
|
||||
className={`format ${
|
||||
theme === "dark" ? "center-align-dark" : "center-align"
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "right");
|
||||
}}
|
||||
className="toolbar-item spaced"
|
||||
aria-label="Right Align"
|
||||
>
|
||||
<i
|
||||
className={`format ${
|
||||
theme === "dark" ? "right-align-dark" : "right-align"
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
editor.dispatchCommand(FORMAT_ELEMENT_COMMAND, "justify");
|
||||
}}
|
||||
className="toolbar-item"
|
||||
aria-label="Justify Align"
|
||||
>
|
||||
<i
|
||||
className={`format ${
|
||||
theme === "dark" ? "justify-align-dark" : "justify-align"
|
||||
}`}
|
||||
/>
|
||||
</button>{" "}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user