Skip to content

Commit bcff7b0

Browse files
committed
chore: update code block
1 parent 71b141e commit bcff7b0

File tree

5 files changed

+45
-276
lines changed

5 files changed

+45
-276
lines changed

docs/extensions/CodeBlock/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ next:
88

99
# CodeBlock
1010

11-
- The `CodeBlock` extension allows you to add code blocks to your editor. It uses [prism-code-editor](https://prism-code-editor.netlify.app/) for syntax highlighting.
11+
- The `CodeBlock` extension allows you to add code blocks to your editor. It uses [prism-code-editor-lightweight](https://github.com/hunghg255/prism-code-editor-lightweight) for syntax highlighting.
1212

1313
## Usage
1414

1515
```tsx
1616
import { CodeBlock } from 'reactjs-tiptap-editor/extension-bundle'; // [!code ++]
1717

18+
import 'prism-code-editor-lightweight/layout.css'; // [!code ++]
19+
import 'prism-code-editor-lightweight/themes/github-dark.css'; // [!code ++]
20+
1821
const extensions = [
1922
...,
2023
// Import Extensions Here

playground/src/App.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ import 'reactjs-tiptap-editor/style.css'
5757
import 'katex/dist/katex.min.css'
5858
import 'easydrawer/styles.css'
5959

60+
import 'prism-code-editor-lightweight/layout.css';
61+
import "prism-code-editor-lightweight/themes/github-dark.css"
62+
63+
6064
function convertBase64ToBlob(base64: string) {
6165
const arr = base64.split(',')
6266
const mime = arr[0].match(/:(.*?);/)![1]
@@ -197,7 +201,31 @@ const extensions = [
197201
Twitter,
198202
]
199203

200-
const DEFAULT = `<h1 style="text-align: center">Rich Text Editor</h1><p>A modern WYSIWYG rich text editor based on <a target="_blank" rel="noopener noreferrer nofollow" class="link" href="https://github.com/scrumpy/tiptap">tiptap</a> and <a target="_blank" rel="noopener noreferrer nofollow" class="link" href="https://ui.shadcn.com/">shadcn ui</a> for Reactjs</p><p></p><p style="text-align: center"></p><div style="text-align: center;" class="image"><img height="auto" src="https://picsum.photos/1920/1080.webp?t=1" align="center" width="500"></div><p></p><div data-type="horizontalRule"><hr></div><h2>Demo</h2><p>👉<a target="_blank" rel="noopener noreferrer nofollow" class="link" href="https://reactjs-tiptap-editor.vercel.app/">Demo</a></p><h2>Features</h2><ul><li><p>Use <a target="_blank" rel="noopener noreferrer nofollow" class="link" href="https://ui.shadcn.com/">shadcn ui</a> components</p></li><li><p>Markdown support</p></li><li><p>TypeScript support</p></li><li><p>I18n support (vi, en, zh, pt)</p></li><li><p>React support</p></li><li><p>Slash Commands</p></li><li><p>Multi Column</p></li><li><p>TailwindCss</p></li><li><p>Support emoji</p></li><li><p>Support iframe</p></li><li><p>Support mermaid</p></li></ul><h2>Installation</h2><pre code="pnpm install reactjs-tiptap-editor" language="bash" linenumbers="true" wordwrap="false" tabsize="2" shouldfocus="false"><code>pnpm install reactjs-tiptap-editor</code></pre><p></p>`
204+
const DEFAULT = `<pre code="const options = {
205+
language: 'html',
206+
insertSpaces: true,
207+
tabSize: 2,
208+
lineNumbers: true,
209+
readOnly: false,
210+
wordWrap: false,
211+
value: code,
212+
rtl: false,
213+
onUpdate(code) {},
214+
onSelectionChange([start, end, direction], code) {},
215+
onTokenize(tokens, language, code) {}
216+
}" language="typescript" linenumbers="true" wordwrap="false" tabsize="2" shouldfocus="false"><code>const options = {
217+
language: 'html',
218+
insertSpaces: true,
219+
tabSize: 2,
220+
lineNumbers: true,
221+
readOnly: false,
222+
wordWrap: false,
223+
value: code,
224+
rtl: false,
225+
onUpdate(code) {},
226+
onSelectionChange([start, end, direction], code) {},
227+
onTokenize(tokens, language, code) {}
228+
}</code></pre>`
201229

202230
function debounce(func: any, wait: number) {
203231
let timeout: NodeJS.Timeout

src/constants/resetCSS.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const RESET_CSS = `
2-
.reactjs-tiptap-editor,
2+
.reactjs-tiptap-editor,
33
.richtext-dialog-content {
44
button,
55
input:where([type=button]),
@@ -12,8 +12,7 @@ export const RESET_CSS = `
1212
1313
input,
1414
optgroup,
15-
select,
16-
textarea {
15+
select {
1716
font-family: inherit;
1817
font-feature-settings: inherit;
1918
font-variation-settings: inherit;
@@ -79,21 +78,17 @@ export const RESET_CSS = `
7978
border-collapse: collapse;
8079
}
8180
82-
input, textarea {
81+
input {
8382
border-width: 1px;
8483
}
8584
86-
textarea {
87-
resize: vertical;
88-
}
8985
90-
input::placeholder,
91-
textarea::placeholder {
86+
input::placeholder {
9287
opacity: 1;
9388
color: #9ca3af;
9489
}
9590
96-
button, input, textarea {
91+
button, input {
9792
cursor: pointer;
9893
color: inherit;
9994
}
@@ -131,7 +126,7 @@ div[data-tippy-root] {
131126
background-color: hsl(var(--richtext-background));
132127
color: hsl(var(--richtext-foreground));
133128
134-
button, input, textarea {
129+
button, input {
135130
cursor: pointer;
136131
color: inherit;
137132
}

src/extensions/CodeBlock/components/NodeViewCodeBlock/NodeViewCodeBlock.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/ban-ts-comment */
21
import React, { useCallback, useEffect, useRef } from 'react';
32

43
import { NodeViewWrapper } from '@tiptap/react';
@@ -46,7 +45,6 @@ import 'prism-code-editor-lightweight/prism/languages/typescript';
4645
import 'prism-code-editor-lightweight/prism/languages/tsx';
4746
import 'prism-code-editor-lightweight/prism/languages/yaml';
4847
import 'prism-code-editor-lightweight/prism/languages/regex';
49-
import 'prism-code-editor-lightweight/layout.css';
5048

5149
import { createEditor, type PrismEditor } from 'prism-code-editor-lightweight';
5250
import { defaultCommands, editHistory } from 'prism-code-editor-lightweight/commands';
@@ -88,7 +86,7 @@ export function NodeViewCodeBlock(props: any) {
8886

8987
const containerRef: any = useRef<HTMLPreElement>(null);
9088

91-
const deleteMe = useCallback(() => deleteNode(CodeBlock.name, props?.editor), [props?.edito]);
89+
const deleteMe = useCallback(() => deleteNode(CodeBlock.name, props?.editor), [props?.editor]);
9290

9391
const codeEditor = useRef<PrismEditor | null>(null);
9492
const code = props.node.attrs.code || props.node.textContent || '';
@@ -120,6 +118,7 @@ export function NodeViewCodeBlock(props: any) {
120118

121119
const validateAndUpdateLanguage = (attrs: any) => {
122120
const validatedAttrs = { ...attrs };
121+
123122
if (validatedAttrs.language && !languages.some(lang => lang.value === validatedAttrs.language)) {
124123
validatedAttrs.language = 'plaintext';
125124
props.updateAttributes({
@@ -170,8 +169,7 @@ export function NodeViewCodeBlock(props: any) {
170169
}, [containerRef]);
171170

172171
useEffect(() => {
173-
//@ts-expect-error
174-
if (codeEditor.current?.setOption) {
172+
if (codeEditor.current?.setOptions) {
175173
codeEditor.current?.setOptions({
176174
readOnly: props.editor.isEditable,
177175
});
@@ -180,12 +178,12 @@ export function NodeViewCodeBlock(props: any) {
180178
}, [codeEditor, props.editor.isEditable]);
181179

182180
useEffect(() => {
183-
//@ts-expect-error
184-
if (codeEditor.current?.setOption) {
181+
if (codeEditor.current?.setOptions) {
185182
const attrs = validateAndUpdateLanguage(props.node.attrs);
183+
186184
codeEditor.current?.setOptions(attrs);
187185
}
188-
}, [codeEditor, props.node.attrs.language, props.node.attrs.lineNumbers, props.node.attrs.wordWrap, props.node.attrs.tabSize, props.node.attrs]);
186+
}, [codeEditor, props.node.attrs]);
189187

190188
return (
191189
<NodeViewWrapper className={clsx(styles.wrap, 'render-wrapper')}>
@@ -218,7 +216,6 @@ export function NodeViewCodeBlock(props: any) {
218216
languages?.map((lang) => {
219217
return (
220218
<SelectItem
221-
className="focus:richtext-bg-[#5a5d5e4f] focus:richtext-text-white"
222219
key={lang.value}
223220
value={lang.value}
224221
>
@@ -298,7 +295,6 @@ export function NodeViewCodeBlock(props: any) {
298295
tabSizes?.map((size) => {
299296
return (
300297
<SelectItem
301-
className="focus:richtext-bg-[#5a5d5e4f] focus:richtext-text-white"
302298
key={size}
303299
value={size as any}
304300
>

0 commit comments

Comments
 (0)