All Projects → atlassian → Prosemirror Utils

atlassian / Prosemirror Utils

Licence: other
⚒ Utils library for ProseMirror

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Prosemirror Utils

react-semantic-render
Semantic helper components for rendering content with React.
Stars: ✭ 13 (-94.61%)
Mutual labels:  utils, helpers
urley
📦 An easy cross-platform utility library to work with URLs in Javascript.
Stars: ✭ 14 (-94.19%)
Mutual labels:  utils, helpers
utils
⚡ A collection of common functions but with better performance, less allocations and less dependencies created for Fiber.
Stars: ✭ 21 (-91.29%)
Mutual labels:  utils, helpers
Smart-Inspector
Fluent re-take on Unity Inspector UX. Packed with QoL improvements.
Stars: ✭ 680 (+182.16%)
Mutual labels:  utils, helpers
sagittarius
🎯 A set of javascript most used utils📑
Stars: ✭ 42 (-82.57%)
Mutual labels:  utils, helpers
Rails stuff
Collection of useful modules for Rails.
Stars: ✭ 110 (-54.36%)
Mutual labels:  utils, helpers
Rambdax
Extended version of Rambda
Stars: ✭ 148 (-38.59%)
Mutual labels:  utils
Torchfunc
PyTorch functions and utilities to make your life easier
Stars: ✭ 177 (-26.56%)
Mutual labels:  utils
Touch Bar Istats
Show CPU/GPU/MEM temperature on Touch Bar with BetterTouchTool!
Stars: ✭ 141 (-41.49%)
Mutual labels:  utils
Note
常规Java工具,算法,加密,数据库,面试题,源代码分析,解决方案
Stars: ✭ 1,846 (+665.98%)
Mutual labels:  utils
Bbo
bbo is a utility library of zero dependencies for javascript. 🍖🌭🍔
Stars: ✭ 227 (-5.81%)
Mutual labels:  utils
Jsonapi Utils
Build JSON API-compliant APIs on Rails with no (or less) learning curve.
Stars: ✭ 191 (-20.75%)
Mutual labels:  utils
Handlebars Helpers
Related projects
Stars: ✭ 2,024 (+739.83%)
Mutual labels:  helpers
Pine Utils
Code Snippets + Tricks & Tips to help Pine Script developers
Stars: ✭ 149 (-38.17%)
Mutual labels:  utils
Vtils
一个面向业务的 JavaScript/TypeScript 实用程序库。支持在浏览器、Node.js、小程序、Taro、Deno 下使用。
Stars: ✭ 177 (-26.56%)
Mutual labels:  utils
Rxtool
Android开发人员不得不收集的工具类集合 | 支付宝支付 | 微信支付(统一下单) | 微信分享 | Zip4j压缩(支持分卷压缩与加密) | 一键集成UCrop选择圆形头像 | 一键集成二维码和条形码的扫描与生成 | 常用Dialog | WebView的封装可播放视频 | 仿斗鱼滑动验证码 | Toast封装 | 震动 | GPS | Location定位 | 图片缩放 | Exif 图片添加地理位置信息(经纬度) | 蛛网等级 | 颜色选择器 | ArcGis | VTPK | 编译运行一下说不定会找到惊喜
Stars: ✭ 11,567 (+4699.59%)
Mutual labels:  utils
Goutil
💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more. Go 的一些工具函数,格式化,特殊处理,常用信息获取等等
Stars: ✭ 205 (-14.94%)
Mutual labels:  utils
Ramda Extension
🤘Utility library for functional JavaScript. With ❤️ to Ramda.
Stars: ✭ 139 (-42.32%)
Mutual labels:  utils
Fastandrutils
android快速开发工具类
Stars: ✭ 165 (-31.54%)
Mutual labels:  utils
Ember Native Dom Helpers
Test helpers for your integration tests that fire native events
Stars: ✭ 187 (-22.41%)
Mutual labels:  helpers

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].