• Stars
    star
    404
  • Rank 103,054 (Top 3 %)
  • Language
    TypeScript
  • License
    Other
  • Created about 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⚒ Utils library for ProseMirror

Utils library for ProseMirror

npm License Github Issues CircleCI codecov Downloads Code size

Quick Start

Install prosemirror-utils package from npm:

npm install prosemirror-utils

Public API documentation

Utils for working with selection

  • findParentNode(predicate: fn(node: ProseMirrorNode) → boolean) → fn(selection: Selection) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode}
    Iterates over parent nodes, returning the closest node and its start position predicate returns truthy for. start points to the start position of the node, pos points directly before the node.

    const predicate = node => node.type === schema.nodes.blockquote;
    const parent = findParentNode(predicate)(selection);
  • findParentNodeClosestToPos($pos: ResolvedPos, predicate: fn(node: ProseMirrorNode) → boolean) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode}
    Iterates over parent nodes starting from the given $pos, returning the closest node and its start position predicate returns truthy for. start points to the start position of the node, pos points directly before the node.

    const predicate = node => node.type === schema.nodes.blockquote;
    const parent = findParentNodeClosestToPos(state.doc.resolve(5), predicate);
  • findParentDomRef(predicate: fn(node: ProseMirrorNode) → boolean, domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → fn(selection: Selection) → ?dom.Node
    Iterates over parent nodes, returning DOM reference of the closest node predicate returns truthy for.

    const domAtPos = view.domAtPos.bind(view);
    const predicate = node => node.type === schema.nodes.table;
    const parent = findParentDomRef(predicate, domAtPos)(selection); // <table>
  • hasParentNode(predicate: fn(node: ProseMirrorNode) → boolean) → fn(selection: Selection) → boolean
    Checks if there's a parent node predicate returns truthy for.

    if (hasParentNode(node => node.type === schema.nodes.table)(selection)) {
      // ....
    }
  • findParentNodeOfType(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode}
    Iterates over parent nodes, returning closest node of a given nodeType. start points to the start position of the node, pos points directly before the node.

    const parent = findParentNodeOfType(schema.nodes.paragraph)(selection);
  • findParentNodeOfTypeClosestToPos($pos: ResolvedPos, nodeType: NodeType | [NodeType]) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode}
    Iterates over parent nodes starting from the given $pos, returning closest node of a given nodeType. start points to the start position of the node, pos points directly before the node.

    const parent = findParentNodeOfTypeClosestToPos(state.doc.resolve(10), schema.nodes.paragraph);
  • hasParentNodeOfType(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → boolean
    Checks if there's a parent node of a given nodeType.

    if (hasParentNodeOfType(schema.nodes.table)(selection)) {
      // ....
    }
  • findParentDomRefOfType(nodeType: NodeType | [NodeType], domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → fn(selection: Selection) → ?dom.Node
    Iterates over parent nodes, returning DOM reference of the closest node of a given nodeType.

    const domAtPos = view.domAtPos.bind(view);
    const parent = findParentDomRefOfType(schema.nodes.codeBlock, domAtPos)(selection); // <pre>
  • findSelectedNodeOfType(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode}
    Returns a node of a given nodeType if it is selected. start points to the start position of the node, pos points directly before the node.

    const { extension, inlineExtension, bodiedExtension } = schema.nodes;
    const selectedNode = findSelectedNodeOfType([
      extension,
      inlineExtension,
      bodiedExtension,
    ])(selection);
  • isNodeSelection(selection: Selection) → boolean
    Checks if current selection is a NodeSelection.

    if (isNodeSelection(tr.selection)) {
      // ...
    }
  • findPositionOfNodeBefore(selection: Selection) → ?number
    Returns position of the previous node.

    const pos = findPositionOfNodeBefore(tr.selection);
  • findDomRefAtPos(position: number, domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → dom.Node
    Returns DOM reference of a node at a given position. If the node type is of type TEXT_NODE it will return the reference of the parent node.

    const domAtPos = view.domAtPos.bind(view);
    const ref = findDomRefAtPos($from.pos, domAtPos);

Utils for working with ProseMirror node

  • flatten(node: ProseMirrorNode, descend: ?boolean = true) → [{node: ProseMirrorNode, pos: number}]
    Flattens descendants of a given node. It doesn't descend into a node when descend argument is false (defaults to true).

    const children = flatten(node);
  • findChildren(node: ProseMirrorNode, predicate: fn(node: ProseMirrorNode) → boolean, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Iterates over descendants of a given node, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument is false (defaults to true).

    const textNodes = findChildren(node, child => child.isText, false);
  • findTextNodes(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Returns text nodes of a given node. It doesn't descend into a node when descend argument is false (defaults to true).

    const textNodes = findTextNodes(node);
  • findInlineNodes(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Returns inline nodes of a given node. It doesn't descend into a node when descend argument is false (defaults to true).

    const inlineNodes = findInlineNodes(node);
  • findBlockNodes(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Returns block descendants of a given node. It doesn't descend into a node when descend argument is false (defaults to true).

    const blockNodes = findBlockNodes(node);
  • findChildrenByAttr(node: ProseMirrorNode, predicate: fn(attrs: ?Object) → boolean, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Iterates over descendants of a given node, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument is false (defaults to true).

    const mergedCells = findChildrenByAttr(table, attrs => attrs.colspan === 2);
  • findChildrenByType(node: ProseMirrorNode, nodeType: NodeType, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Iterates over descendants of a given node, returning child nodes of a given nodeType. It doesn't descend into a node when descend argument is false (defaults to true).

    const cells = findChildrenByType(table, schema.nodes.tableCell);
  • findChildrenByMark(node: ProseMirrorNode, markType: markType, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
    Iterates over descendants of a given node, returning child nodes that have a mark of a given markType. It doesn't descend into a node when descend argument is false (defaults to true).

    const nodes = findChildrenByMark(state.doc, schema.marks.strong);
  • contains(node: ProseMirrorNode, nodeType: NodeType) → boolean
    Returns true if a given node contains nodes of a given nodeType

    if (contains(panel, schema.nodes.listItem)) {
      // ...
    }

Utils for document transformation

  • removeParentNodeOfType(nodeType: NodeType | [NodeType]) → fn(tr: Transaction) → Transaction
    Returns a new transaction that removes a node of a given nodeType. It will return an original transaction if parent node hasn't been found.

    dispatch(
      removeParentNodeOfType(schema.nodes.table)(tr)
    );
  • replaceParentNodeOfType(nodeType: NodeType | [NodeType], content: ProseMirrorNode | Fragment) → fn(tr: Transaction) → Transaction
    Returns a new transaction that replaces parent node of a given nodeType with the given content. It will return an original transaction if either parent node hasn't been found or replacing is not possible.

    const node = schema.nodes.paragraph.createChecked({}, schema.text('new'));
    
    dispatch(
     replaceParentNodeOfType(schema.nodes.table, node)(tr)
    );
  • removeSelectedNode(tr: Transaction) → Transaction
    Returns a new transaction that removes selected node. It will return an original transaction if current selection is not a NodeSelection.

    dispatch(
      removeSelectedNode(tr)
    );
  • replaceSelectedNode(content: ProseMirrorNode | ProseMirrorFragment) → fn(tr: Transaction) → Transaction
    Returns a new transaction that replaces selected node with a given node, keeping NodeSelection on the new node. It will return the original transaction if either current selection is not a NodeSelection or replacing is not possible.

    const node = schema.nodes.paragraph.createChecked({}, schema.text('new'));
    dispatch(
      replaceSelectedNode(node)(tr)
    );
  • canInsert($pos: ResolvedPos, content: ProseMirrorNode | Fragment) → boolean
    Checks if a given content can be inserted at the given $pos

    const { selection: { $from } } = state;
    const node = state.schema.nodes.atom.createChecked();
    if (canInsert($from, node)) {
      // ...
    }
  • safeInsert(content: ProseMirrorNode | Fragment, position: ?number, tryToReplace: ?boolean) → fn(tr: Transaction) → Transaction
    Returns a new transaction that inserts a given content at the current cursor position, or at a given position, if it is allowed by schema. If schema restricts such nesting, it will try to find an appropriate place for a given node in the document, looping through parent nodes up until the root document node. If tryToReplace is true and current selection is a NodeSelection, it will replace selected node with inserted content if its allowed by schema. If cursor is inside of an empty paragraph, it will try to replace that paragraph with the given content. If insertion is successful and inserted node has content, it will set cursor inside of that content. It will return an original transaction if the place for insertion hasn't been found.

    const node = schema.nodes.extension.createChecked({});
    dispatch(
      safeInsert(node)(tr)
    );
  • setParentNodeMarkup(nodeType: NodeType | [NodeType], type: ?NodeType | null, attrs: ?Object | null, marks: ?[Mark]) → fn(tr: Transaction) → Transaction
    Returns a transaction that changes the type, attributes, and/or marks of the parent node of a given nodeType.

    const node = schema.nodes.extension.createChecked({});
    dispatch(
      setParentNodeMarkup(schema.nodes.panel, null, { panelType })(tr);
    );
  • selectParentNodeOfType(nodeType: NodeType | [NodeType]) → fn(tr: Transaction) → Transaction
    Returns a new transaction that sets a NodeSelection on a parent node of a given nodeType.

    dispatch(
      selectParentNodeOfType([tableCell, tableHeader])(state.tr)
    );
  • removeNodeBefore(tr: Transaction) → Transaction
    Returns a new transaction that deletes previous node.

    dispatch(
      removeNodeBefore(state.tr)
    );
  • setTextSelection(position: number, dir: ?number = 1) → fn(tr: Transaction) → Transaction
    Returns a new transaction that tries to find a valid cursor selection starting at the given position and searching back if dir is negative, and forward if positive. If a valid cursor position hasn't been found, it will return the original transaction.

    dispatch(
      setTextSelection(5)(tr)
    );

License

More Repositories

1

react-beautiful-dnd

Beautiful and accessible drag and drop for lists with React
JavaScript
32,454
star
2

jest-in-case

Jest utility for creating variations of the same test
JavaScript
1,041
star
3

react-sweet-state

Shared state management solution for React
JavaScript
850
star
4

escalator

Escalator is a batch or job optimized horizontal autoscaler for Kubernetes
Go
636
star
5

github-for-jira

Connect your code with your project management in Jira
TypeScript
580
star
6

docker-chromium-xvfb

Docker image for running browser tests against headless Chromium
Dockerfile
385
star
7

nucleus

A configurable and versatile update server for all your Electron apps
TypeScript
379
star
8

gostatsd

An implementation of Etsy's statsd in Go with tags support
Go
372
star
9

smith

Smith is a Kubernetes workflow engine / resource manager
Go
287
star
10

babel-plugin-react-flow-props-to-prop-types

Convert Flow React props annotation to PropTypes
JavaScript
234
star
11

better-ajv-errors

JSON Schema validation for Human 👨‍🎤
JavaScript
222
star
12

browser-interaction-time

⏰ A JavaScript library (written in TypeScript) to measure the time a user is active on a website
TypeScript
208
star
13

gajira

GitHub Actions for Jira
191
star
14

extract-react-types

One stop shop for documenting your react components.
JavaScript
176
star
15

stricter

A project-wide js-linting tool
TypeScript
157
star
16

data-center-helm-charts

Helm charts for Atlassian's Data Center products
Java
148
star
17

bazel-tools

Reusable bits for Bazel
Starlark
112
star
18

terraform-provider-artifactory

Terraform provider to manage Artifactory
Go
89
star
19

gajira-login

Jira Login GitHub Action
JavaScript
87
star
20

build-stats

🏆 get the build stats for pipelines 🏆
TypeScript
79
star
21

kubetoken

Kubetoken
Go
74
star
22

dc-app-performance-toolkit

Atlassian Data Center App Performance Toolkit
Python
71
star
23

koa-oas3

Request and response validator for Koa using Open API Specification
TypeScript
69
star
24

1time

Lightweight, thread-safe Java/Kotlin TOTP (time-based one-time passwords) and HOTP generator and validator for multi-factor authentication valid for both prover and verifier based on shared secret
Kotlin
64
star
25

gajira-transition

JavaScript
57
star
26

sketch-plugin

Design your next Atlassian app with our component libraries and suite of Sketch tools 💎
JavaScript
57
star
27

gajira-create

JavaScript
54
star
28

go-sentry-api

A go client for the sentry api https://sentry.io/api/
Go
50
star
29

themis

Autoscaling EMR clusters and Kinesis streams on Amazon Web Services (AWS)
JavaScript
48
star
30

gajira-todo

JavaScript
47
star
31

jira-cloud-for-sketch

A Sketch plugin providing integration with JIRA Cloud
JavaScript
44
star
32

gajira-find-issue-key

JavaScript
43
star
33

oas3-chow-chow

Request and response validator against OpenAPI Specification 3
TypeScript
39
star
34

validate-npm-package

Validate a package.json file
JavaScript
38
star
35

gajira-cli

JavaScript
37
star
36

conartist

Scaffold out and keep all your files in sync over time. Code-shifts for your file system.
JavaScript
34
star
37

jira-github-connector-plugin

This project has been superseded by the JIRA DVCS Connector
JavaScript
30
star
38

voyager

Voyager PaaS
Go
30
star
39

gajira-comment

JavaScript
30
star
40

atlaskit-framerx

[Unofficial] Atlaskit for Framer X (experimental)
TypeScript
28
star
41

jira-actions

Kotlin
27
star
42

sourcemap

Java
24
star
43

go-artifactory

Go library for artifactory REST API
Go
22
star
44

asap-authentication-python

This package provides a python implementation of the Atlassian Service to Service Authentication specification.
Python
21
star
45

homebrew-tap

This repository contains a collection of Homebrew (aka, Brew) "formulae" for Atlassian
Ruby
16
star
46

atlassian-connect-example-app-node

TypeScript
15
star
47

vscode-extension-jira-frontend

JavaScript
15
star
48

ssh

Kotlin
14
star
49

jira-performance-tests

Kotlin
14
star
50

docker-fluentd

Docker image for fluentd with support for both elasticsearch and kinesis
Makefile
11
star
51

omniauth-jira

OmniAuth strategy for JIRA
Ruby
11
star
52

redis-dump-restore

Node.js library to dump and restore Redis.
JavaScript
10
star
53

jenkins-for-jira

Connect your Jenkins server to Jira Software Cloud for more visibility into your development pipeline
TypeScript
10
star
54

fluent-plugin-kinesis-aggregation

fluent kinesis plugin shipping KPL aggregation format records, based on https://github.com/awslabs/aws-fluent-plugin-kinesis
Ruby
10
star
55

infrastructure

Kotlin
9
star
56

hubot-stride

JavaScript
9
star
57

graphql-braid

9
star
58

copy-pkg

Copy a package.json with filters and normalization
JavaScript
8
star
59

rocker

Little text UI for docker
Rust
8
star
60

aws-infrastructure

Kotlin
8
star
61

gray-matter-loader

Webpack loader for extracting front-matter using gray-matter - https://www.npmjs.com/package/gray-matter
JavaScript
8
star
62

autoconvert

TinyMCE plugin for Atlassian Autoconvert
JavaScript
8
star
63

jira-hardware-exploration

Kotlin
7
star
64

virtual-users

Kotlin
7
star
65

less-plugin-inline-svg

A Less plugin that allows to inline SVG file and customize its CSS styles
JavaScript
6
star
66

report

HTML
6
star
67

docker-infrastructure

Kotlin
6
star
68

jobsite

Tools for working with workspaces as defined by Yarn, Lerna, Bolt, etc.
JavaScript
5
star
69

git-lob

Experimental large files in Git (discontinued, use git-lfs instead)
Go
5
star
70

ansible-ixgbevf

4
star
71

concurrency

Kotlin
4
star
72

jvm-tasks

Kotlin
4
star
73

jpt-example-btf

Java
3
star
74

gojiid

A Goji Middleware For adding Request Id to Context
Go
3
star
75

workspace

Kotlin
3
star
76

jsm-integration-scripts

Jira Service Management Integration Scripts
Python
3
star
77

ssh-ubuntu

Kotlin
3
star
78

jira-software-actions

Kotlin
3
star
79

atlassian-connect-example-app-python

Python
2
star
80

atlassian-connect-example-app-java

Java
2
star
81

nadel-graphql-gateway-demo

Nadel GraphQL Gateway Demo app
HTML
1
star
82

homebrew-bitbucket

A collection of pinned versions of dependencies for Bitbucket
Ruby
1
star
83

parcel-stress-test

JavaScript
1
star
84

putty-sourcetree-fork

A fork of PuTTY used by Sourcetree
C
1
star
85

frontend-guides

1
star
86

tangerine-state-viewer

Visual Studio Code extension to facilitate tangerine state navigation
TypeScript
1
star
87

uniql-es

JavaScript
1
star
88

jasmine-http-server-spy

Creates jasmine spy objects backed by a http server.
CoffeeScript
1
star
89

packit-cli

CLI tool for creating package based architecture for enterprise frontend applications.
JavaScript
1
star
90

fluent-plugin-statsd_event

Fluentd plugin for sendind events to a statsd service
Ruby
1
star
91

github-packages-test

Test repo to verify artifact delivery pipeline
Kotlin
1
star
92

org.eclipse.jgit-atlassian

Java
1
star
93

jces-1209

Benchmark for Cloud and DC
Kotlin
1
star
94

webvieweventtest

TypeScript
1
star
95

quick-303

Cloud vs DC
Kotlin
1
star