Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
use-prosemirror
Advanced tools
ProseMirror + React done right
ProseMirror is one of the best rich text editors out there. Although it's not written in React, its render model is very similar to React's. It is therefore a great fit for your React project.
This package lets you bootstrap a minimal React integration quickly, using modern React best practices.
node install --save use-prosemirror
or
yarn add use-prosemirror
This package specifies React and ProseMirror as peer dependencies, so makes sure you have them installed, too.
This is all you need to get started:
import 'prosemirror-view/style/prosemirror.css';
import React from 'react';
import {schema} from 'prosemirror-schema-basic';
import {useProseMirror, ProseMirror} from 'use-prosemirror';
return function MyEditor() {
const [state, setState] = useProseMirror({schema});
return <ProseMirror state={state} onChange={setState} />;
};
useProseMirror(config)
This hook maintains editor state. It accepts one argument: the
same object as
EditorState.create
.
It follows the same convention as React's useState
and creates
the initial state and returns it along with an update function.
<ProseMirror />
This component wraps ProseMirror's EditorView
. It displays the
editor state provided by useProseMirror
and dispaches state
updates using the update function. It accepts the following props:
state
— the EditorState
created by useProseMirror
.onChange
— the update function returned by useProseMirror
.style
— (optional) a React style object to pass to the div
containing ProseMirror.className
— (optional) a string of classes you want to pass to the div
containing ProseMirror.It also accepts any
EditorProps
.
So you can, for example, set
transformPastedHTML
directly on the component:
<ProseMirror
state={props.state}
onChange={props.onChange}
transformPastedHTML={string => {
console.log('transformPastedHTML', string);
return string;
}}
/>
If you pass a ref
, <ProseMirror />
exposes a view
getter to retrieve the underlying EditorView
instance:
const [state, setState] = useProseMirror({schema});
const viewRef = useRef();
return (
<ProseMirror
ref={viewRef}
state={state}
onChange={state => {
setState(state);
console.log(viewRef.current.view.hasFocus());
}}
/>
);
React and ProseMirror follow the same data flow model. Even though ProseMirror is not written in React, it fits nicely into a React project.
From the ProseMirror documentation:
So the editor view displays a given editor state, and when something happens, it creates a transaction and broadcasts this. This transaction is then, typically, used to create a new state, which is given to the view using its updateState method.
This creates a straightforward, cyclic data flow, as opposed to the classic approach (in the JavaScript world) of a host of imperative event handlers, which tends to create a much more complex web of data flows.
Sound familiar? It continues:
It is possible to ‘intercept’ transactions as they are dispatched with the dispatchTransaction prop, in order to wire this cyclic data flow into a larger cycle—if your whole app is using a data flow model like this, as with Redux and similar architectures, you can integrate ProseMirror's transactions in your main action-dispatching cycle, and keep ProseMirror's state in your application ‘store’.
Which is exactly what this module does.
FAQs
ProseMirror for React
The npm package use-prosemirror receives a total of 4,215 weekly downloads. As such, use-prosemirror popularity was classified as popular.
We found that use-prosemirror demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.