Untitled conversation
- User
import uuid import random import time import json from typing import List, Optional, Dict, Any from enum import Enum from abc import ABC, abstractmethod import re # Enums for predefined constants class StrokeStyle(Enum): SOLID = "solid" DASHED = "dashed" DOTTED = "dotted" class FillStyle(Enum): SOLID = "solid" HACHURE = "hachure" CROSS_HATCH = "cross-hatch" class RoundnessType(Enum): NONE = 0 MILD = 1 MODERATE = 2 ROUND = 3 class ArrowType(Enum): SHARP = "sharp" CURVED = "curved" ELBOW = "elbow" class ArrowHead(Enum): NONE = "none" ARROW = "arrow" BAR = "bar" TRIANGLE = "triangle" class FontStyle(Enum): COMIC_SANS = "Comic Sans" EXCALIFONT = "Excalifont" LILITA_ONE = "Lilita One" NUNITO = "Nunito" NORMAL = "Normal" # Added 'Normal' for default class FontFamily(Enum): HANDDRAWN = "handdrawn" NORMAL = "normal" CODE = "code" class TextAlign(Enum): LEFT = "left" MIDDLE = "middle" RIGHT = "right" class VerticalAlign(Enum): TOP = "top" MIDDLE = "middle" BOTTOM = "bottom" # Base class for all elements class ExcalidrawElement(ABC): def __init__( self, element_type: str, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0, # Rotation in radians ): self.id = str(uuid.uuid4()).replace('-', '') self.type = element_type self.x = x self.y = y self.width = width self.height = height self.angle = angle # Rotation in radians self.strokeColor = stroke_color self.backgroundColor = background_color self.fillStyle = fill_style.value if fill_style else "solid" self.strokeWidth = stroke_width self.strokeStyle = stroke_style.value if stroke_style else "solid" self.roughness = roughness self.opacity = opacity self.groupIds = [] self.frameId = None self.index = "" # To be set by the container self.roundness = {"type": roundness_type.value} if roundness_type and roundness_type != RoundnessType.NONE else None self.seed = random.randint(0, 2**32) self.version = 1 self.versionNonce = random.randint(0, 2**32) self.isDeleted = False self.boundElements = None self.updated = int(time.time() * 1000) self.link = None self.locked = False @abstractmethod def to_dict(self, index: str) -> Dict[str, Any]: pass # Rectangle element class Rectangle(ExcalidrawElement): def __init__(self, **kwargs): super().__init__(element_type="rectangle", **kwargs) def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, } if self.roundness is not None: element["roundness"] = self.roundness return element # Diamond element class Diamond(ExcalidrawElement): def __init__(self, **kwargs): super().__init__(element_type="diamond", **kwargs) def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, } if self.roundness is not None: element["roundness"] = self.roundness return element # Ellipse element class Ellipse(ExcalidrawElement): def __init__(self, **kwargs): super().__init__(element_type="ellipse", **kwargs) # Ellipse does not use roundness def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, } # Ellipse does not have roundness return element # Arrow element class Arrow(ExcalidrawElement): def __init__( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, arrow_type: ArrowType = ArrowType.SHARP, arrow_head_left: ArrowHead = ArrowHead.NONE, arrow_head_right: ArrowHead = ArrowHead.ARROW, points: Optional[List[List[float]]] = None, angle: float = 0.0 ): super().__init__( element_type="arrow", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color="transparent", # Arrows typically don't have background colors fill_style=FillStyle.SOLID, # Not used for arrows but required by the base class stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=None, # Arrows don't have roundness angle=angle ) self.points = points if points else [ [0, 0], [width, height] ] self.lastCommittedPoint = None self.startBinding = None self.endBinding = None self.startArrowhead = arrow_head_left.value self.endArrowhead = arrow_head_right.value self.elbowed = arrow_type == ArrowType.ELBOW def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "points": self.points, "lastCommittedPoint": self.lastCommittedPoint, "startBinding": self.startBinding, "endBinding": self.endBinding, "startArrowhead": self.startArrowhead, "endArrowhead": self.endArrowhead, "elbowed": self.elbowed, }) return element # Line element class Line(ExcalidrawElement): def __init__( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "#b2f2bb", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: RoundnessType = RoundnessType.MODERATE, points: Optional[List[List[float]]] = None, angle: float = 0.0 ): super().__init__( element_type="line", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.points = points if points else [ [0, 0], [width, height] ] self.lastCommittedPoint = None self.startBinding = None self.endBinding = None self.startArrowhead = ArrowHead.NONE.value self.endArrowhead = ArrowHead.NONE.value self.elbowed = False # Lines typically don't have elbowed paths unless specified def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "points": self.points, "lastCommittedPoint": self.lastCommittedPoint, "startBinding": self.startBinding, "endBinding": self.endBinding, "startArrowhead": self.startArrowhead, "endArrowhead": self.endArrowhead, "elbowed": self.elbowed, }) return element # Text element class Text(ExcalidrawElement): def __init__( self, x: float, y: float, text: str, font_size: int = 16, font_style: FontStyle = FontStyle.NORMAL, font_family: FontFamily = FontFamily.NORMAL, text_align: TextAlign = TextAlign.LEFT, vertical_align: VerticalAlign = VerticalAlign.TOP, line_height: float = 1.15, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 80, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ): super().__init__( element_type="text", x=x, y=y, width=0, # Width is auto-calculated if autoResize is True height=0, # Height is auto-calculated if autoResize is True stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.text = text self.fontSize = font_size self.fontStyle = font_style.value self.fontFamily = font_family.value self.textAlign = text_align.value self.verticalAlign = vertical_align.value self.lineHeight = line_height self.autoResize = True self.originalText = text self.containerId = None def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, "text": self.text, "fontSize": self.fontSize, "fontFamily": self.fontFamily, "textAlign": self.textAlign, "verticalAlign": self.verticalAlign, "originalText": self.originalText, "autoResize": self.autoResize, "lineHeight": self.lineHeight } if self.roundness is not None: element["roundness"] = self.roundness if self.containerId: element["containerId"] = self.containerId return element # Freedraw element class Freedraw(ExcalidrawElement): def __init__( self, x: float, y: float, width: float, height: float, points: Optional[List[List[float]]] = None, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ): super().__init__( element_type="freedraw", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.points = points if points else [ [0, 0], [width, height] ] self.lastCommittedPoint = None self.startBinding = None self.endBinding = None self.startArrowhead = ArrowHead.NONE.value self.endArrowhead = ArrowHead.NONE.value self.elbowed = False # Freedraw typically doesn't have elbowed paths def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "points": self.points, "lastCommittedPoint": self.lastCommittedPoint, "startBinding": self.startBinding, "endBinding": self.endBinding, "startArrowhead": self.startArrowhead, "endArrowhead": self.endArrowhead, "elbowed": self.elbowed, }) return element # ExcalidrawDocument class class ExcalidrawDocument: def __init__( self, grid_size: int = 20, grid_step: int = 5, grid_mode_enabled: bool = False, view_background_color: str = "#ffffff" ): self.elements: List[ExcalidrawElement] = [] self.appState = { "gridSize": grid_size, "gridStep": grid_step, "gridModeEnabled": grid_mode_enabled, "viewBackgroundColor": view_background_color } self.files: Dict[str, Any] = {} self.index_counter = 0 # To keep track of ordering def add_rectangle( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, fill_style: FillStyle = FillStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) rectangle = Rectangle( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, stroke_width=stroke_width, stroke_style=stroke_style, fill_style=fill_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(rectangle) def add_diamond( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, fill_style: FillStyle = FillStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) diamond = Diamond( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, stroke_width=stroke_width, stroke_style=stroke_style, fill_style=fill_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(diamond) def add_ellipse( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, fill_style: FillStyle = FillStyle.SOLID, roughness: int = 1, opacity: int = 100, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) ellipse = Ellipse( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, stroke_width=stroke_width, stroke_style=stroke_style, fill_style=fill_style, roughness=roughness, opacity=opacity, angle=angle # Ellipse does not use roundness ) self.elements.append(ellipse) def add_arrow( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, arrow_type: ArrowType = ArrowType.SHARP, arrow_head_left: ArrowHead = ArrowHead.NONE, arrow_head_right: ArrowHead = ArrowHead.ARROW, points: Optional[List[List[float]]] = None, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_dimensions(width, height) arrow = Arrow( x=x, y=y, width=width, height=height, stroke_color=stroke_color, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, arrow_type=arrow_type, arrow_head_left=arrow_head_left, arrow_head_right=arrow_head_right, points=points, angle=angle ) self.elements.append(arrow) def add_line( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "#b2f2bb", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: RoundnessType = RoundnessType.MODERATE, points: Optional[List[List[float]]] = None, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) line = Line( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, points=points, angle=angle ) self.elements.append(line) def add_text( self, x: float, y: float, text: str, font_size: int = 16, font_style: FontStyle = FontStyle.NORMAL, font_family: FontFamily = FontFamily.NORMAL, text_align: TextAlign = TextAlign.LEFT, vertical_align: VerticalAlign = VerticalAlign.TOP, line_height: float = 1.15, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 80, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) text_element = Text( x=x, y=y, text=text, font_size=font_size, font_style=font_style, font_family=font_family, text_align=text_align, vertical_align=vertical_align, line_height=line_height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(text_element) def add_freedraw( self, x: float, y: float, width: float, height: float, points: Optional[List[List[float]]] = None, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) freedraw_element = Freedraw( x=x, y=y, width=width, height=height, points=points, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(freedraw_element) def set_app_state( self, grid_size: Optional[int] = None, grid_step: Optional[int] = None, grid_mode_enabled: Optional[bool] = None, view_background_color: Optional[str] = None ) -> None: if grid_size is not None: self.appState["gridSize"] = grid_size if grid_step is not None: self.appState["gridStep"] = grid_step if grid_mode_enabled is not None: self.appState["gridModeEnabled"] = grid_mode_enabled if view_background_color is not None: self._validate_color(view_background_color) self.appState["viewBackgroundColor"] = view_background_color def to_json(self) -> str: excalidraw_json = { "type": "excalidraw", "version": 2, "source": "https://excalidraw.com", "elements": [], "appState": self.appState, "files": self.files } for element in self.elements: self.index_counter += 1 # Generate index like aA, aB, ..., aZ, aAA, aAB, etc. suffix = "" num = self.index_counter while num > 0: num, rem = divmod(num - 1, 26) suffix = chr(65 + rem) + suffix prefix = 'a' index = f"{prefix}{suffix}" excalidraw_json["elements"].append(element.to_dict(index=index)) return json.dumps(excalidraw_json, indent=2) def _validate_color(self, color: str) -> None: if not re.fullmatch(r"#(?:[0-9a-fA-F]{3}){1,2}", color): raise ValueError(f"Invalid color code: {color}") def _validate_dimensions(self, width: float, height: float) -> None: if width <= 0 or height <= 0: raise ValueError("Width and height must be positive numbers.")extend this to allow adding images, here is an example excalidraw that has 2 images:
{ "type": "excalidraw", "version": 2, "source": "https://excalidraw.com", "elements": [ { "id": "QIKBTDO6hFh3mN8uR8S9s", "type": "image", "x": -1246.840577990733, "y": 36.73726456668044, "width": 150, "height": 150, "angle": 0, "strokeColor": "transparent", "backgroundColor": "#ffec99", "fillStyle": "cross-hatch", "strokeWidth": 2, "strokeStyle": "solid", "roughness": 1, "opacity": 50, "groupIds": [], "frameId": null, "index": "a5", "roundness": null, "seed": 1237394127, "version": 6, "versionNonce": 954057551, "isDeleted": false, "boundElements": null, "updated": 1727830689991, "link": null, "locked": false, "status": "saved", "fileId": "434ac9b72010b4e981539df729db47e7de3b49ad", "scale": [ 1, 1 ] }, { "id": "agV6NU-wt2jWT2mfx_lHR", "type": "image", "x": -833.0405901977642, "y": 87.33727067019606, "width": 220, "height": 250, "angle": 0, "strokeColor": "transparent", "backgroundColor": "#ffec99", "fillStyle": "cross-hatch", "strokeWidth": 2, "strokeStyle": "solid", "roughness": 1, "opacity": 50, "groupIds": [], "frameId": null, "index": "a6", "roundness": null, "seed": 1873968015, "version": 32, "versionNonce": 745669999, "isDeleted": false, "boundElements": null, "updated": 1727830704791, "link": null, "locked": false, "status": "saved", "fileId": "6a8d7b3fa376f8f988ba55a21ab817495d76dcfd", "scale": [ 1, 1 ] } ], "appState": { "gridSize": 20, "gridStep": 5, "gridModeEnabled": false, "viewBackgroundColor": "#ffffff" }, "files": { "434ac9b72010b4e981539df729db47e7de3b49ad": { "mimeType": "image/svg+xml", "id": "434ac9b72010b4e981539df729db47e7de3b49ad", "dataURL": "data:image/svg+xml;base64,PHN2ZyB4bWWY9IiNTVkciLz4KCjwvc3ZnPg==", "created": 1727830689682, "lastRetrieved": 1727830689682 }, "6a8d7b3fa376f8f988ba55a21ab817495d76dcfd": { "mimeType": "image/jpeg", "id": "6a8d7b3fa376f8f988ba55a21ab817495d76dcfd", "dataURL": "data:image/jpeg;base64,/9j/4AAQSkZJPhHYiFQIENjK5X//Z", "created": 1727830703720, "lastRetrieved": 1727830703720 } } }basically, it supports jpeg,jpg, png,svg,avif,webp,gif,bmp, ico, jfif, svgz, pjpeg, pjp, dib where mime types and base64 data are stored as element
- Assistant
Certainly! Below is the extended version of your
ExcalidrawDocumentPython module that includes support for adding images. This extension introduces a newImageclass and anadd_imagemethod within theExcalidrawDocumentclass. The implementation ensures that only supported image MIME types are allowed and that image data is correctly handled and stored.Key Additions:
- Supported MIME Types: A list of allowed MIME types for images is defined to ensure only valid image formats are added.
- Image Class: A new
Imageclass inherits fromExcalidrawElementand includes properties specific to image elements, such asfileIdandscale. - Add Image Method: The
ExcalidrawDocumentclass now includes anadd_imagemethod that handles image validation, encoding, and integration into the document'selementsandfilessections.
Complete Extended Code:
import uuid import random import time import json from typing import List, Optional, Dict, Any from enum import Enum from abc import ABC, abstractmethod import re # Enums for predefined constants class StrokeStyle(Enum): SOLID = "solid" DASHED = "dashed" DOTTED = "dotted" class FillStyle(Enum): SOLID = "solid" HACHURE = "hachure" CROSS_HATCH = "cross-hatch" class RoundnessType(Enum): NONE = 0 MILD = 1 MODERATE = 2 ROUND = 3 class ArrowType(Enum): SHARP = "sharp" CURVED = "curved" ELBOW = "elbow" class ArrowHead(Enum): NONE = "none" ARROW = "arrow" BAR = "bar" TRIANGLE = "triangle" class FontStyle(Enum): COMIC_SANS = "Comic Sans" EXCALIFONT = "Excalifont" LILITA_ONE = "Lilita One" NUNITO = "Nunito" NORMAL = "Normal" # Added 'Normal' for default class FontFamily(Enum): HANDDRAWN = "handdrawn" NORMAL = "normal" CODE = "code" class TextAlign(Enum): LEFT = "left" MIDDLE = "middle" RIGHT = "right" class VerticalAlign(Enum): TOP = "top" MIDDLE = "middle" BOTTOM = "bottom" # Base class for all elements class ExcalidrawElement(ABC): def __init__( self, element_type: str, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0, # Rotation in radians ): self.id = str(uuid.uuid4()).replace('-', '') self.type = element_type self.x = x self.y = y self.width = width self.height = height self.angle = angle # Rotation in radians self.strokeColor = stroke_color self.backgroundColor = background_color self.fillStyle = fill_style.value if fill_style else "solid" self.strokeWidth = stroke_width self.strokeStyle = stroke_style.value if stroke_style else "solid" self.roughness = roughness self.opacity = opacity self.groupIds = [] self.frameId = None self.index = "" # To be set by the container self.roundness = {"type": roundness_type.value} if roundness_type and roundness_type != RoundnessType.NONE else None self.seed = random.randint(0, 2**32) self.version = 1 self.versionNonce = random.randint(0, 2**32) self.isDeleted = False self.boundElements = None self.updated = int(time.time() * 1000) self.link = None self.locked = False @abstractmethod def to_dict(self, index: str) -> Dict[str, Any]: pass # Rectangle element class Rectangle(ExcalidrawElement): def __init__(self, **kwargs): super().__init__(element_type="rectangle", **kwargs) def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, } if self.roundness is not None: element["roundness"] = self.roundness return element # Diamond element class Diamond(ExcalidrawElement): def __init__(self, **kwargs): super().__init__(element_type="diamond", **kwargs) def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, } if self.roundness is not None: element["roundness"] = self.roundness return element # Ellipse element class Ellipse(ExcalidrawElement): def __init__(self, **kwargs): super().__init__(element_type="ellipse", **kwargs) # Ellipse does not use roundness def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, } # Ellipse does not have roundness return element # Arrow element class Arrow(ExcalidrawElement): def __init__( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, arrow_type: ArrowType = ArrowType.SHARP, arrow_head_left: ArrowHead = ArrowHead.NONE, arrow_head_right: ArrowHead = ArrowHead.ARROW, points: Optional[List[List[float]]] = None, angle: float = 0.0 ): super().__init__( element_type="arrow", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color="transparent", # Arrows typically don't have background colors fill_style=FillStyle.SOLID, # Not used for arrows but required by the base class stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=None, # Arrows don't have roundness angle=angle ) self.points = points if points else [ [0, 0], [width, height] ] self.lastCommittedPoint = None self.startBinding = None self.endBinding = None self.startArrowhead = arrow_head_left.value self.endArrowhead = arrow_head_right.value self.elbowed = arrow_type == ArrowType.ELBOW def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "points": self.points, "lastCommittedPoint": self.lastCommittedPoint, "startBinding": self.startBinding, "endBinding": self.endBinding, "startArrowhead": self.startArrowhead, "endArrowhead": self.endArrowhead, "elbowed": self.elbowed, }) return element # Line element class Line(ExcalidrawElement): def __init__( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "#b2f2bb", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: RoundnessType = RoundnessType.MODERATE, points: Optional[List[List[float]]] = None, angle: float = 0.0 ): super().__init__( element_type="line", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.points = points if points else [ [0, 0], [width, height] ] self.lastCommittedPoint = None self.startBinding = None self.endBinding = None self.startArrowhead = ArrowHead.NONE.value self.endArrowhead = ArrowHead.NONE.value self.elbowed = False # Lines typically don't have elbowed paths unless specified def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "points": self.points, "lastCommittedPoint": self.lastCommittedPoint, "startBinding": self.startBinding, "endBinding": self.endBinding, "startArrowhead": self.startArrowhead, "endArrowhead": self.endArrowhead, "elbowed": self.elbowed, }) return element # Text element class Text(ExcalidrawElement): def __init__( self, x: float, y: float, text: str, font_size: int = 16, font_style: FontStyle = FontStyle.NORMAL, font_family: FontFamily = FontFamily.NORMAL, text_align: TextAlign = TextAlign.LEFT, vertical_align: VerticalAlign = VerticalAlign.TOP, line_height: float = 1.15, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 80, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ): super().__init__( element_type="text", x=x, y=y, width=0, # Width is auto-calculated if autoResize is True height=0, # Height is auto-calculated if autoResize is True stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.text = text self.fontSize = font_size self.fontStyle = font_style.value self.fontFamily = font_family.value self.textAlign = text_align.value self.verticalAlign = vertical_align.value self.lineHeight = line_height self.autoResize = True self.originalText = text self.containerId = None def to_dict(self, index: str) -> Dict[str, Any]: element = { "id": self.id, "type": self.type, "x": self.x, "y": self.y, "width": self.width, "height": self.height, "angle": self.angle, "strokeColor": self.strokeColor, "backgroundColor": self.backgroundColor, "fillStyle": self.fillStyle, "strokeWidth": self.strokeWidth, "strokeStyle": self.strokeStyle, "roughness": self.roughness, "opacity": self.opacity, "groupIds": self.groupIds, "frameId": self.frameId, "index": index, "seed": self.seed, "version": self.version, "versionNonce": self.versionNonce, "isDeleted": self.isDeleted, "boundElements": self.boundElements, "updated": self.updated, "link": self.link, "locked": self.locked, "text": self.text, "fontSize": self.fontSize, "fontFamily": self.fontFamily, "textAlign": self.textAlign, "verticalAlign": self.verticalAlign, "originalText": self.originalText, "autoResize": self.autoResize, "lineHeight": self.lineHeight } if self.roundness is not None: element["roundness"] = self.roundness if self.containerId: element["containerId"] = self.containerId return element # Freedraw element class Freedraw(ExcalidrawElement): def __init__( self, x: float, y: float, width: float, height: float, points: Optional[List[List[float]]] = None, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ): super().__init__( element_type="freedraw", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.points = points if points else [ [0, 0], [width, height] ] self.lastCommittedPoint = None self.startBinding = None self.endBinding = None self.startArrowhead = ArrowHead.NONE.value self.endArrowhead = ArrowHead.NONE.value self.elbowed = False # Freedraw typically doesn't have elbowed paths def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "points": self.points, "lastCommittedPoint": self.lastCommittedPoint, "startBinding": self.startBinding, "endBinding": self.endBinding, "startArrowhead": self.startArrowhead, "endArrowhead": self.endArrowhead, "elbowed": self.elbowed, }) return element # Image element class ImageElement(ExcalidrawElement): SUPPORTED_MIME_TYPES = { "image/jpeg", "image/jpg", "image/png", "image/svg+xml", "image/avif", "image/webp", "image/gif", "image/bmp", "image/x-icon", "image/jfif", "image/svg+xml", "image/svgz", "image/pjpeg", "image/pjp", "image/vnd.microsoft.icon", "image/dib", } def __init__( self, x: float, y: float, width: float, height: float, mime_type: str, base64_data: str, stroke_color: str = "transparent", background_color: str = "#ffec99", fill_style: FillStyle = FillStyle.CROSS_HATCH, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 50, angle: float = 0.0, scale: Optional[List[float]] = None, status: str = "saved" ): super().__init__( element_type="image", x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=None, # Images do not have roundness angle=angle ) self.status = status self.fileId = "" # To be set when added to document self.scale = scale if scale else [1, 1] # Validate MIME type if mime_type not in self.SUPPORTED_MIME_TYPES: raise ValueError(f"Unsupported MIME type: {mime_type}") self.mimeType = mime_type # Validate base64 data if not re.fullmatch(r"data:(image/(?:jpeg|jpg|png|svg\+xml|avif|webp|gif|bmp|x-icon|jfif|svgz|pjpeg|pjp|vnd\.microsoft\.icon|dib));base64,[A-Za-z0-9+/=]+", base64_data): raise ValueError("Invalid base64 data for image.") self.dataURL = base64_data def to_dict(self, index: str) -> Dict[str, Any]: element = super().to_dict(index) element.update({ "status": self.status, "fileId": self.fileId, "scale": self.scale, }) return element # ExcalidrawDocument class class ExcalidrawDocument: SUPPORTED_IMAGE_MIME_TYPES = { "image/jpeg", "image/jpg", "image/png", "image/svg+xml", "image/avif", "image/webp", "image/gif", "image/bmp", "image/x-icon", "image/jfif", "image/svgz", "image/pjpeg", "image/pjp", "image/vnd.microsoft.icon", "image/dib", } def __init__( self, grid_size: int = 20, grid_step: int = 5, grid_mode_enabled: bool = False, view_background_color: str = "#ffffff" ): self.elements: List[ExcalidrawElement] = [] self.appState = { "gridSize": grid_size, "gridStep": grid_step, "gridModeEnabled": grid_mode_enabled, "viewBackgroundColor": view_background_color } self.files: Dict[str, Any] = {} self.index_counter = 0 # To keep track of ordering def add_rectangle( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, fill_style: FillStyle = FillStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) rectangle = Rectangle( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, stroke_width=stroke_width, stroke_style=stroke_style, fill_style=fill_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(rectangle) def add_diamond( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, fill_style: FillStyle = FillStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) diamond = Diamond( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, stroke_width=stroke_width, stroke_style=stroke_style, fill_style=fill_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(diamond) def add_ellipse( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "transparent", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, fill_style: FillStyle = FillStyle.SOLID, roughness: int = 1, opacity: int = 100, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) ellipse = Ellipse( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, stroke_width=stroke_width, stroke_style=stroke_style, fill_style=fill_style, roughness=roughness, opacity=opacity, angle=angle # Ellipse does not use roundness ) self.elements.append(ellipse) def add_arrow( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", stroke_width: int = 1, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, arrow_type: ArrowType = ArrowType.SHARP, arrow_head_left: ArrowHead = ArrowHead.NONE, arrow_head_right: ArrowHead = ArrowHead.ARROW, points: Optional[List[List[float]]] = None, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_dimensions(width, height) arrow = Arrow( x=x, y=y, width=width, height=height, stroke_color=stroke_color, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, arrow_type=arrow_type, arrow_head_left=arrow_head_left, arrow_head_right=arrow_head_right, points=points, angle=angle ) self.elements.append(arrow) def add_line( self, x: float, y: float, width: float, height: float, stroke_color: str = "#000000", background_color: str = "#b2f2bb", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: RoundnessType = RoundnessType.MODERATE, points: Optional[List[List[float]]] = None, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) line = Line( x=x, y=y, width=width, height=height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, points=points, angle=angle ) self.elements.append(line) def add_text( self, x: float, y: float, text: str, font_size: int = 16, font_style: FontStyle = FontStyle.NORMAL, font_family: FontFamily = FontFamily.NORMAL, text_align: TextAlign = TextAlign.LEFT, vertical_align: VerticalAlign = VerticalAlign.TOP, line_height: float = 1.15, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 80, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) text_element = Text( x=x, y=y, text=text, font_size=font_size, font_style=font_style, font_family=font_family, text_align=text_align, vertical_align=vertical_align, line_height=line_height, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(text_element) def add_freedraw( self, x: float, y: float, width: float, height: float, points: Optional[List[List[float]]] = None, stroke_color: str = "#000000", background_color: str = "transparent", fill_style: FillStyle = FillStyle.SOLID, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 100, roundness_type: Optional[RoundnessType] = RoundnessType.NONE, angle: float = 0.0 ) -> None: self._validate_color(stroke_color) self._validate_color(background_color) self._validate_dimensions(width, height) freedraw_element = Freedraw( x=x, y=y, width=width, height=height, points=points, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, roundness_type=roundness_type, angle=angle ) self.elements.append(freedraw_element) def add_image( self, x: float, y: float, width: float, height: float, mime_type: str, base64_data: str, stroke_color: str = "transparent", background_color: str = "#ffec99", fill_style: FillStyle = FillStyle.CROSS_HATCH, stroke_width: int = 2, stroke_style: StrokeStyle = StrokeStyle.SOLID, roughness: int = 1, opacity: int = 50, angle: float = 0.0, scale: Optional[List[float]] = None, status: str = "saved" ) -> None: self._validate_image_mime_type(mime_type) self._validate_base64_data(mime_type, base64_data) self._validate_dimensions(width, height) self._validate_color(stroke_color) self._validate_color(background_color) image = ImageElement( x=x, y=y, width=width, height=height, mime_type=mime_type, base64_data=base64_data, stroke_color=stroke_color, background_color=background_color, fill_style=fill_style, stroke_width=stroke_width, stroke_style=stroke_style, roughness=roughness, opacity=opacity, angle=angle, scale=scale, status=status ) # Generate a unique fileId file_id = str(uuid.uuid4()).replace('-', '') image.fileId = file_id # Add file to files dict current_time = int(time.time() * 1000) self.files[file_id] = { "mimeType": mime_type, "id": file_id, "dataURL": base64_data, "created": current_time, "lastRetrieved": current_time } self.elements.append(image) def set_app_state( self, grid_size: Optional[int] = None, grid_step: Optional[int] = None, grid_mode_enabled: Optional[bool] = None, view_background_color: Optional[str] = None ) -> None: if grid_size is not None: self.appState["gridSize"] = grid_size if grid_step is not None: self.appState["gridStep"] = grid_step if grid_mode_enabled is not None: self.appState["gridModeEnabled"] = grid_mode_enabled if view_background_color is not None: self._validate_color(view_background_color) self.appState["viewBackgroundColor"] = view_background_color def to_json(self) -> str: excalidraw_json = { "type": "excalidraw", "version": 2, "source": "https://excalidraw.com", "elements": [], "appState": self.appState, "files": self.files } for element in self.elements: self.index_counter += 1 # Generate index like aA, aB, ..., aZ, aAA, aAB, etc. suffix = "" num = self.index_counter while num > 0: num, rem = divmod(num - 1, 26) suffix = chr(65 + rem) + suffix prefix = 'a' index = f"{prefix}{suffix}" excalidraw_json["elements"].append(element.to_dict(index=index)) return json.dumps(excalidraw_json, indent=2) def _validate_color(self, color: str) -> None: if not re.fullmatch(r"#(?:[0-9a-fA-F]{3}){1,2}", color): raise ValueError(f"Invalid color code: {color}") def _validate_dimensions(self, width: float, height: float) -> None: if width <= 0 or height <= 0: raise ValueError("Width and height must be positive numbers.") def _validate_image_mime_type(self, mime_type: str) -> None: if mime_type not in self.SUPPORTED_IMAGE_MIME_TYPES: raise ValueError(f"Unsupported image MIME type: {mime_type}") def _validate_base64_data(self, mime_type: str, base64_data: str) -> None: pattern = rf"data:{re.escape(mime_type)};base64,[A-Za-z0-9+/=]+" if not re.fullmatch(pattern, base64_data): raise ValueError("Invalid base64 data for the provided MIME type.")Explanation of the Additions:
-
Supported MIME Types:
SUPPORTED_MIME_TYPES = { "image/jpeg", "image/jpg", "image/png", "image/svg+xml", "image/avif", "image/webp", "image/gif", "image/bmp", "image/x-icon", "image/jfif", "image/svgz", "image/pjpeg", "image/pjp", "image/vnd.microsoft.icon", "image/dib", }- This set ensures that only images with these MIME types can be added to the document.
-
ImageElement Class:
class ImageElement(ExcalidrawElement): SUPPORTED_MIME_TYPES = { ... } # As defined above def __init__( ... ): ... # Validate MIME type if mime_type not in self.SUPPORTED_MIME_TYPES: raise ValueError(f"Unsupported MIME type: {mime_type}") self.mimeType = mime_type # Validate base64 data if not re.fullmatch(r"data:(image/(?:jpeg|jpg|png|svg\+xml|avif|webp|gif|bmp|x-icon|jfif|svgz|pjpeg|pjp|vnd\.microsoft\.icon|dib));base64,[A-Za-z0-9+/=]+", base64_data): raise ValueError("Invalid base64 data for image.") self.dataURL = base64_data ...- Inherits from
ExcalidrawElement. - Validates the MIME type against the supported types.
- Ensures that the provided
base64_datamatches the expected format for the given MIME type. - Initializes image-specific properties like
fileIdandscale.
- Inherits from
-
add_image Method in ExcalidrawDocument:
def add_image( ... ): self._validate_image_mime_type(mime_type) self._validate_base64_data(mime_type, base64_data) self._validate_dimensions(width, height) self._validate_color(stroke_color) self._validate_color(background_color) image = ImageElement( ... ) # Generate a unique fileId file_id = str(uuid.uuid4()).replace('-', '') image.fileId = file_id # Add file to files dict current_time = int(time.time() * 1000) self.files[file_id] = { "mimeType": mime_type, "id": file_id, "dataURL": base64_data, "created": current_time, "lastRetrieved": current_time } self.elements.append(image)- Validates the MIME type and base64 data.
- Creates an
ImageElementinstance. - Generates a unique
fileIdand associates it with the image. - Stores the image data in the
filesdictionary with metadata. - Appends the image to the
elementslist.
-
Validation Methods:
def _validate_image_mime_type(self, mime_type: str) -> None: if mime_type not in self.SUPPORTED_IMAGE_MIME_TYPES: raise ValueError(f"Unsupported image MIME type: {mime_type}") def _validate_base64_data(self, mime_type: str, base64_data: str) -> None: pattern = rf"data:{re.escape(mime_type)};base64,[A-Za-z0-9+/=]+" if not re.fullmatch(pattern, base64_data): raise ValueError("Invalid base64 data for the provided MIME type.")- Ensures that only supported MIME types and correctly formatted base64 data are accepted.
Usage Example:
Here's how you can use the extended
ExcalidrawDocumentto add images:# Instantiate the document doc = ExcalidrawDocument() # Example base64 image data (shortened for brevity) base64_svg = "data:image/svg+xml;base64,PHN2ZyB4bWw..." base64_png = "data:image/png;base64,iVBORw0KGgoAAAANSUhE..." # Add two images doc.add_image( x=-1246.840577990733, y=36.73726456668044, width=150, height=150, mime_type="image/svg+xml", base64_data=base64_svg, background_color="#ffec99", fill_style=FillStyle.CROSS_HATCH, opacity=50 ) doc.add_image( x=-833.0405901977642, y=87.33727067019606, width=220, height=250, mime_type="image/jpeg", base64_data=base64_png, background_color="#ffec99", fill_style=FillStyle.CROSS_HATCH, opacity=50 ) # Export to JSON excalidraw_json = doc.to_json() print(excalidraw_json)Notes:
- Base64 Data: Ensure that the
base64_dataprovided to theadd_imagemethod is correctly encoded and matches the specifiedmime_type. - Scaling: The
scaleparameter defaults to[1, 1]. Adjust as needed for different scaling factors. - Unique
fileId: Each image added generates a uniquefileIdto reference the image data within thefilessection of the JSON. - Validation: The module includes robust validation to prevent adding unsupported image types or malformed data, ensuring the integrity of the Excalidraw document.
With these extensions, your
ExcalidrawDocumentclass is now capable of handling image elements alongside other drawing elements, fully adhering to the structure expected by Excalidraw.