-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2962 from navikt/poc-quill-konvertere-delmalblokk…
…-til-fritekst Konvertere delmalblokk til rikt tekstfelt
- Loading branch information
Showing
9 changed files
with
256 additions
and
21 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 |
---|---|---|
|
@@ -20,7 +20,10 @@ | |
"set-node-options": "NODE_OPTIONS='--import=./node_dist/backend/register.js --es-module-specifier-resolution=node'" | ||
}, | ||
"lint-staged": { | ||
"src/**/*.{js,jsx,ts,tsx,json,css}": ["prettier --write", "eslint --fix --max-warnings=0"] | ||
"src/**/*.{js,jsx,ts,tsx,json,css}": [ | ||
"prettier --write", | ||
"eslint --fix --max-warnings=0" | ||
] | ||
}, | ||
"dependencyComments": { | ||
"color-string": "Overrider versjon som brukes av winston, som kommer fra familie-frontend-backend", | ||
|
@@ -64,6 +67,7 @@ | |
"passport": "^0.7.0", | ||
"passport-azure-ad": "^4.3.5", | ||
"prom-client": "^15.1.3", | ||
"quill": "^2.0.3", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"react-pdf": "9.1.1", | ||
|
@@ -124,5 +128,6 @@ | |
"resolutions": { | ||
"@types/react": "^18.x", | ||
"@types/react-select": "^5.x" | ||
} | ||
}, | ||
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import Quill from 'quill'; | ||
import React, { forwardRef, useEffect, useLayoutEffect, useRef, useState } from 'react'; | ||
import 'quill/dist/quill.snow.css'; | ||
import { BlockBlot } from 'parchment'; | ||
import { AlertError } from '../Visningskomponenter/Alerts'; | ||
|
||
type Props = { | ||
defaultValue?: string; | ||
onTextChange: (html: string, renTekst: string) => void; | ||
}; | ||
|
||
/** | ||
* Overstyre default tag fra `p` til `div` ettersom `p` gir noen uheldige sideeffekter med mellomrom i tekstene våre. | ||
*/ | ||
const Block = Quill.import('blots/block') as BlockBlot; | ||
// @ts-expect-error Utypet kode - usikkert hvordan vi får dette til | ||
Block.tagName = 'div'; | ||
// @ts-expect-error Utypet kode - usikkert hvordan vi får dette til | ||
Quill.register(Block); | ||
|
||
export const HtmlEditor = forwardRef(({ defaultValue, onTextChange }: Props, ref) => { | ||
const defaultValueRef = useRef(defaultValue); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
const onTextChangeRef = useRef(onTextChange); | ||
const [feilVedOpprettelse, settFeilVedOpprettelse] = useState<boolean>(false); | ||
useLayoutEffect(() => { | ||
onTextChangeRef.current = onTextChange; | ||
}); | ||
|
||
useEffect(() => { | ||
const container = containerRef.current; | ||
|
||
if (!container) { | ||
settFeilVedOpprettelse(true); | ||
return; | ||
} | ||
|
||
settFeilVedOpprettelse(false); | ||
const editorContainer = container.appendChild(container.ownerDocument.createElement('div')); | ||
|
||
const verktøySomSkalMed = [[{ list: 'bullet' }], ['clean']]; | ||
|
||
const quill = new Quill(editorContainer, { | ||
theme: 'snow', | ||
modules: { | ||
toolbar: verktøySomSkalMed, | ||
}, | ||
}); | ||
|
||
if (ref && typeof ref === 'function') { | ||
ref(quill); | ||
} else if (ref && 'current' in ref) { | ||
(ref as React.MutableRefObject<Quill | null>).current = quill; | ||
} | ||
|
||
if (defaultValueRef.current) { | ||
const htmlAsDelta = quill.clipboard.convert({ html: defaultValueRef.current }); | ||
quill.setContents(htmlAsDelta); | ||
} | ||
|
||
quill.on('text-change', () => { | ||
const html = quill.root.innerHTML; | ||
const text = quill.getText(); | ||
|
||
onTextChangeRef.current(html, text); | ||
}); | ||
|
||
return () => { | ||
if (ref && 'current' in ref) { | ||
(ref as React.MutableRefObject<Quill | null>).current = null; | ||
} | ||
container.innerHTML = ''; | ||
}; | ||
}, [ref]); | ||
|
||
return feilVedOpprettelse ? ( | ||
<AlertError>En uventet feil oppstod ved opprettelse av tekstfelt.</AlertError> | ||
) : ( | ||
<div ref={containerRef}></div> | ||
); | ||
}); | ||
|
||
HtmlEditor.displayName = 'HtmlEditor'; |
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
Oops, something went wrong.