-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for using Quill with React
- Loading branch information
Showing
13 changed files
with
199 additions
and
50 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
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
9 changes: 0 additions & 9 deletions
9
packages/website/src/playground/custom-formats/externalResources.json
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
packages/website/src/playground/custom-formats/playground.json
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,12 @@ | ||
{ | ||
"template": "static", | ||
"externalResources": [ | ||
"{{site.highlightjs}}/highlight.min.js", | ||
"{{site.highlightjs}}/styles/atom-one-dark.min.css", | ||
"{{site.katex}}/katex.min.js", | ||
"{{site.katex}}/katex.min.css", | ||
"{{site.cdn}}/quill.snow.css", | ||
"{{site.cdn}}/quill.bubble.css", | ||
"{{site.cdn}}/quill.js" | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
{ | ||
"template": "static", | ||
"externalResources": [ | ||
"{{site.highlightjs}}/highlight.min.js", | ||
"{{site.highlightjs}}/styles/atom-one-dark.min.css", | ||
"{{site.katex}}/katex.min.js", | ||
"{{site.katex}}/katex.min.css", | ||
"{{site.cdn}}/quill.snow.css", | ||
"{{site.cdn}}/quill.bubble.css", | ||
"{{site.cdn}}/quill.js" | ||
] | ||
} |
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,61 @@ | ||
import React, { useRef, useState } from 'react'; | ||
import Editor from './Editor'; | ||
|
||
const Delta = Quill.import('delta'); | ||
|
||
const App = () => { | ||
const [range, setRange] = useState(); | ||
const [lastChange, setLastChange] = useState(); | ||
const [readOnly, setReadOnly] = useState(false); | ||
|
||
// Use a ref to access the quill instance directly | ||
const quillRef = useRef(); | ||
|
||
return ( | ||
<div> | ||
<Editor | ||
ref={quillRef} | ||
readOnly={readOnly} | ||
defaultValue={new Delta() | ||
.insert('Hello') | ||
.insert('\n', { header: 1 }) | ||
.insert('Some ') | ||
.insert('initial', { bold: true }) | ||
.insert(' ') | ||
.insert('content', { underline: true }) | ||
.insert('\n')} | ||
onSelectionChange={setRange} | ||
onTextChange={setLastChange} | ||
/> | ||
<div class="controls"> | ||
<label> | ||
Read Only:{' '} | ||
<input | ||
type="checkbox" | ||
value={readOnly} | ||
onChange={(e) => setReadOnly(e.target.checked)} | ||
/> | ||
</label> | ||
<button | ||
className="controls-right" | ||
type="button" | ||
onClick={() => { | ||
alert(quillRef.current?.getLength()); | ||
}} | ||
> | ||
Get Content Length | ||
</button> | ||
</div> | ||
<div className="state"> | ||
<div className="state-title">Current Range:</div> | ||
{range ? JSON.stringify(range) : 'Empty'} | ||
</div> | ||
<div className="state"> | ||
<div className="state-title">Last Change:</div> | ||
{lastChange ? JSON.stringify(lastChange.ops) : 'Empty'} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default App; |
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,55 @@ | ||
import React, { forwardRef, useEffect, useLayoutEffect, useRef } from 'react'; | ||
|
||
// Editor is an uncontrolled React component | ||
const Editor = forwardRef( | ||
({ readOnly, defaultValue, onTextChange, onSelectionChange }, ref) => { | ||
const containerRef = useRef(null); | ||
const defaultValueRef = useRef(defaultValue); | ||
const onTextChangeRef = useRef(onTextChange); | ||
const onSelectionChangeRef = useRef(onSelectionChange); | ||
|
||
useLayoutEffect(() => { | ||
onTextChangeRef.current = onTextChange; | ||
onSelectionChangeRef.current = onSelectionChange; | ||
}); | ||
|
||
useEffect(() => { | ||
ref.current?.enable(!readOnly); | ||
}, [ref, readOnly]); | ||
|
||
useEffect(() => { | ||
const container = containerRef.current; | ||
const editorContainer = container.appendChild( | ||
container.ownerDocument.createElement('div'), | ||
); | ||
const quill = new Quill(editorContainer, { | ||
theme: 'snow', | ||
}); | ||
|
||
ref.current = quill; | ||
|
||
if (defaultValueRef.current) { | ||
quill.setContents(defaultValueRef.current); | ||
} | ||
|
||
quill.on(Quill.events.TEXT_CHANGE, (...args) => { | ||
onTextChangeRef.current?.(...args); | ||
}); | ||
|
||
quill.on(Quill.events.SELECTION_CHANGE, (...args) => { | ||
onSelectionChangeRef.current?.(...args); | ||
}); | ||
|
||
return () => { | ||
ref.current = null; | ||
container.innerHTML = ''; | ||
}; | ||
}, []); | ||
|
||
return <div ref={containerRef}></div>; | ||
}, | ||
); | ||
|
||
Editor.displayName = 'Editor'; | ||
|
||
export default Editor; |
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,13 @@ | ||
{ | ||
"template": "react", | ||
"externalResources": [ | ||
"{{site.highlightjs}}/highlight.min.js", | ||
"{{site.highlightjs}}/styles/atom-one-dark.min.css", | ||
"{{site.katex}}/katex.min.js", | ||
"{{site.katex}}/katex.min.css", | ||
"{{site.cdn}}/quill.snow.css", | ||
"{{site.cdn}}/quill.bubble.css", | ||
"{{site.cdn}}/quill.js" | ||
], | ||
"activeFile": "App.js" | ||
} |
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,20 @@ | ||
.controls { | ||
display: flex; | ||
border: 1px solid #ccc; | ||
border-top: 0; | ||
padding: 10px; | ||
} | ||
|
||
.controls-right { | ||
margin-left: auto; | ||
} | ||
|
||
.state { | ||
margin: 10px 0; | ||
font-family: monospace; | ||
} | ||
|
||
.state-title { | ||
color: #999; | ||
text-transform: uppercase; | ||
} |
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
{ | ||
"template": "static", | ||
"externalResources": [ | ||
"{{site.highlightjs}}/highlight.min.js", | ||
"{{site.highlightjs}}/styles/atom-one-dark.min.css", | ||
"{{site.katex}}/katex.min.js", | ||
"{{site.katex}}/katex.min.css", | ||
"{{site.cdn}}/quill.snow.css", | ||
"{{site.cdn}}/quill.bubble.css", | ||
"{{site.cdn}}/quill.js" | ||
] | ||
} |