Class: Prosereflect::Image
- 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
-
#add_child ⇒ Object
Override content-related methods since images don’t have content.
- #alt ⇒ Object
-
#alt=(alt_text) ⇒ Object
Update the alt text.
- #content ⇒ Object
-
#dimensions=(dimensions) ⇒ Object
Update dimensions (width and height).
- #height ⇒ Object
-
#height=(value) ⇒ Object
Update the height.
-
#image_attributes ⇒ Object
Get image attributes as a hash.
-
#initialize(attributes = {}) ⇒ Image
constructor
A new instance of Image.
- #src ⇒ Object
-
#src=(src_url) ⇒ Object
Update the image source URL.
- #title ⇒ Object
-
#title=(title_text) ⇒ Object
Update the title (tooltip).
- #to_h ⇒ Object
- #width ⇒ Object
-
#width=(value) ⇒ Object
Update the width.
Methods inherited from Node
#copy, #cut, #descendants, #eq?, #find_all, #find_children, #find_first, #marks, #marks=, #node, #node_size, #nodes_between, #parse_content, #process_attrs_data, #raw_marks, #resolve, #text?, #text_content, #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_child ⇒ Object
Override content-related methods since images don’t have content
107 108 109 |
# File 'lib/prosereflect/image.rb', line 107 def add_child(*) raise NotImplementedError, "Image nodes cannot have children" end |
#alt ⇒ Object
119 120 121 |
# File 'lib/prosereflect/image.rb', line 119 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 |
#content ⇒ Object
111 112 113 |
# File 'lib/prosereflect/image.rb', line 111 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 |
#height ⇒ Object
131 132 133 |
# File 'lib/prosereflect/image.rb', line 131 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_attributes ⇒ Object
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 |
#src ⇒ Object
115 116 117 |
# File 'lib/prosereflect/image.rb', line 115 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 |
#title ⇒ Object
123 124 125 |
# File 'lib/prosereflect/image.rb', line 123 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 |
#to_h ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/prosereflect/image.rb', line 97 def to_h hash = super if hash["attrs"] %w[title width height].each { |k| hash["attrs"].delete(k) if hash["attrs"][k].nil? } hash.delete("attrs") if hash["attrs"].empty? end hash end |
#width ⇒ Object
127 128 129 |
# File 'lib/prosereflect/image.rb', line 127 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 |