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

@thomas.gafner/prosemirror-selection-markers

v0.2.0

Published

Functions that help to keep track of the selection when writing composite changes in one transformation for a ProseMirror editor.

Downloads

1

Readme

prosemirror-selection-markers

[ CHANGELOG ]

This is a utility module for writing transformations for ProseMirror.

This module exports schema elements and helper functions being useful when keeping track of the selection while creating composite change transformations for a ProseMirror editor.

Never ever use the schema node types provided by this module in your actual documents, that get persisted or shared in collaboration.

If it is ok for you to implement composite changes to your document as a series of commands or one command with many transformations (and many steps), then you most probably cannot benefit from this module.

Use this module when writing composite transformations preferably consisting of only one replace step.

Motivation

If you want to implement composite changes in one atomic transformation (just one big replace step) this module can help you. You just implement the changes of the document and you need not care for the selection (cursor or head and anchor). The selection neither gets lost nor is it put at some random position nor simply at the end of the changed part of your document nor is it reduced to a simple cursor. Instead the selection remains expanded and at the most sensible position within the newly changed document.

When big parts of the document change, writing code to calculate what happens to the selection is tedious and error-prone. Using this module you can avoid that. When writing code of a composite transformation, you just write code for the document change and you need not calculate what happens to the selection.

How it is done

The idea is:

  1. At the beginning of the changes the selection is put to the document as two nodes. One for the head and one for the anchor or just one if the selection is a simple cursor.
  2. You implement the changes as one fragment representing how that part of the document should be after the changes. The selection nodes get moved arround by the changes like single characters do.
  3. At the end of the changes the selection nodes get removed and the actual selection is set to their positions.

Since all this happens within one transformation step and on a version of the document, that is not attached to the state, the selection marker nodes cannot make it to the actual document. The selection nodes actually get added and removed again before the one and only replace step is done on the states transaction. So the markers never appear in any transaction history nor get persisted nor shared to collaborators of the same document.

Example

Let's write a composite transformation.

Extend the schema by the marker node types

When setting up your schema you need to add the marker node types.

import {addSelectionMarkersNodes} from "prosemirror-selection-markers"

const mySchema = new Schema({
  nodes: addSelectionMarkersNodes(baseSchema.spec.nodes, "inline"),
  marks: baseSchema.spec.marks
})

Write your transformations

When writing your transformations use insertSelectionMarkers and replaceWithAndSetSelection.

import {Fragment} from "prosemirror-model"
import {Transform} from "prosemirror-transform"
import {insertSelectionMarkers, replaceWithAndSetSelection} from "prosemirror-selection-markers"

function doComplicatedTransform(tr, doc, selection) {

	({doc, selection} = insertSelectionMarkers(doc, selection));

	let from, to, fragment;

	// Expand the selection to the actual range (from, to) that contains all changes.
	// Set up a fragment as replacement for that range consisting of changing and remaining nodes.
	// The code here might contain the following expressions:
	// .. = doc.slice(.., ..).content ..
	// .. = Fragment.from(doc.type.schema.nodes.my_node.create(null, ..))

	tr = replaceWithAndSetSelection(tr, fragment, from, to);

	return tr;
}

License

This code is released under an MIT license.