Class: Prosereflect::Image

Inherits:
Node
  • Object
show all
Defined in:
lib/prosereflect/image.rb

Overview

Image class represents a ProseMirror image node. It handles image attributes like src, alt, title, dimensions, etc.

Constant Summary collapse

PM_TYPE =
'image'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#find_all, #find_children, #find_first, #marks, #marks=, #parse_content, #process_attrs_data, #raw_marks, #text_content, #to_h, #to_yaml

Constructor Details

#initialize(attributes = {}) ⇒ Image

Returns a new instance of Image.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/prosereflect/image.rb', line 24

def initialize(attributes = {})
  # Images don't have content, they're self-contained
  attributes[:content] = []

  # Extract attributes from the attrs hash if provided
  if attributes[:attrs]
    @src = attributes[:attrs]['src']
    @alt = attributes[:attrs]['alt']
    @title = attributes[:attrs]['title']
    @width = attributes[:attrs]['width']
    @height = attributes[:attrs]['height']
  end

  super
end

Class Method Details

.create(attrs = nil) ⇒ Object



40
41
42
# File 'lib/prosereflect/image.rb', line 40

def self.create(attrs = nil)
  new(attrs: attrs)
end

Instance Method Details

#add_childObject

Override content-related methods since images don’t have content

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/prosereflect/image.rb', line 98

def add_child(*)
  raise NotImplementedError, 'Image nodes cannot have children'
end

#altObject



110
111
112
# File 'lib/prosereflect/image.rb', line 110

def alt
  @alt || attrs&.[]('alt')
end

#alt=(alt_text) ⇒ Object

Update the alt text



52
53
54
55
56
# File 'lib/prosereflect/image.rb', line 52

def alt=(alt_text)
  @alt = alt_text
  self.attrs ||= {}
  attrs['alt'] = alt_text
end

#contentObject



102
103
104
# File 'lib/prosereflect/image.rb', line 102

def content
  []
end

#dimensions=(dimensions) ⇒ Object

Update dimensions (width and height)



80
81
82
83
84
# File 'lib/prosereflect/image.rb', line 80

def dimensions=(dimensions)
  width, height = dimensions
  self.width = width if width
  self.height = height if height
end

#heightObject



122
123
124
# File 'lib/prosereflect/image.rb', line 122

def height
  @height || attrs&.[]('height')
end

#height=(value) ⇒ Object

Update the height



73
74
75
76
77
# File 'lib/prosereflect/image.rb', line 73

def height=(value)
  @height = value
  self.attrs ||= {}
  attrs['height'] = value
end

#image_attributesObject

Get image attributes as a hash



87
88
89
90
91
92
93
94
95
# File 'lib/prosereflect/image.rb', line 87

def image_attributes
  {
    src: src,
    alt: alt,
    title: title,
    width: width,
    height: height
  }.compact
end

#srcObject



106
107
108
# File 'lib/prosereflect/image.rb', line 106

def src
  @src || attrs&.[]('src')
end

#src=(src_url) ⇒ Object

Update the image source URL



45
46
47
48
49
# File 'lib/prosereflect/image.rb', line 45

def src=(src_url)
  @src = src_url
  self.attrs ||= {}
  attrs['src'] = src_url
end

#titleObject



114
115
116
# File 'lib/prosereflect/image.rb', line 114

def title
  @title || attrs&.[]('title')
end

#title=(title_text) ⇒ Object

Update the title (tooltip)



59
60
61
62
63
# File 'lib/prosereflect/image.rb', line 59

def title=(title_text)
  @title = title_text
  self.attrs ||= {}
  attrs['title'] = title_text
end

#widthObject



118
119
120
# File 'lib/prosereflect/image.rb', line 118

def width
  @width || attrs&.[]('width')
end

#width=(value) ⇒ Object

Update the width



66
67
68
69
70
# File 'lib/prosereflect/image.rb', line 66

def width=(value)
  @width = value
  self.attrs ||= {}
  attrs['width'] = value
end