Class: Y::ProseMirror

Inherits:
Object
  • Object
show all
Defined in:
lib/y/rendering.rb

Direct Known Subclasses

Tiptap

Instance Method Summary collapse

Constructor Details

#initialize(doc, nodes: {}, marks: {}) {|builder| ... } ⇒ ProseMirror

Y::ProseMirror.new(doc, nodes: {...}, marks: {...}) — see Y::RenderRules for the rule forms. This is core ProseMirror only: prosemirror-schema-basic plus the prosemirror-tables family, and the full mark set (marks are native — see rules.mark for overrides). Editor-specific nodes arrive as rules — Y::Tiptap subclasses this with Tiptap's extension nodes; a different ProseMirror editor brings its own rule set the same way.

Yields:

  • (builder)


254
255
256
257
258
259
260
261
# File 'lib/y/rendering.rb', line 254

def initialize(doc, nodes: {}, marks: {})
  builder = RenderRules::Builder.new(marks_allowed: true)
  yield builder if block_given?
  nodes = nodes.transform_keys(&:to_s).merge(builder.nodes)
  marks = marks.transform_keys(&:to_s).merge(builder.marks)
  rules_json, @render_callbacks = RenderRules.compile(nodes, marks)
  @native = NativeProseMirror.new(doc, rules_json)
end

Instance Method Details

#node_types(root = nil) ⇒ Object

What node types this document actually contains — the discovery aid for writing rules. Facts per type: "count", "attrs" (names as stored), "children" (child node types), "text" (whether it holds text runs), and "handled" ("builtin", "rule", or nil — nil marks the types you still need a rule for). Children plus text is how you pick contains:.



275
276
277
278
# File 'lib/y/rendering.rb', line 275

def node_types(root = nil)
  json = root.nil? ? @native.node_types : @native.node_types(root)
  json && JSON.parse(json)
end

#to_html(root = nil) ⇒ Object



263
264
265
266
267
268
# File 'lib/y/rendering.rb', line 263

def to_html(root = nil)
  result = root.nil? ? @native.to_html : @native.to_html(root)
  return result unless result.is_a?(Array)

  RenderRules.splice(result, @render_callbacks)
end