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

@braidjs/dom-diff

v0.0.20

Published

utilities for diffing html doms

Downloads

195

Readme

DOM Diffing and Synchronization over Braid-HTTP

Note: Currently buggy! WIP.

Efficiently diff and synchronize a DOM, either locally or over the network via Braid-HTTP. Sync multiple browser & server DOMs over the network, via Dom Diffs. Collaboratively edit live HTML. Create mashups of UIs, interoperating via Dom Diffs.

  • Supports Braid-HTTP 04 protocol
  • Uses Tree-sitter for robust, incremental HTML parsing
  • Implements DOM diffing algorithm over Tree-sitter
  • Run it on server or client
  • Developed as part of the braid.org project

This library makes it easy to add collaborative HTML editing to your web applications, allowing multiple users to edit the same DOM structure in real-time.

General Use on Server

Install in your project:

npm install @braidjs/dom-diff

Require the library, which adds the serve_dom_diff request handler to the global namespace, and use it to handle HTTP requests:

require('@braidjs/dom-diff')

http_server.on("request", (req, res) => {
  // Your server logic...

  // Serve DOM diffing for this request/response:
  serve_dom_diff(req, res, (subscribe) => {

    // The DOM diffing needs to subscribe to an underlying textual representation
    require("braid-text").get(req.url, {subscribe})
  })
})

serve_dom_diff first subscribes to an underlying textual resource using the provided callback. It then responds to the request with a Braid subscription using the simpleton merge type for HTML, which sends replacement HTML for a given xpath. When changes occur in the underlying textual resource, serve_dom_diff calculates the relevant DOM changes and sends these patches to all subscribed clients.

Server API

serve_dom_diff(req, res, braid_text_cb, options)

  • req: Incoming HTTP request object.
  • res: Outgoing HTTP response object.
  • braid_text_cb: Function called by serve_dom_diff to subscribe to the underlying textual resource. It receives a callback function with parameters like ({ version, parents, body, patches }) to be called for each new Braid version.
  • options: (optional) An object containing additional options:
    • key: (optional) ID of HTML resource to sync with. Defaults to req.url.
  • This method handles Braid-HTTP requests for collaborative DOM editing.

Client-side Usage

Include the library in your client-side JavaScript.

<script src="https://unpkg.com/@braidjs/dom-diff"></script>

Once included, you can use the DOM diffing functionality in your client-side code:

// Initialize DOM diffing
await init_dom_diff()

// Create a DOM diff object
let dd = create_dom_diff(initialHtml)

// Get current HTML
let currentHtml = dd.get()

// Apply a patch and get the diff
let diff = dd.patch(newHtml)

// Perhaps send diff over network
// Here's an example diff:
[{ range: `/*[1]/*[2:2]`, content: '<b>hi</b>' },
 { range: `/*[2:2]`, content: 'world' }]

// Apply DOM diff to actual DOM
apply_dom_diff(domElement, diff)

Client API

async init_dom_diff()

  • Initializes the DOM diffing system, loading necessary dependencies.

create_dom_diff(html)

  • Creates a new DOM diff object for the given html.
  • Returns an object with methods:
    • get(): Returns the current HTML.
    • patch(newHtml): Updates to the newHtml and returns the DOM diff to get there.

apply_dom_diff(dom, diff)

  • Applies a diff to the given DOM element.
  • dom: The target DOM element to update.
  • diff: The diff object generated by patch().