npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

y-draft-js

v1.8.0

Published

Draft-js editor bindings for Yjs

Downloads

76

Readme

y-draft-js

Draft.js editor bindings for Yjs, a modern, fast, and powerful JavaScript library for collaborative editing.

Awareness has saved SelectionState.

usage

  1. install
npm install y-draft-js
  1. bindYdoc
const ydoc = new Y.Doc();

const provider = new WebsocketProvider(
  'ws://localhost:1234', // your websockt url
  id,
  ydoc,
  {
    connect: false,
  }
);
  1. bind Draft editor & connect to ydoc
import { DraftBinding } from 'y-draft-js';
const draftBind = new DraftBinding({
  ydoc,
  rawPath: 'raw', // your draft-js raw path(or [], exp: ['content', 'raw'])
  editor: editorRef.current, // draft-js editor ref , or plugin editor ref, or null(You can bind the editor asynchronously, exp: draftBind.bindEditor(editorRef.current))
  provider,
});
provider.connect();
  1. destroy the binding on departure
draftBind.destroy();

awareness

All clients are stored in the provider.awareness.

provider.awareness.getStates().forEach(state => {
  console.log(state.selection); // selection state by all clients
});

undoManager

draftBind.undo and draftBind.redo can be used to undo and redo. if you want to jump to the specified step, you can set allowUndo = false in editorState

const newEditorState = EditorState.push(
  editorState,
  newContentState,
  'insert-characters'
);
newEditorState.allowUndo = false; // not allow undo [y-draft-js]
this.setState({ editorState: newEditorState });

exp:


handleKeyCommand = (command, editorState) => {
  if(command === 'y-draft-undo') {
    this.draftBind?.undo();
    return 'handled';
  }
  if(command === 'y-draft-redo') {
    this.draftBind?.redo();
    return 'handled';
  }
  const newState = RichUtils.handleKeyCommand(editorState, command);
  if (newState) {
    this._onChange(newState);
    return true;
  }
  return false;
};

myKeyBindingFn(e) {
  const cmd = getDefaultKeyBinding(e);
  if (cmd === 'undo') return 'y-draft-undo';
  if (cmd === 'redo') return 'y-draft-redo';
  if (cmd) return cmd;
}
<Editor
  editorState={editorState}
  handleKeyCommand={this.handleKeyCommand}
  keyBindingFn={this.myKeyBindingFn}
  onChange={this._onChange}
  ref={ref => (this.editorRef = ref)}
/>

API

Because raw is not suitable for diff calculation save, so it did a layer of RAW conversion, conversion methods will be introduced

  1. getRawBySharedData
const raw = getRawBySharedData(path, ymap); // get raw data by ymap path
  1. toRawSharedData
ymap.set('content', toRawSharedData(raw)); // set raw data to ymap path
  1. getTargetByPath
const target = getTargetByPath(path, ymap); // get target by ymap path
  1. onTargetSync
const cancel = onTargetSync(path, ydoc, callback); // The callback is triggered when the listening target has a value or target is replaced (This is useful when you are not sure if the data under the destination path exists)
// cancel the listener when component unmount
cancel();
  1. toSyncElement
ymap.set('content', toSyncElement(data)); // set data to ydoc (Depth conversion of this data,if the data contains raw, it will be converted to ydoc raw)

develop

  1. start websoket-server
npm run start:server
  1. start demo
npm run dev