Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

feat: wireframe-to-code (#7334)

David Luzarc7ee46e7f8a7
diff --git a/src/actions/actionAlign.tsx b/src/actions/actionAlign.tsx
index 5697a707e2..137f68ae9f 100644
--- a/src/actions/actionAlign.tsx
+++ b/src/actions/actionAlign.tsx
@@ -9,6 +9,7 @@ import {
} from "../components/icons";
import { ToolButton } from "../components/ToolButton";
import { getNonDeletedElements } from "../element";
+import { isFrameLikeElement } from "../element/typeChecks";
import { ExcalidrawElement } from "../element/types";
import { updateFrameMembershipOfSelectedElements } from "../frame";
import { t } from "../i18n";
@@ -28,7 +29,7 @@ const alignActionsPredicate = (
return (
selectedElements.length > 1 &&
// TODO enable aligning frames when implemented properly
- !selectedElements.some((el) => el.type === "frame")
+ !selectedElements.some((el) => isFrameLikeElement(el))
);
};
diff --git a/src/actions/actionDeleteSelected.tsx b/src/actions/actionDeleteSelected.tsx
index 4d7ec6a7c3..de25ed8986 100644
--- a/src/actions/actionDeleteSelected.tsx
+++ b/src/actions/actionDeleteSelected.tsx
@@ -10,7 +10,7 @@ import { newElementWith } from "../element/mutateElement";
import { getElementsInGroup } from "../groups";
import { LinearElementEditor } from "../element/linearElementEditor";
import { fixBindingsAfterDeletion } from "../element/binding";
-import { isBoundToContainer } from "../element/typeChecks";
+import { isBoundToContainer, isFrameLikeElement } from "../element/typeChecks";
import { updateActiveTool } from "../utils";
import { TrashIcon } from "../components/icons";
@@ -20,7 +20,7 @@ const deleteSelectedElements = (
) => {
const framesToBeDeleted = new Set(
getSelectedElements(
- elements.filter((el) => el.type === "frame"),
+ elements.filter((el) => isFrameLikeElement(el)),
appState,
).map((el) => el.id),
);
diff --git a/src/actions/actionDistribute.tsx b/src/actions/actionDistribute.tsx
index d3cdb5c9cd..bf51bedf4b 100644
--- a/src/actions/actionDistribute.tsx
+++ b/src/actions/actionDistribute.tsx
@@ -5,6 +5,7 @@ import {
import { ToolButton } from "../components/ToolButton";
import { distributeElements, Distribution } from "../distribute";
import { getNonDeletedElements } from "../element";
+import { isFrameLikeElement } from "../element/typeChecks";
import { ExcalidrawElement } from "../element/types";
import { updateFrameMembershipOfSelectedElements } from "../frame";
import { t } from "../i18n";
@@ -19,7 +20,7 @@ const enableActionGroup = (appState: AppState, app: AppClassProperties) => {
return (
selectedElements.length > 1 &&
// TODO enable distributing frames when implemented properly
- !selectedElements.some((el) => el.type === "frame")
+ !selectedElements.some((el) => isFrameLikeElement(el))
);
};
diff --git a/src/actions/actionDuplicateSelection.tsx b/src/actions/actionDuplicateSelection.tsx
index 060a286800..ba079168e9 100644
--- a/src/actions/actionDuplicateSelection.tsx
+++ b/src/actions/actionDuplicateSelection.tsx
@@ -20,7 +20,7 @@ import {
bindTextToShapeAfterDuplication,
getBoundTextElement,
} from "../element/textElement";
-import { isBoundToContainer, isFrameElement } from "../element/typeChecks";
+import { isBoundToContainer, isFrameLikeElement } from "../element/typeChecks";
import { normalizeElementOrder } from "../element/sortElements";
import { DuplicateIcon } from "../components/icons";
import {
@@ -140,11 +140,11 @@ const duplicateElements = (
}
const boundTextElement = getBoundTextElement(element);
- const isElementAFrame = isFrameElement(element);
+ const isElementAFrameLike = isFrameLikeElement(element);
if (idsOfElementsToDuplicate.get(element.id)) {
// if a group or a container/bound-text or frame, duplicate atomically
- if (element.groupIds.length || boundTextElement || isElementAFrame) {
+ if (element.groupIds.length || boundTextElement || isElementAFrameLike) {
const groupId = getSelectedGroupForElement(appState, element);
if (groupId) {
// TODO:
@@ -154,7 +154,7 @@ const duplicateElements = (
sortedElements,
groupId,
).flatMap((element) =>
- isFrameElement(element)
+ isFrameLikeElement(element)
? [...getFrameChildren(elements, element.id), element]
: [element],
);
@@ -180,7 +180,7 @@ const duplicateElements = (
);
continue;
}
- if (isElementAFrame) {
+ if (isElementAFrameLike) {
const elementsInFrame = getFrameChildren(sortedElements, element.id);
elementsWithClones.push(
diff --git a/src/actions/actionElementLock.ts b/src/actions/actionElementLock.ts
index cd539c5a35..164240b290 100644
--- a/src/actions/actionElementLock.ts
+++ b/src/actions/actionElementLock.ts
@@ -1,4 +1,5 @@
import { newElementWith } from "../element/mutateElement";
+import { isFrameLikeElement } from "../element/typeChecks";
import { ExcalidrawElement } from "../element/types";
import { KEYS } from "../keys";
import { arrayToMap } from "../utils";
@@ -51,7 +52,7 @@ export const actionToggleElementLock = register({
selectedElementIds: appState.selectedElementIds,
includeBoundTextElement: false,
});
- if (selected.length === 1 && selected[0].type !== "frame") {
+ if (selected.length === 1 && !isFrameLikeElement(selected[0])) {
return selected[0].locked
? "labels.elementLock.unlock"
: "labels.elementLock.lock";
diff --git a/src/actions/actionFrame.ts b/src/actions/actionFrame.ts
index 9e8c16c23d..4cddb2ac0f 100644
--- a/src/actions/actionFrame.ts
+++ b/src/actions/actionFrame.ts
@@ -7,23 +7,27 @@ import { AppClassProperties, AppState } from "../types";
import { updateActiveTool } from "../utils";
import { setCursorForShape } from "../cursor";
import { register } from "./register";
+import { isFrameLikeElement } from "../element/typeChecks";
const isSingleFrameSelected = (appState: AppState, app: AppClassProperties) => {
const selectedElements = app.scene.getSelectedElements(appState);
- return selectedElements.length === 1 && selectedElements[0].type === "frame";
+ return (
+ selectedElements.length === 1 && isFrameLikeElement(selectedElements[0])
+ );
};
export const actionSelectAllElementsInFrame = register({
name: "selectAllElementsInFrame",
trackEvent: { category: "canvas" },
perform: (elements, appState, _, app) => {
- const selectedFrame = app.scene.getSelectedElements(appState)[0];
+ const selectedElement =
+ app.scene.getSelectedElements(appState).at(0) || null;
- if (selectedFrame && selectedFrame.type === "frame") {
+ if (isFrameLikeElement(selectedElement)) {
const elementsInFrame = getFrameChildren(
getNonDeletedElements(elements),
- selectedFrame.id,
+ selectedElement.id,
).filter((element) => !(element.type === "text" && element.containerId));
return {
@@ -54,15 +58,20 @@ export const actionRemoveAllElementsFromFrame = register({
name: "removeAllElementsFromFrame",
trackEvent: { category: "history" },
perform: (elements, appState, _, app) => {
- const selectedFrame = app.scene.getSelectedElements(appState)[0];
+ const selectedElement =
+ app.scene.getSelectedElements(appState).at(0) || null;
- if (selectedFrame && selectedFrame.type === "frame") {
+ if (isFrameLikeElement(selectedElement)) {
return {
- elements: removeAllElementsFromFrame(elements, selectedFrame, appState),
+ elements: removeAllElementsFromFrame(
+ elements,
+ selectedElement,
+ appState,
+ ),
appState: {
...appState,
selectedElementIds: {
- [selectedFrame.id]: true,
+ [selectedElement.id]: true,
},
},
commitToHistory: true,
diff --git a/src/actions/actionGroup.tsx b/src/actions/actionGroup.tsx
index 219f1444cb..e6cb058401 100644
--- a/src/actions/actionGroup.tsx
+++ b/src/actions/actionGroup.tsx
@@ -22,8 +22,8 @@ import { AppClassProperties, AppState } from "../types";
import { isBoundToContainer } from "../element/typeChecks";
import {
getElementsInResizingFrame,
- getFrameElements,
- groupByFrames,
+ getFrameLikeElements,
+ groupByFrameLikes,
removeElementsFromFrame,
replaceAllElementsInFrame,
} from "../frame";
@@ -102,7 +102,7 @@ export const actionGroup = register({
// when it happens, we want to remove elements that are in the frame
// and are going to be grouped from the frame (mouthful, I know)
if (groupingElementsFromDifferentFrames) {
- const frameElementsMap = groupByFrames(selectedElements);
+ const frameElementsMap = groupByFrameLikes(selectedElements);
frameElementsMap.forEach((elementsInFrame, frameId) => {
nextElements = removeElementsFromFrame(
@@ -219,7 +219,7 @@ export const actionUngroup = register({
.map((element) => element.frameId!),
);
- const targetFrames = getFrameElements(elements).filter((frame) =>
+ const targetFrames = getFrameLikeElements(elements).filter((frame) =>
selectedElementFrameIds.has(frame.id),
);
diff --git a/src/actions/actionStyles.ts b/src/actions/actionStyles.ts
index 2b656b050e..9c6589bbc7 100644
--- a/src/actions/actionStyles.ts
+++ b/src/actions/actionStyles.ts
@@ -20,7 +20,7 @@ import {
hasBoundTextElement,
canApplyRoundnessTypeToElement,
getDefaultRoundnessTypeForElement,
- isFrameElement,
+ isFrameLikeElement,
isArrowElement,
} from "../element/typeChecks";
import { getSelectedElements } from "../scene";
@@ -138,7 +138,7 @@ export const actionPasteStyles = register({
});
}
- if (isFrameElement(element)) {
+ if (isFrameLikeElement(element)) {
newElement = newElementWith(newElement, {
roundness: null,
backgroundColor: "transparent",
diff --git a/src/clipboard.ts b/src/clipboard.ts
index 32b0edf1a1..a88402d694 100644
--- a/src/clipboard.ts
+++ b/src/clipboard.ts
@@ -9,7 +9,10 @@ import {
EXPORT_DATA_TYPES,
MIME_TYPES,
} from "./constants";
-import { isInitializedImageElement } from "./element/typeChecks";
+import {
+ isFrameLikeElement,
+ isInitializedImageElement,
+} from "./element/typeChecks";
import { deepCopyElement } from "./element/newElement";
import { mutateElement } from "./element/mutateElement";
import { getContainingFrame } from "./frame";
@@ -124,7 +127,7 @@ export const serializeAsClipboardJSON = ({
files: BinaryFiles | null;
}) => {
const framesToCopy = new Set(
- elements.filter((element) => element.type === "frame"),
+ elements.filter((element) => isFrameLikeElement(element)),
);
let foundFile = false;
diff --git a/src/components/Actions.tsx b/src/components/Actions.tsx
index 6d1d80b1e1..556dc4af7e 100644
--- a/src/components/Actions.tsx
+++ b/src/components/Actions.tsx
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import { ActionManager } from "../actions/manager";
import { getNonDeletedElements } from "../element";
-import { ExcalidrawElement } from "../element/types";
+import { ExcalidrawElement, ExcalidrawElementType } from "../element/types";
import { t } from "../i18n";
import { useDevice } from "../components/App";
import {
@@ -36,6 +36,8 @@ import {
frameToolIcon,
mermaidLogoIcon,
laserPointerToolIcon,
+ OpenAIIcon,
+ MagicIcon,
} from "./icons";
import { KEYS } from "../keys";
@@ -79,7 +81,8 @@ export const SelectedShapeActions = ({
const showLinkIcon =
targetElements.length === 1 || isSingleElementBoundContainer;
- let commonSelectedType: string | null = targetElements[0]?.type || null;
+ let commonSelectedType: ExcalidrawElementType | null =
+ targetElements[0]?.type || null;
for (const element of targetElements) {
if (element.type !== commonSelectedType) {
@@ -94,7 +97,8 @@ export const SelectedShapeActions = ({
{((hasStrokeColor(appState.activeTool.type) &&
appState.activeTool.type !== "image" &&
commonSelectedType !== "image" &&
- commonSelectedType !== "frame") ||
+ commonSelectedType !== "frame" &&
+ commonSelectedType !== "magicframe") ||
targetElements.some((element) => hasStrokeColor(element.type))) &&
renderAction("changeStrokeColor")}
</div>
@@ -331,6 +335,9 @@ export const ShapesSwitcher = ({
>
{t("toolBar.laser")}
</DropdownMenu.Item>
+ <div style={{ margin: "6px 0", fontSize: 14, fontWeight: 600 }}>
+ Generate
+ </div>
<DropdownMenu.Item
onSelect={() => app.setOpenDialog("mermaid")}
icon={mermaidLogoIcon}
@@ -338,6 +345,25 @@ export const ShapesSwitcher = ({
>
{t("toolBar.mermaidToExcalidraw")}
</DropdownMenu.Item>
+
+ {app.props.aiEnabled !== false && (
+ <>
+ <DropdownMenu.Item
+ onSelect={() => app.onMagicButtonSelect()}
+ icon={MagicIcon}
+ data-testid="toolbar-magicframe"
+ >
+ {t("toolBar.magicframe")}
+ </DropdownMenu.Item>
+ <DropdownMenu.Item
+ onSelect={() => app.setOpenDialog("magicSettings")}
+ icon={OpenAIIcon}
+ data-testid="toolbar-magicSettings"
+ >
+ {t("toolBar.magicSettings")}
+ </DropdownMenu.Item>
+ </>
+ )}
</DropdownMenu.Content>
</DropdownMenu>
</>
diff --git a/src/components/App.tsx b/src/components/App.tsx
index 0892a40a6f..c579d8b1cd 100644
--- a/src/components/App.tsx
+++ b/src/components/App.tsx
@@ -47,12 +47,15 @@ import {
isEraserActive,
isHandToolActive,
} from "../appState";
-import { PastedMixedContent, parseClipboard } from "../clipboard";
+import {
+ PastedMixedContent,
+ copyTextToSystemClipboard,
+ parseClipboard,
+} from "../clipboard";
import {
APP_NAME,
CURSOR_TYPE,
DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT,
- DEFAULT_UI_OPTIONS,
DEFAULT_VERTICAL_ALIGN,
DRAGGING_THRESHOLD,
ELEMENT_READY_TO_ERASE_OPACITY,
@@ -86,6 +89,8 @@ import {
YOUTUBE_STATES,
ZOOM_STEP,
POINTER_EVENTS,
+ TOOL_TYPE,
+ EDITOR_LS_KEYS,
} from "../constants";
import { ExportedElements, exportCanvas, loadFromBlob } from "../data";
import Library, { distributeLibraryItemsOnSquareGrid } from "../data/library";
@@ -139,6 +144,8 @@ import {
newFrameElement,
newFreeDrawElement,
newEmbeddableElement,
+ newMagicFrameElement,
+ newIframeElement,
} from "../element/newElement";
import {
hasBoundTextElement,
@@ -146,13 +153,17 @@ import {
isBindingElement,
isBindingElementType,
isBoundToContainer,
- isFrameElement,
+ isFrameLikeElement,
isImageElement,
isEmbeddableElement,
isInitializedImageElement,
isLinearElement,
isLinearElementType,
isUsingAdaptiveRadius,
+ isFrameElement,
+ isIframeElement,
+ isIframeLikeElement,
+ isMagicFrameElement,
} from "../element/typeChecks";
import {
ExcalidrawBindableElement,
@@ -167,8 +178,11 @@ import {
FileId,
NonDeletedExcalidrawElement,
ExcalidrawTextContainer,
- ExcalidrawFrameElement,
- ExcalidrawEmbeddableElement,
+ ExcalidrawFrameLikeElement,
+ ExcalidrawMagicFrameElement,
+ ExcalidrawIframeLikeElement,
+ IframeData,
+ ExcalidrawIframeElement,
} from "../element/types";
import { getCenter, getDistance } from "../gesture";
import {
@@ -256,6 +270,7 @@ import {
easeOut,
} from "../utils";
import {
+ createSrcDoc,
embeddableURLValidator,
extractSrc,
getEmbedLink,
@@ -328,6 +343,7 @@ import {
elementOverlapsWithFrame,
updateFrameMembershipOfSelectedElements,
isElementInFrame,
+ getFrameLikeTitle,
} from "../frame";
import {
excludeElementsInFramesFromSelection,
@@ -375,6 +391,13 @@ import {
setCursorForShape,
} from "../cursor";
import { Emitter } from "../emitter";
+import { ElementCanvasButtons } from "../element/ElementCanvasButtons";
+import { MagicCacheData, diagramToHTML } from "../data/magic";
+import { elementsOverlappingBBox, exportToBlob } from "../packages/utils";
+import { COLOR_PALETTE } from "../colors";
+import { ElementCanvasButton } from "./MagicButton";
+import { MagicIcon, copyIcon, fullscreenIcon } from "./icons";
+import { EditorLocalStorage } from "../data/EditorLocalStorage";
const AppContext = React.createContext<AppClassProperties>(null!);
const AppPropsContext = React.createContext<AppProps>(null!);
@@ -481,11 +504,6 @@ class App extends React.Component<AppProps, AppState> {
private excalidrawContainerRef = React.createRef<HTMLDivElement>();
- public static defaultProps: Partial<AppProps> = {
- // needed for tests to pass since we directly render App in many tests
- UIOptions: DEFAULT_UI_OPTIONS,
- };
-
public scene: Scene;
public renderer: Renderer;
private fonts: Fonts;
@@ -692,22 +710,22 @@ class App extends React.Component<AppProps, AppState> {
}
}
- private updateEmbeddableRef(
- id: ExcalidrawEmbeddableElement["id"],
+ private cacheEmbeddableRef(
+ element: ExcalidrawIframeLikeElement,
ref: HTMLIFrameElement | null,
) {
if (ref) {
- this.iFrameRefs.set(id, ref);
+ this.iFrameRefs.set(element.id, ref);
}
}
private getHTMLIFrameElement(
- id: ExcalidrawEmbeddableElement["id"],
+ element: ExcalidrawIframeLikeElement,
): HTMLIFrameElement | undefined {
- return this.iFrameRefs.get(id);
+ return this.iFrameRefs.get(element.id);
}
- private handleEmbeddableCenterClick(element: ExcalidrawEmbeddableElement) {
+ private handleEmbeddableCenterClick(element: ExcalidrawIframeLikeElement) {
if (
this.state.activeEmbeddable?.element === element &&
this.state.activeEmbeddable?.state === "active"
@@ -730,7 +748,11 @@ class App extends React.Component<AppProps, AppState> {
});
}, 100);
- const iframe = this.getHTMLIFrameElement(element.id);
+ if (isIframeElement(element)) {
+ return;
+ }
+
+ const iframe = this.getHTMLIFrameElement(element);
if (!iframe?.contentWindow) {
return;
@@ -782,8 +804,8 @@ class App extends React.Component<AppProps, AppState> {
}
}
- private isEmbeddableCenter(
- el: ExcalidrawEmbeddableElement | null,
+ private isIframeLikeElementCenter(
+ el: ExcalidrawIframeLikeElement | null,
event: React.PointerEvent<HTMLElement> | PointerEvent,
sceneX: number,
sceneY: number,
@@ -805,12 +827,12 @@ class App extends React.Component<AppProps, AppState> {
}
private updateEmbeddables = () => {
- const embeddableElements = new Map<ExcalidrawElement["id"], true>();
+ const iframeLikes = new Set<ExcalidrawIframeLikeElement["id"]>();
let updated = false;
this.scene.getNonDeletedElements().filter((element) => {
if (isEmbeddableElement(element)) {
- embeddableElements.set(element.id, true);
+ iframeLikes.add(element.id);
if (element.validated == null) {
updated = true;
@@ -822,6 +844,8 @@ class App extends React.Component<AppProps, AppState> {
mutateElement(element, { validated }, false);
ShapeCache.delete(element);
}
+ } else if (isIframeElement(element)) {
+ iframeLikes.add(element.id);
}
return false;
});
@@ -832,7 +856,7 @@ class App extends React.Component<AppProps, AppState> {
// GC
this.iFrameRefs.forEach((ref, id) => {
- if (!embeddableElements.has(id)) {
+ if (!iframeLikes.has(id)) {
this.iFrameRefs.delete(id);
}
});
@@ -846,8 +870,8 @@ class App extends React.Component<AppProps, AppState> {
const embeddableElements = this.scene
.getNonDeletedElements()
.filter(
- (el): el is NonDeleted<ExcalidrawEmbeddableElement> =>
- isEmbeddableElement(el) && !!el.validated,
+ (el): el is NonDeleted<ExcalidrawIframeLikeElement> =>
+ (isEmbeddableElement(el) && !!el.validated) || isIframeElement(el),
);
return (
@@ -857,7 +881,150 @@ class App extends React.Component<AppProps, AppState> {
{ sceneX: el.x, sceneY: el.y },
this.state,
);
- const embedLink = getEmbedLink(toValidURL(el.link || ""));
+
+ let src: IframeData | null;
+
+ if (isIframeElement(el)) {
+ src = null;
+
+ const data: MagicCacheData = (el.customData?.generationData ??
+ this.magicGenerations.get(el.id)) || {
+ status: "error",
+ message: "No generation data",
+ code: "ERR_NO_GENERATION_DATA",
+ };
+
+ if (data.status === "done") {
+ const html = data.html;
+ src = {
+ intrinsicSize: { w: el.width, h: el.height },
+ type: "document",
+ srcdoc: () => {
+ return html;
+ },
+ } as const;
+ } else if (data.status === "pending") {
+ src = {
+ intrinsicSize: { w: el.width, h: el.height },
+ type: "document",
+ srcdoc: () => {
+ return createSrcDoc(`
+ <style>
+ html, body {
+ width: 100%;
+ height: 100%;
+ color: ${
+ this.state.theme === "dark" ? "white" : "black"
+ };
+ }
+ body {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex-direction: column;
+ gap: 1rem;
+ }
+
+ .Spinner {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ margin-left: auto;
+ margin-right: auto;
+ }
+
+ .Spinner svg {
+ animation: rotate 1.6s linear infinite;
+ transform-origin: center center;
+ width: 40px;
+ height: 40px;
+ }
+
+ .Spinner circle {
+ stroke: currentColor;
+ animation: dash 1.6s linear 0s infinite;
+ stroke-linecap: round;
+ }
+
+ @keyframes rotate {
+ 100% {
+ transform: rotate(360deg);
+ }
+ }
+
+ @keyframes dash {
+ 0% {
+ stroke-dasharray: 1, 300;
+ stroke-dashoffset: 0;
+ }
+ 50% {
+ stroke-dasharray: 150, 300;
+ stroke-dashoffset: -200;
+ }
+ 100% {
+ stroke-dasharray: 1, 300;
+ stroke-dashoffset: -280;
+ }
+ }
+ </style>
+ <div class="Spinner">
+ <svg
+ viewBox="0 0 100 100"
+ >
+ <circle
+ cx="50"
+ cy="50"
+ r="46"
+ stroke-width="8"
+ fill="none"
+ stroke-miter-limit="10"
+ />
+ </svg>
+ </div>
+ <div>Generating...</div>
+ `);
+ },
+ } as const;
+ } else {
+ let message: string;
+ if (data.code === "ERR_GENERATION_INTERRUPTED") {
+ message = "Generation was interrupted...";
+ } else {
+ message = data.message || "Generation failed";
+ }
+ src = {
+ intrinsicSize: { w: el.width, h: el.height },
+ type: "document",
+ srcdoc: () => {
+ return createSrcDoc(`
+ <style>
+ html, body {
+ height: 100%;
+ }
+ body {
+ display: flex;
+ flex-direction: column;
+ align-items: center;
+ justify-content: center;
+ color: ${COLOR_PALETTE.red[3]};
+ }
+ h1, h3 {
+ margin-top: 0;
+ margin-bottom: 0.5rem;
+ }
+ </style>
+ <h1>Error!</h1>
+ <h3>${message}</h3>
+ `);
+ },
+ } as const;
+ }
+ } else {
+ src = getEmbedLink(toValidURL(el.link || ""));
+ }
+
+ // console.log({ src });
+
const isVisible = isElementInViewport(
el,
normalizedWidth,
@@ -929,19 +1096,19 @@ class App extends React.Component<AppProps, AppState> {
padding: `${el.strokeWidth}px`,
}}
>
- {this.props.renderEmbeddable?.(el, this.state) ?? (
+ {(isEmbeddableElement(el)
+ ? this.props.renderEmbeddable?.(el, this.state)
+ : null) ?? (
<iframe
- ref={(ref) => this.updateEmbeddableRef(el.id, ref)}
+ ref={(ref) => this.cacheEmbeddableRef(el, ref)}
className="excalidraw__embeddable"
srcDoc={
- embedLink?.type === "document"
- ? embedLink.srcdoc(this.state.theme)
+ src?.type === "document"
+ ? src.srcdoc(this.state.theme)
: undefined
}
src={
- embedLink?.type !== "document"
- ? embedLink?.link ?? ""
- : undefined
+ src?.type !== "document" ? src?.link ?? "" : undefined
}
// https://stackoverflow.com/q/18470015
scrolling="no"
@@ -1020,7 +1187,15 @@ class App extends React.Component<AppProps, AppState> {
const isDarkTheme = this.state.theme === "dark";
- return this.scene.getNonDeletedFrames().map((f, index) => {
+ let frameIndex = 0;
+ let magicFrameIndex = 0;
+
+ return this.scene.getNonDeletedFramesLikes().map((f) => {
+ if (isFrameElement(f)) {
+ frameIndex++;
+ } else {
+ magicFrameIndex++;
+ }
if (
!isElementInViewport(
f,
@@ -1056,8 +1231,13 @@ class App extends React.Component<AppProps, AppState> {
let frameNameJSX;
+ const frameName = getFrameLikeTitle(
+ f,
+ isFrameElement(f) ? frameIndex : magicFrameIndex,
+ );
+
if (f.id === this.state.editingFrame) {
- const frameNameInEdit = f.name == null ? `Frame ${index + 1}` : f.name;
+ const frameNameInEdit = frameName;
frameNameJSX = (
<input
@@ -1103,10 +1283,7 @@ class App extends React.Component<AppProps, AppState> {
/>
);
} else {
- frameNameJSX =
- f.name == null || f.name.trim() === ""
- ? `Frame ${index + 1}`
- : f.name.trim();
+ frameNameJSX = frameName;
}
return (
@@ -1190,6 +1367,8 @@ class App extends React.Component<AppProps, AppState> {
(this.state.editingElement &&
!isTextElement(this.state.editingElement)));
+ const firstSelectedElement = selectedElements[0];
+
return (
<div
className={clsx("excalidraw excalidraw-container", {
@@ -1250,6 +1429,10 @@ class App extends React.Component<AppProps, AppState> {
}
app={this}
isCollaborating={this.props.isCollaborating}
+ openAIKey={this.OPENAI_KEY}
+ isOpenAIKeyPersisted={this.OPENAI_KEY_IS_PERSISTED}
+ onOpenAIAPIKeyChange={this.onOpenAIKeyChange}
+ onMagicSettingsConfirm={this.onMagicSettingsConfirm}
>
{this.props.children}
{this.state.openDialog === "mermaid" && (
@@ -1262,16 +1445,83 @@ class App extends React.Component<AppProps, AppState> {
<div className="excalidraw-eye-dropper-container" />
<LaserToolOverlay manager={this.laserPathManager} />
{selectedElements.length === 1 &&
- !this.state.contextMenu &&
this.state.showHyperlinkPopup && (
<Hyperlink
- key={selectedElements[0].id}
- element={selectedElements[0]}
+ key={firstSelectedElement.id}
+ element={firstSelectedElement}
setAppState={this.setAppState}
onLinkOpen={this.props.onLinkOpen}
setToast={this.setToast}
/>
)}
+ {this.props.aiEnabled !== false &&
+ selectedElements.length === 1 &&
+ isMagicFrameElement(firstSelectedElement) && (
+ <ElementCanvasButtons
+ element={firstSelectedElement}
+ >
+ <ElementCanvasButton
+ title={t("labels.convertToCode")}
+ icon={MagicIcon}
+ checked={false}
+ onChange={() =>
+ this.onMagicFrameGenerate(
+ firstSelectedElement,
+ )
+ }
+ />
+ </ElementCanvasButtons>
+ )}
+ {selectedElements.length === 1 &&
+ isIframeElement(firstSelectedElement) &&
+ firstSelectedElement.customData?.generationData
+ ?.status === "done" && (
+ <ElementCanvasButtons
+ element={firstSelectedElement}
+ >
+ <ElementCanvasButton
+ title={t("labels.copySource")}
+ icon={copyIcon}
+ checked={false}
+ onChange={() =>
+ this.onIframeSrcCopy(firstSelectedElement)
+ }
+ />
+ <ElementCanvasButton
+ title="Enter fullscreen"
+ icon={fullscreenIcon}
+ checked={false}
+ onChange={() => {
+ const iframe =
+ this.getHTMLIFrameElement(
+ firstSelectedElement,
+ );
+ if (iframe) {
+ try {
+ iframe.requestFullscreen();
+ this.setState({
+ activeEmbeddable: {
+ element: firstSelectedElement,
+ state: "active",
+ },
+ selectedElementIds: {
+ [firstSelectedElement.id]: true,
+ },
+ draggingElement: null,
+ selectionElement: null,
+ });
+ } catch (err: any) {
+ console.warn(err);
+ this.setState({
+ errorMessage:
+ "Couldn't enter fullscreen",
+ });
+ }
+ }
+ }}
+ />
+ </ElementCanvasButtons>
+ )}
{this.state.toast !== null && (
<Toast
message={this.state.toast.message}
@@ -1372,7 +1622,7 @@ class App extends React.Component<AppProps, AppState> {
public onExportImage = async (
type: keyof typeof EXPORT_IMAGE_TYPES,
elements: ExportedElements,
- opts: { exportingFrame: ExcalidrawFrameElement | null },
+ opts: { exportingFrame: ExcalidrawFrameLikeElement | null },
) => {
trackEvent("export", type, "ui");
const fileHandle = await exportCanvas(
@@ -1402,6 +1652,250 @@ class App extends React.Component<AppProps, AppState> {
}
};
+ private magicGenerations = new Map<
+ ExcalidrawIframeElement["id"],
+ MagicCacheData
+ >();
+
+ private updateMagicGeneration = ({
+ frameElement,
+ data,
+ }: {
+ frameElement: ExcalidrawIframeElement;
+ data: MagicCacheData;
+ }) => {
+ if (data.status === "pending") {
+ // We don't wanna persist pending state to storage. It should be in-app
+ // state only.
+ // Thus reset so that we prefer local cache (if there was some
+ // generationData set previously)
+ mutateElement(
+ frameElement,
+ { customData: { generationData: undefined } },
+ false,
+ );
+ } else {
+ mutateElement(
+ frameElement,
+ { customData: { generationData: data } },
+ false,
+ );
+ }
+ this.magicGenerations.set(frameElement.id, data);
+ this.onSceneUpdated();
+ };
+
+ private getTextFromElements(elements: readonly ExcalidrawElement[]) {
+ const text = elements
+ .reduce((acc: string[], element) => {
+ if (isTextElement(element)) {
+ acc.push(element.text);
+ }
+ return acc;
+ }, [])
+ .join("\n\n");
+ return text;
+ }
+
+ private async onMagicFrameGenerate(magicFrame: ExcalidrawMagicFrameElement) {
+ if (!this.OPENAI_KEY) {
+ this.setState({
+ openDialog: "magicSettings",
+ });
+ return;
+ }
+
+ const magicFrameChildren = elementsOverlappingBBox({
+ elements: this.scene.getNonDeletedElements(),
+ bounds: magicFrame,
+ type: "overlap",
+ }).filter((el) => !isMagicFrameElement(el));
+
+ if (!magicFrameChildren.length) {
+ this.setState({ errorMessage: "Cannot generate from an empty frame" });
+ return;
+ }
+
+ const frameElement = this.insertIframeElement({
+ sceneX: magicFrame.x + magicFrame.width + 30,
+ sceneY: magicFrame.y,
+ width: magicFrame.width,
+ height: magicFrame.height,
+ });
+
+ if (!frameElement) {
+ return;
+ }
+
+ this.updateMagicGeneration({
+ frameElement,
+ data: { status: "pending" },
+ });
+
+ this.setState({
+ selectedElementIds: { [frameElement.id]: true },
+ });
+
+ const blob = await exportToBlob({
+ elements: this.scene.getNonDeletedElements(),
+ appState: {
+ ...this.state,
+ exportBackground: true,
+ viewBackgroundColor: this.state.viewBackgroundColor,
+ },
+ exportingFrame: magicFrame,
+ files: this.files,
+ });
+
+ const dataURL = await getDataURL(blob);
+
+ const textFromFrameChildren = this.getTextFromElements(magicFrameChildren);
+
+ const result = await diagramToHTML({
+ image: dataURL,
+ apiKey: this.OPENAI_KEY,
+ text: textFromFrameChildren,
+ theme: this.state.theme,
+ });
+
+ if (!result.ok) {
+ console.error(result.error);
+ this.updateMagicGeneration({
+ frameElement,
+ data: {
+ status: "error",
+ code: "ERR_OAI",
+ message: result.error?.message || "Unknown error during generation",
+ },
+ });
+ return;
+ }
+
+ if (result.choices[0].message.content == null) {
+ this.updateMagicGeneration({
+ frameElement,
+ data: {
+ status: "error",
+ code: "ERR_OAI",
+ message: "Nothing genereated :(",
+ },
+ });
+ return;
+ }
+
+ const message = result.choices[0].message.content;
+
+ const html = message.slice(
+ message.indexOf("<!DOCTYPE html>"),
+ message.indexOf("</html>") + "</html>".length,
+ );
+
+ this.updateMagicGeneration({
+ frameElement,
+ data: { status: "done", html },
+ });
+ }
+
+ private onIframeSrcCopy(element: ExcalidrawIframeElement) {
+ if (element.customData?.generationData?.status === "done") {
+ copyTextToSystemClipboard(element.customData.generationData.html);
+ this.setToast({
+ message: "copied to clipboard",
+ closable: false,
+ duration: 1500,
+ });
+ }
+ }
+
+ private OPENAI_KEY: string | null = EditorLocalStorage.get(
+ EDITOR_LS_KEYS.OAI_API_KEY,
+ );
+ private OPENAI_KEY_IS_PERSISTED: boolean =
+ EditorLocalStorage.has(EDITOR_LS_KEYS.OAI_API_KEY) || false;
+
+ private onOpenAIKeyChange = (openAIKey: string, shouldPersist: boolean) => {
+ this.OPENAI_KEY = openAIKey || null;
+ if (shouldPersist) {
+ const didPersist = EditorLocalStorage.set(
+ EDITOR_LS_KEYS.OAI_API_KEY,
+ openAIKey,
+ );
+ this.OPENAI_KEY_IS_PERSISTED = didPersist;
+ } else {
+ this.OPENAI_KEY_IS_PERSISTED = false;
+ }
+ };
+
+ private onMagicSettingsConfirm = (apiKey: string, shouldPersist: boolean) => {
+ this.onOpenAIKeyChange(apiKey, shouldPersist);
+
+ if (apiKey) {
+ const selectedElements = this.scene.getSelectedElements({
+ selectedElementIds: this.state.selectedElementIds,
+ });
+ if (selectedElements.length) {
+ this.onMagicButtonSelect();
+ }
+ } else {
+ this.OPENAI_KEY = null;
+ }
+ };
+
+ public onMagicButtonSelect = () => {
+ if (!this.OPENAI_KEY) {
+ this.setState({
+ openDialog: "magicSettings",
+ });
+ return;
+ }
+
+ const selectedElements = this.scene.getSelectedElements({
+ selectedElementIds: this.state.selectedElementIds,
+ });
+
+ if (selectedElements.length === 0) {
+ this.setActiveTool({ type: TOOL_TYPE.magicframe });
+ } else {
+ if (selectedElements.some((el) => isFrameLikeElement(el))) {
+ this.setActiveTool({ type: TOOL_TYPE.magicframe });
+ return;
+ }
+
+ let frame: ExcalidrawMagicFrameElement | null = null;
+ if (
+ selectedElements.length === 1 &&
+ isMagicFrameElement(selectedElements[0])
+ ) {
+ frame = selectedElements[0];
+ } else {
+ const [minX, minY, maxX, maxY] = getCommonBounds(selectedElements);
+ const padding = 50;
+
+ frame = newMagicFrameElement({
+ ...FRAME_STYLE,
+ x: minX - padding,
+ y: minY - padding,
+ width: maxX - minX + padding * 2,
+ height: maxY - minY + padding * 2,
+ opacity: 100,
+ locked: false,
+ });
+ }
+
+ this.scene.addNewElement(frame);
+
+ for (const child of selectedElements) {
+ mutateElement(child, { frameId: frame.id });
+ }
+
+ this.setState({
+ selectedElementIds: { [frame.id]: true },
+ });
+
+ this.onMagicFrameGenerate(frame);
+ }
+ };
+
private openEyeDropper = ({ type }: { type: "stroke" | "background" }) => {
jotaiStore.set(activeEyeDropperAtom, {
swapPreviewOnAlt: true,
@@ -1878,10 +2372,27 @@ class App extends React.Component<AppProps, AppState> {
this.onGestureEnd as any,
false,
);
+ document.removeEventListener(
+ EVENT.FULLSCREENCHANGE,
+ this.onFullscreenChange,
+ );
window.removeEventListener(EVENT.MESSAGE, this.onWindowMessage, false);
}
+ /** generally invoked only if fullscreen was invoked programmatically */
+ private onFullscreenChange = () => {
+ if (
+ // points to the iframe element we fullscreened
+ !document.fullscreenElement &&
+ this.state.activeEmbeddable?.state === "active"
+ ) {
+ this.setState({
+ activeEmbeddable: null,
+ });
+ }
+ };
+
private addEventListeners() {
this.removeEventListeners();
window.addEventListener(EVENT.MESSAGE, this.onWindowMessage, false);
@@ -1927,6 +2438,7 @@ class App extends React.Component<AppProps, AppState> {
return;
}
+ document.addEventListener(EVENT.FULLSCREENCHANGE, this.onFullscreenChange);
document.addEventListener(EVENT.PASTE, this.pasteFromClipboard);
document.addEventListener(EVENT.CUT, this.onCut);
if (this.props.detectScroll) {
@@ -3163,7 +3675,7 @@ class App extends React.Component<AppProps, AppState> {
});
event.preventDefault();
return;
- } else if (isFrameElement(selectedElement)) {
+ } else if (isFrameLikeElement(selectedElement)) {
this.setState({
editingFrame: selectedElement.id,
});
@@ -3873,7 +4385,7 @@ class App extends React.Component<AppProps, AppState> {
if (!event[KEYS.CTRL_OR_CMD] && !this.state.viewModeEnabled) {
const hitElement = this.getElementAtPosition(sceneX, sceneY);
- if (isEmbeddableElement(hitElement)) {
+ if (isIframeLikeElement(hitElement)) {
this.setState({
activeEmbeddable: { element: hitElement, state: "active" },
});
@@ -4011,9 +4523,9 @@ class App extends React.Component<AppProps, AppState> {
y: number;
}) => {
const frames = this.scene
- .getNonDeletedFrames()
- .filter((frame) =>
- isCursorInFrame(sceneCoords, frame as ExcalidrawFrameElement),
+ .getNonDeletedFramesLikes()
+ .filter((frame): frame is ExcalidrawFrameLikeElement =>
+ isCursorInFrame(sceneCoords, frame),
);
return frames.length ? frames[frames.length - 1] : null;
@@ -4374,8 +4886,8 @@ class App extends React.Component<AppProps, AppState> {
) {
if (
hitElement &&
- isEmbeddableElement(hitElement) &&
- this.isEmbeddableCenter(
+ isIframeLikeElement(hitElement) &&
+ this.isIframeLikeElementCenter(
hitElement,
event,
scenePointerX,
@@ -4783,8 +5295,14 @@ class App extends React.Component<AppProps, AppState> {
);
} else if (this.state.activeTool.type === "custom") {
setCursorForShape(this.interactiveCanvas, this.state);
- } else if (this.state.activeTool.type === "frame") {
- this.createFrameElementOnPointerDown(pointerDownState);
+ } else if (
+ this.state.activeTool.type === TOOL_TYPE.frame ||
+ this.state.activeTool.type === TOOL_TYPE.magicframe
+ ) {
+ this.createFrameElementOnPointerDown(
+ pointerDownState,
+ this.state.activeTool.type,
+ );
} else if (this.state.activeTool.type === "laser") {
this.laserPathManager.startPath(
pointerDownState.lastCoords.x,
@@ -4848,8 +5366,8 @@ class App extends React.Component<AppProps, AppState> {
scenePointer.y,
);
if (
- isEmbeddableElement(hitElement) &&
- this.isEmbeddableCenter(
+ isIframeLikeElement(hitElement) &&
+ this.isIframeLikeElementCenter(
hitElement,
event,
scenePointer.x,
@@ -4878,7 +5396,7 @@ class App extends React.Component<AppProps, AppState> {
) {
if (
clicklength < 300 &&
- this.hitLinkElement.type === "embeddable" &&
+ isIframeLikeElement(this.hitLinkElement) &&
!isPointHittingLinkIcon(this.hitLinkElement, this.state, [
scenePointer.x,
scenePointer.y,
@@ -5358,8 +5876,9 @@ class App extends React.Component<AppProps, AppState> {
element && previouslySelectedElements.push(element);
});
- // if hitElement is frame, deselect all of its elements if they are selected
- if (hitElement.type === "frame") {
+ // if hitElement is frame-like, deselect all of its elements
+ // if they are selected
+ if (isFrameLikeElement(hitElement)) {
getFrameChildren(
previouslySelectedElements,
hitElement.id,
@@ -5389,7 +5908,7 @@ class App extends React.Component<AppProps, AppState> {
gid,
),
)
- .filter((element) => element.type === "frame")
+ .filter((element) => isFrameLikeElement(element))
.map((frame) => frame.id),
);
@@ -5582,8 +6101,52 @@ class App extends React.Component<AppProps, AppState> {
});
};
+ public insertIframeElement = ({
+ sceneX,
+ sceneY,
+ width,
+ height,
+ }: {
+ sceneX: number;
+ sceneY: number;
+ width: number;
+ height: number;
+ }) => {
+ const [gridX, gridY] = getGridPoint(
+ sceneX,
+ sceneY,
+ this.lastPointerDownEvent?.[KEYS.CTRL_OR_CMD]
+ ? null
+ : this.state.gridSize,
+ );
+
+ const element = newIframeElement({
+ type: "iframe",
+ x: gridX,
+ y: gridY,
+ strokeColor: "transparent",
+ backgroundColor: "transparent",
+ fillStyle: this.state.currentItemFillStyle,
+ strokeWidth: this.state.currentItemStrokeWidth,
+ strokeStyle: this.state.currentItemStrokeStyle,
+ roughness: this.state.currentItemRoughness,
+ roundness: this.getCurrentItemRoundness("iframe"),
+ opacity: this.state.currentItemOpacity,
+ locked: false,
+ width,
+ height,
+ });
+
+ this.scene.replaceAllElements([
+ ...this.scene.getElementsIncludingDeleted(),
+ element,
+ ]);
+
+ return element;
+ };
+
//create rectangle element with youtube top left on nearest grid point width / hight 640/360
- private insertEmbeddableElement = ({
+ public insertEmbeddableElement = ({
sceneX,
sceneY,
link,
@@ -5623,8 +6186,8 @@ class App extends React.Component<AppProps, AppState> {
roundness: this.getCurrentItemRoundness("embeddable"),
opacity: this.state.currentItemOpacity,
locked: false,
- width: embedLink.aspectRatio.w,
- height: embedLink.aspectRatio.h,
+ width: embedLink.intrinsicSize.w,
+ height: embedLink.intrinsicSize.h,
link,
validated: null,
});
@@ -5812,6 +6375,7 @@ class App extends React.Component<AppProps, AppState> {
| "rectangle"
| "diamond"
| "ellipse"
+ | "iframe"
| "embeddable",
) {
return this.state.currentItemRoundness === "round"
@@ -5886,6 +6450,7 @@ class App extends React.Component<AppProps, AppState> {
private createFrameElementOnPointerDown = (
pointerDownState: PointerDownState,
+ type: Extract<ToolType, "frame" | "magicframe">,
): void => {
const [gridX, gridY] = getGridPoint(
pointerDownState.origin.x,
@@ -5895,13 +6460,18 @@ class App extends React.Component<AppProps, AppState> {
: this.state.gridSize,
);
- const frame = newFrameElement({
+ const constructorOpts = {
x: gridX,
y: gridY,
opacity: this.state.currentItemOpacity,
locked: false,
...FRAME_STYLE,
- });
+ } as const;
+
+ const frame =
+ type === TOOL_TYPE.magicframe
+ ? newMagicFrameElement(constructorOpts)
+ : newFrameElement(constructorOpts);
this.scene.replaceAllElements([
...this.scene.getElementsIncludingDeleted(),
@@ -6171,7 +6741,7 @@ class App extends React.Component<AppProps, AppState> {
}
const selectedElementsHasAFrame = selectedElements.find((e) =>
- isFrameElement(e),
+ isFrameLikeElement(e),
);
const topLayerFrame = this.getTopLayerFrameAtSceneCoords(pointerCoords);
this.setState({
@@ -6940,7 +7510,7 @@ class App extends React.Component<AppProps, AppState> {
}
}
- if (draggingElement.type === "frame") {
+ if (isFrameLikeElement(draggingElement)) {
const elementsInsideFrame = getElementsInNewFrame(
this.scene.getElementsIncludingDeleted(),
draggingElement,
@@ -6983,9 +7553,9 @@ class App extends React.Component<AppProps, AppState> {
const selectedFrames = this.scene
.getSelectedElements(this.state)
- .filter(
- (element) => element.type === "frame",
- ) as ExcalidrawFrameElement[];
+ .filter((element): element is ExcalidrawFrameLikeElement =>
+ isFrameLikeElement(element),
+ );
for (const frame of selectedFrames) {
nextElements = replaceAllElementsInFrame(
@@ -7301,8 +7871,8 @@ class App extends React.Component<AppProps, AppState> {
this.lastPointerDownEvent.timeStamp <
300 &&
gesture.pointers.size <= 1 &&
- isEmbeddableElement(hitElement) &&
- this.isEmbeddableCenter(
+ isIframeLikeElement(hitElement) &&
+ this.isIframeLikeElementCenter(
hitElement,
this.lastPointerUpEvent,
pointerDownState.origin.x,
@@ -8199,11 +8769,14 @@ class App extends React.Component<AppProps, AppState> {
this.maybeSuggestBindingForAll([draggingElement]);
// highlight elements that are to be added to frames on frames creation
- if (this.state.activeTool.type === "frame") {
+ if (
+ this.state.activeTool.type === TOOL_TYPE.frame ||
+ this.state.activeTool.type === TOOL_TYPE.magicframe
+ ) {
this.setState({
elementsToHighlight: getElementsInResizingFrame(
this.scene.getNonDeletedElements(),
- draggingElement as ExcalidrawFrameElement,
+ draggingElement as ExcalidrawFrameLikeElement,
this.state,
),
});
@@ -8217,8 +8790,9 @@ class App extends React.Component<AppProps, AppState> {
): boolean => {
const selectedElements = this.scene.getSelectedElements(this.state);
const selectedFrames = selectedElements.filter(
- (element) => element.type === "frame",
- ) as ExcalidrawFrameElement[];
+ (element): element is ExcalidrawFrameLikeElement =>
+ isFrameLikeElement(element),
+ );
const transformHandleType = pointerDownState.resize.handleType;
diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx
index b32adcf30a..3581f681f6 100644
--- a/src/components/LayerUI.tsx
+++ b/src/components/LayerUI.tsx
@@ -1,7 +1,12 @@
import clsx from "clsx";
import React from "react";
import { ActionManager } from "../actions/manager";
-import { CLASSES, DEFAULT_SIDEBAR, LIBRARY_SIDEBAR_WIDTH } from "../constants";
+import {
+ CLASSES,
+ DEFAULT_SIDEBAR,
+ LIBRARY_SIDEBAR_WIDTH,
+ TOOL_TYPE,
+} from "../constants";
import { showSelectedShapeActions } from "../element";
import { NonDeletedExcalidrawElement } from "../element/types";
import { Language, t } from "../i18n";
@@ -56,6 +61,7 @@ import { mutateElement } from "../element/mutateElement";
import { ShapeCache } from "../scene/ShapeCache";
import Scene from "../scene/Scene";
import { LaserPointerButton } from "./LaserTool/LaserPointerButton";
+import { MagicSettings } from "./MagicSettings";
interface LayerUIProps {
actionManager: ActionManager;
@@ -77,6 +83,10 @@ interface LayerUIProps {
children?: React.ReactNode;
app: AppClassProperties;
isCollaborating: boolean;
+ openAIKey: string | null;
+ isOpenAIKeyPersisted: boolean;
+ onOpenAIAPIKeyChange: (apiKey: string, shouldPersist: boolean) => void;
+ onMagicSettingsConfirm: (apiKey: string, shouldPersist: boolean) => void;
}
const DefaultMainMenu: React.FC<{
@@ -133,6 +143,10 @@ const LayerUI = ({
children,
app,
isCollaborating,
+ openAIKey,
+ isOpenAIKeyPersisted,
+ onOpenAIAPIKeyChange,
+ onMagicSettingsConfirm,
}: LayerUIProps) => {
const device = useDevice();
const tunnels = useInitializeTunnels();
@@ -295,9 +309,11 @@ const LayerUI = ({
>
<LaserPointerButton
title={t("toolBar.laser")}
- checked={appState.activeTool.type === "laser"}
+ checked={
+ appState.activeTool.type === TOOL_TYPE.laser
+ }
onChange={() =>
- app.setActiveTool({ type: "laser" })
+ app.setActiveTool({ type: TOOL_TYPE.laser })
}
isMobile
/>
@@ -439,6 +455,20 @@ const LayerUI = ({
}}
/>
)}
+ {appState.openDialog === "magicSettings" && (
+ <MagicSettings
+ openAIKey={openAIKey}
+ isPersisted={isOpenAIKeyPersisted}
+ onChange={onOpenAIAPIKeyChange}
+ onConfirm={(apiKey, shouldPersist) => {
+ setAppState({ openDialog: null });
+ onMagicSettingsConfirm(apiKey, shouldPersist);
+ }}
+ onClose={() => {
+ setAppState({ openDialog: null });
+ }}
+ />
+ )}
<ActiveConfirmDialog />
<tunnels.OverwriteConfirmDialogTunnel.Out />
{renderImageExportDialog()}
diff --git a/src/components/MagicButton.tsx b/src/components/MagicButton.tsx
new file mode 100644
index 0000000000..b8aad5bfd6
--- /dev/null
+++ b/src/components/MagicButton.tsx
@@ -0,0 +1,38 @@
+import "./ToolIcon.scss";
+
+import clsx from "clsx";
+import { ToolButtonSize } from "./ToolButton";
+
+const DEFAULT_SIZE: ToolButtonSize = "small";
+
+export const ElementCanvasButton = (props: {
+ title?: string;
+ icon: JSX.Element;
+ name?: string;
+ checked: boolean;
+ onChange?(): void;
+ isMobile?: boolean;
+}) => {
+ return (
+ <label
+ className={clsx(
+ "ToolIcon ToolIcon__MagicButton",
+ `ToolIcon_size_${DEFAULT_SIZE}`,
+ {
+ "is-mobile": props.isMobile,
+ },
+ )}
+ title={`${props.title}`}
+ >
+ <input
+ className="ToolIcon_type_checkbox"
+ type="checkbox"
+ name={props.name}
+ onChange={props.onChange}
+ checked={props.checked}
+ aria-label={props.title}
+ />
+ <div className="ToolIcon__icon">{props.icon}</div>
+ </label>
+ );
+};
diff --git a/src/components/MagicSettings.scss b/src/components/MagicSettings.scss
new file mode 100644
index 0000000000..1b2b8dda74
--- /dev/null
+++ b/src/components/MagicSettings.scss
@@ -0,0 +1,9 @@
+.excalidraw {
+ .MagicSettings-confirm {
+ padding: 0.5rem 1rem;
+ }
+
+ .MagicSettings__confirm {
+ margin-top: 2rem;
+ }
+}
diff --git a/src/components/MagicSettings.tsx b/src/components/MagicSettings.tsx
new file mode 100644
index 0000000000..b0fa9492a1
--- /dev/null
+++ b/src/components/MagicSettings.tsx
@@ -0,0 +1,145 @@
+import { useState } from "react";
+import { Dialog } from "./Dialog";
+import { TextField } from "./TextField";
+import { MagicIcon, OpenAIIcon } from "./icons";
+
+import "./MagicSettings.scss";
+import { FilledButton } from "./FilledButton";
+import { CheckboxItem } from "./CheckboxItem";
+import { KEYS } from "../keys";
+import { useUIAppState } from "../context/ui-appState";
+
+const InlineButton = ({ icon }: { icon: JSX.Element }) => {
+ return (
+ <span
+ style={{
+ width: "1em",
+ margin: "0 0.5ex 0 0.5ex",
+ display: "inline-block",
+ lineHeight: 0,
+ verticalAlign: "middle",
+ }}
+ >
+ {icon}
+ </span>
+ );
+};
+
+export const MagicSettings = (props: {
+ openAIKey: string | null;
+ isPersisted: boolean;
+ onChange: (key: string, shouldPersist: boolean) => void;
+ onConfirm: (key: string, shouldPersist: boolean) => void;
+ onClose: () => void;
+}) => {
+ const { theme } = useUIAppState();
+ const [keyInputValue, setKeyInputValue] = useState(props.openAIKey || "");
+ const [shouldPersist, setShouldPersist] = useState<boolean>(
+ props.isPersisted,
+ );
+
+ const onConfirm = () => {
+ props.onConfirm(keyInputValue.trim(), shouldPersist);
+ };
+
+ return (
+ <Dialog
+ onCloseRequest={() => {
+ props.onClose();
+ props.onConfirm(keyInputValue.trim(), shouldPersist);
+ }}
+ title={
+ <div style={{ display: "flex" }}>
+ Diagram to Code (AI){" "}
+ <div
+ style={{
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ padding: "0.1rem 0.5rem",
+ marginLeft: "1rem",
+ fontSize: 14,
+ borderRadius: "12px",
+ background: theme === "light" ? "#FFCCCC" : "#703333",
+ }}
+ >
+ Experimental
+ </div>
+ </div>
+ }
+ className="MagicSettings"
+ autofocus={false}
+ >
+ <p
+ style={{
+ display: "inline-flex",
+ alignItems: "center",
+ marginBottom: 0,
+ }}
+ >
+ For the diagram-to-code feature we use{" "}
+ <InlineButton icon={OpenAIIcon} />
+ OpenAI.
+ </p>
+ <p>
+ While the OpenAI API is in beta, its use is strictly limited — as such
+ we require you use your own API key. You can create an{" "}
+ <a
+ href="https://platform.openai.com/login?launch"
+ rel="noopener noreferrer"
+ target="_blank"
+ >
+ OpenAI account
+ </a>
+ , add a small credit (5 USD minimum), and{" "}
+ <a
+ href="https://platform.openai.com/api-keys"
+ rel="noopener noreferrer"
+ target="_blank"
+ >
+ generate your own API key
+ </a>
+ .
+ </p>
+ <p>
+ Your OpenAI key does not leave the browser, and you can also set your
+ own limit in your OpenAI account dashboard if needed.
+ </p>
+ <TextField
+ isPassword
+ value={keyInputValue}
+ placeholder="Paste your API key here"
+ label="OpenAI API key"
+ onChange={(value) => {
+ setKeyInputValue(value);
+ props.onChange(value.trim(), shouldPersist);
+ }}
+ selectOnRender
+ onKeyDown={(event) => event.key === KEYS.ENTER && onConfirm()}
+ />
+ <p>
+ By default, your API token is not persisted anywhere so you'll need to
+ insert it again after reload. But, you can persist locally in your
+ browser below.
+ </p>
+
+ <CheckboxItem checked={shouldPersist} onChange={setShouldPersist}>
+ Persist API key in browser storage
+ </CheckboxItem>
+
+ <p>
+ Once API key is set, you can use the <InlineButton icon={MagicIcon} />{" "}
+ tool to wrap your elements in a frame that will then allow you to turn
+ it into code. This dialog can be accessed using the <b>AI Settings</b>{" "}
+ <InlineButton icon={OpenAIIcon} />.
+ </p>
+
+ <FilledButton
+ className="MagicSettings__confirm"
+ size="large"
+ label="Confirm"
+ onClick={onConfirm}
+ />
+ </Dialog>
+ );
+};
diff --git a/src/components/PasteChartDialog.tsx b/src/components/PasteChartDialog.tsx
index 9878f6fcc2..17cd7121e2 100644
--- a/src/components/PasteChartDialog.tsx
+++ b/src/components/PasteChartDialog.tsx
@@ -94,7 +94,7 @@ export const PasteChartDialog = ({
const handleChartClick = (chartType: ChartType, elements: ChartElements) => {
onInsertElements(elements);
- trackEvent("magic", "chart", chartType);
+ trackEvent("paste", "chart", chartType);
setAppState({
currentChartType: chartType,
pasteDialog: {
diff --git a/src/components/PublishLibrary.tsx b/src/components/PublishLibrary.tsx
index ab02e1a891..6ec501a594 100644
--- a/src/components/PublishLibrary.tsx
+++ b/src/components/PublishLibrary.tsx
@@ -8,6 +8,7 @@ import Trans from "./Trans";
import { LibraryItems, LibraryItem, UIAppState } from "../types";
import { exportToCanvas, exportToSvg } from "../packages/utils";
import {
+ EDITOR_LS_KEYS,
EXPORT_DATA_TYPES,
EXPORT_SOURCE,
MIME_TYPES,
@@ -19,6 +20,7 @@ import { chunk } from "../utils";
import DialogActionButton from "./DialogActionButton";
import { CloseIcon } from "./icons";
import { ToolButton } from "./ToolButton";
+import { EditorLocalStorage } from "../data/EditorLocalStorage";
import "./PublishLibrary.scss";
@@ -31,34 +33,6 @@ interface PublishLibraryDataParams {
website: string;
}
-const LOCAL_STORAGE_KEY_PUBLISH_LIBRARY = "publish-library-data";
-
-const savePublishLibDataToStorage = (data: PublishLibraryDataParams) => {
- try {
- localStorage.setItem(
- LOCAL_STORAGE_KEY_PUBLISH_LIBRARY,
- JSON.stringify(data),
- );
- } catch (error: any) {
- // Unable to access window.localStorage
- console.error(error);
- }
-};
-
-const importPublishLibDataFromStorage = () => {
- try {
- const data = localStorage.getItem(LOCAL_STORAGE_KEY_PUBLISH_LIBRARY);
- if (data) {
- return JSON.parse(data);
- }
- } catch (error: any) {
- // Unable to access localStorage
- console.error(error);
- }
-
- return null;
-};
-
const generatePreviewImage = async (libraryItems: LibraryItems) => {
const MAX_ITEMS_PER_ROW = 6;
const BOX_SIZE = 128;
@@ -255,7 +229,9 @@ const PublishLibrary = ({
const [isSubmitting, setIsSubmitting] = useState(false);
useEffect(() => {
- const data = importPublishLibDataFromStorage();
+ const data = EditorLocalStorage.get<PublishLibraryDataParams>(
+ EDITOR_LS_KEYS.PUBLISH_LIBRARY,
+ );
if (data) {
setLibraryData(data);
}
@@ -328,7 +304,7 @@ const PublishLibrary = ({
if (response.ok) {
return response.json().then(({ url }) => {
// flush data from local storage
- localStorage.removeItem(LOCAL_STORAGE_KEY_PUBLISH_LIBRARY);
+ EditorLocalStorage.delete(EDITOR_LS_KEYS.PUBLISH_LIBRARY);
onSuccess({
url,
authorName: libraryData.authorName,
@@ -384,7 +360,7 @@ const PublishLibrary = ({
const onDialogClose = useCallback(() => {
updateItemsInStorage(clonedLibItems);
- savePublishLibDataToStorage(libraryData);
+ EditorLocalStorage.set(EDITOR_LS_KEYS.PUBLISH_LIBRARY, libraryData);
onClose();
}, [clonedLibItems, onClose, updateItemsInStorage, libraryData]);
diff --git a/src/components/TextField.tsx b/src/components/TextField.tsx
index 0f87bdb5c0..bae1b40a0b 100644
--- a/src/components/TextField.tsx
+++ b/src/components/TextField.tsx
@@ -4,12 +4,15 @@ import {
useImperativeHandle,
KeyboardEvent,
useLayoutEffect,
+ useState,
} from "react";
import clsx from "clsx";
import "./TextField.scss";
+import { Button } from "./Button";
+import { eyeIcon, eyeClosedIcon } from "./icons";
-export type TextFieldProps = {
+type TextFieldProps = {
value?: string;
onChange?: (value: string) => void;
@@ -22,6 +25,7 @@ export type TextFieldProps = {
label?: string;
placeholder?: string;
+ isPassword?: boolean;
};
export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
@@ -35,6 +39,7 @@ export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
readonly,
selectOnRender,
onKeyDown,
+ isPassword = false,
},
ref,
) => {
@@ -48,6 +53,8 @@ export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
}
}, [selectOnRender]);
+ const [isVisible, setIsVisible] = useState<boolean>(true);
+
return (
<div
className={clsx("ExcTextField", {
@@ -64,14 +71,22 @@ export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(
})}
>
<input
+ type={isPassword && isVisible ? "password" : undefined}
readOnly={readonly}
- type="text"
value={value}
placeholder={placeholder}
ref={innerRef}
onChange={(event) => onChange?.(event.target.value)}
onKeyDown={onKeyDown}
/>
+ {isPassword && (
+ <Button
+ onSelect={() => setIsVisible(!isVisible)}
+ style={{ border: 0 }}
+ >
+ {isVisible ? eyeIcon : eyeClosedIcon}
+ </Button>
+ )}
</div>
</div>
);
diff --git a/src/components/ToolIcon.scss b/src/components/ToolIcon.scss
index 01d0bef3dd..8e857dd54b 100644
--- a/src/components/ToolIcon.scss
+++ b/src/components/ToolIcon.scss
@@ -175,7 +175,8 @@
}
}
- .ToolIcon__LaserPointer .ToolIcon__icon {
+ .ToolIcon__LaserPointer .ToolIcon__icon,
+ .ToolIcon__MagicButton .ToolIcon__icon {
width: var(--default-button-size);
height: var(--default-button-size);
}
diff --git a/src/components/canvases/InteractiveCanvas.tsx b/src/components/canvases/InteractiveCanvas.tsx
index 73de478a64..5a524921a2 100644
--- a/src/components/canvases/InteractiveCanvas.tsx
+++ b/src/components/canvases/InteractiveCanvas.tsx
@@ -189,8 +189,6 @@ const getRelevantAppStateProps = (
suggestedBindings: appState.suggestedBindings,
isRotating: appState.isRotating,
elementsToHighlight: appState.elementsToHighlight,
- openSidebar: appState.openSidebar,
- showHyperlinkPopup: appState.showHyperlinkPopup,
collaborators: appState.collaborators, // Necessary for collab. sessions
activeEmbeddable: appState.activeEmbeddable,
snapLines: appState.snapLines,
diff --git a/src/components/icons.tsx b/src/components/icons.tsx
index 2695fd41be..7f591e7d77 100644
--- a/src/components/icons.tsx
+++ b/src/components/icons.tsx
@@ -1688,3 +1688,57 @@ export const laserPointerToolIcon = createIcon(
20,
);
+
+export const MagicIcon = createIcon(
+ <g stroke="currentColor" fill="none">
+ <path stroke="none" d="M0 0h24v24H0z" />
+ <path d="M6 21l15 -15l-3 -3l-15 15l3 3" />
+ <path d="M15 6l3 3" />
+ <path d="M9 3a2 2 0 0 0 2 2a2 2 0 0 0 -2 2a2 2 0 0 0 -2 -2a2 2 0 0 0 2 -2" />
+ <path d="M19 13a2 2 0 0 0 2 2a2 2 0 0 0 -2 2a2 2 0 0 0 -2 -2a2 2 0 0 0 2 -2" />
+ </g>,
+ tablerIconProps,
+);
+
+export const OpenAIIcon = createIcon(
+ <g stroke="currentColor" fill="none">
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
+ <path d="M11.217 19.384a3.501 3.501 0 0 0 6.783 -1.217v-5.167l-6 -3.35" />
+ <path d="M5.214 15.014a3.501 3.501 0 0 0 4.446 5.266l4.34 -2.534v-6.946" />
+ <path d="M6 7.63c-1.391 -.236 -2.787 .395 -3.534 1.689a3.474 3.474 0 0 0 1.271 4.745l4.263 2.514l6 -3.348" />
+ <path d="M12.783 4.616a3.501 3.501 0 0 0 -6.783 1.217v5.067l6 3.45" />
+ <path d="M18.786 8.986a3.501 3.501 0 0 0 -4.446 -5.266l-4.34 2.534v6.946" />
+ <path d="M18 16.302c1.391 .236 2.787 -.395 3.534 -1.689a3.474 3.474 0 0 0 -1.271 -4.745l-4.308 -2.514l-5.955 3.42" />
+ </g>,
+ tablerIconProps,
+);
+
+export const fullscreenIcon = createIcon(
+ <g stroke="currentColor" fill="none">
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
+ <path d="M4 8v-2a2 2 0 0 1 2 -2h2" />
+ <path d="M4 16v2a2 2 0 0 0 2 2h2" />
+ <path d="M16 4h2a2 2 0 0 1 2 2v2" />
+ <path d="M16 20h2a2 2 0 0 0 2 -2v-2" />
+ </g>,
+ tablerIconProps,
+);
+
+export const eyeIcon = createIcon(
+ <g stroke="currentColor" fill="none">
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
+ <path d="M10 12a2 2 0 1 0 4 0a2 2 0 0 0 -4 0" />
+ <path d="M21 12c-2.4 4 -5.4 6 -9 6c-3.6 0 -6.6 -2 -9 -6c2.4 -4 5.4 -6 9 -6c3.6 0 6.6 2 9 6" />
+ </g>,
+ tablerIconProps,
+);
+
+export const eyeClosedIcon = createIcon(
+ <g stroke="currentColor" fill="none">
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
+ <path d="M10.585 10.587a2 2 0 0 0 2.829 2.828" />
+ <path d="M16.681 16.673a8.717 8.717 0 0 1 -4.681 1.327c-3.6 0 -6.6 -2 -9 -6c1.272 -2.12 2.712 -3.678 4.32 -4.674m2.86 -1.146a9.055 9.055 0 0 1 1.82 -.18c3.6 0 6.6 2 9 6c-.666 1.11 -1.379 2.067 -2.138 2.87" />
+ <path d="M3 3l18 18" />
+ </g>,
+ tablerIconProps,
+);
diff --git a/src/constants.ts b/src/constants.ts
index 247349f645..902706201f 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -80,6 +80,7 @@ export enum EVENT {
EXCALIDRAW_LINK = "excalidraw-link",
MENU_ITEM_SELECT = "menu.itemSelect",
MESSAGE = "message",
+ FULLSCREENCHANGE = "fullscreenchange",
}
export const YOUTUBE_STATES = {
@@ -344,4 +345,33 @@ export const DEFAULT_SIDEBAR = {
defaultTab: LIBRARY_SIDEBAR_TAB,
} as const;
-export const LIBRARY_DISABLED_TYPES = new Set(["embeddable", "image"] as const);
+export const LIBRARY_DISABLED_TYPES = new Set([
+ "iframe",
+ "embeddable",
+ "image",
+] as const);
+
+// use these constants to easily identify reference sites
+export const TOOL_TYPE = {
+ selection: "selection",
+ rectangle: "rectangle",
+ diamond: "diamond",
+ ellipse: "ellipse",
+ arrow: "arrow",
+ line: "line",
+ freedraw: "freedraw",
+ text: "text",
+ image: "image",
+ eraser: "eraser",
+ hand: "hand",
+ frame: "frame",
+ magicframe: "magicframe",
+ embeddable: "embeddable",
+ laser: "laser",
+} as const;
+
+export const EDITOR_LS_KEYS = {
+ OAI_API_KEY: "excalidraw-oai-api-key",
+ // legacy naming (non)scheme
+ PUBLISH_LIBRARY: "publish-library-data",
+} as const;
diff --git a/src/css/styles.scss b/src/css/styles.scss
index a7cd9c21eb..2f690a821f 100644
--- a/src/css/styles.scss
+++ b/src/css/styles.scss
@@ -5,9 +5,11 @@
--zIndex-canvas: 1;
--zIndex-interactiveCanvas: 2;
--zIndex-wysiwyg: 3;
+ --zIndex-canvasButtons: 3;
--zIndex-layerUI: 4;
--zIndex-eyeDropperBackdrop: 5;
--zIndex-eyeDropperPreview: 6;
+ --zIndex-hyperlinkContainer: 7;
--zIndex-modal: 1000;
--zIndex-popup: 1001;
diff --git a/src/data/EditorLocalStorage.ts b/src/data/EditorLocalStorage.ts
new file mode 100644
index 0000000000..b149d5406c
--- /dev/null
+++ b/src/data/EditorLocalStorage.ts
@@ -0,0 +1,51 @@
+import { EDITOR_LS_KEYS } from "../constants";
+import { JSONValue } from "../types";
+
+export class EditorLocalStorage {
+ static has(key: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS]) {
+ try {
+ return !!window.localStorage.getItem(key);
+ } catch (error: any) {
+ console.warn(`localStorage.getItem error: ${error.message}`);
+ return false;
+ }
+ }
+
+ static get<T extends JSONValue>(
+ key: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS],
+ ) {
+ try {
+ const value = window.localStorage.getItem(key);
+ if (value) {
+ return JSON.parse(value) as T;
+ }
+ return null;
+ } catch (error: any) {
+ console.warn(`localStorage.getItem error: ${error.message}`);
+ return null;
+ }
+ }
+
+ static set = (
+ key: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS],
+ value: JSONValue,
+ ) => {
+ try {
+ window.localStorage.setItem(key, JSON.stringify(value));
+ return true;
+ } catch (error: any) {
+ console.warn(`localStorage.setItem error: ${error.message}`);
+ return false;
+ }
+ };
+
+ static delete = (
+ name: typeof EDITOR_LS_KEYS[keyof typeof EDITOR_LS_KEYS],
+ ) => {
+ try {
+ window.localStorage.removeItem(name);
+ } catch (error: any) {
+ console.warn(`localStorage.removeItem error: ${error.message}`);
+ }
+ };
+}
diff --git a/src/data/ai/types.ts b/src/data/ai/types.ts
new file mode 100644
index 0000000000..4ea03109b2
--- /dev/null
+++ b/src/data/ai/types.ts
@@ -0,0 +1,300 @@
+export namespace OpenAIInput {
+ type ChatCompletionContentPart =
+ | ChatCompletionContentPartText
+ | ChatCompletionContentPartImage;
+
+ interface ChatCompletionContentPartImage {
+ image_url: ChatCompletionContentPartImage.ImageURL;
+
+ /**
+ * The type of the content part.
+ */
+ type: "image_url";
+ }
+
+ namespace ChatCompletionContentPartImage {
+ export interface ImageURL {
+ /**
+ * Either a URL of the image or the base64 encoded image data.
+ */
+ url: string;
+
+ /**
+ * Specifies the detail level of the image.
+ */
+ detail?: "auto" | "low" | "high";
+ }
+ }
+
+ interface ChatCompletionContentPartText {
+ /**
+ * The text content.
+ */
+ text: string;
+
+ /**
+ * The type of the content part.
+ */
+ type: "text";
+ }
+
+ interface ChatCompletionUserMessageParam {
+ /**
+ * The contents of the user message.
+ */
+ content: string | Array<ChatCompletionContentPart> | null;
+
+ /**
+ * The role of the messages author, in this case `user`.
+ */
+ role: "user";
+ }
+
+ interface ChatCompletionSystemMessageParam {
+ /**
+ * The contents of the system message.
+ */
+ content: string | null;
+
+ /**
+ * The role of the messages author, in this case `system`.
+ */
+ role: "system";
+ }
+
+ export interface ChatCompletionCreateParamsBase {
+ /**
+ * A list of messages comprising the conversation so far.
+ * [Example Python code](https://cookbook.openai.com/examples/how_to_format_inputs_to_chatgpt_models).
+ */
+ messages: Array<
+ ChatCompletionUserMessageParam | ChatCompletionSystemMessageParam
+ >;
+
+ /**
+ * ID of the model to use. See the
+ * [model endpoint compatibility](https://platform.openai.com/docs/models/model-endpoint-compatibility)
+ * table for details on which models work with the Chat API.
+ */
+ model:
+ | (string & {})
+ | "gpt-4-1106-preview"
+ | "gpt-4-vision-preview"
+ | "gpt-4"
+ | "gpt-4-0314"
+ | "gpt-4-0613"
+ | "gpt-4-32k"
+ | "gpt-4-32k-0314"
+ | "gpt-4-32k-0613"
+ | "gpt-3.5-turbo"
+ | "gpt-3.5-turbo-16k"
+ | "gpt-3.5-turbo-0301"
+ | "gpt-3.5-turbo-0613"
+ | "gpt-3.5-turbo-16k-0613";
+
+ /**
+ * Number between -2.0 and 2.0. Positive values penalize new tokens based on their
+ * existing frequency in the text so far, decreasing the model's likelihood to
+ * repeat the same line verbatim.
+ *
+ * [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/gpt/parameter-details)
+ */
+ frequency_penalty?: number | null;
+
+ /**
+ * Modify the likelihood of specified tokens appearing in the completion.
+ *
+ * Accepts a JSON object that maps tokens (specified by their token ID in the
+ * tokenizer) to an associated bias value from -100 to 100. Mathematically, the
+ * bias is added to the logits generated by the model prior to sampling. The exact
+ * effect will vary per model, but values between -1 and 1 should decrease or
+ * increase likelihood of selection; values like -100 or 100 should result in a ban
+ * or exclusive selection of the relevant token.
+ */
+ logit_bias?: Record<string, number> | null;
+
+ /**
+ * The maximum number of [tokens](/tokenizer) to generate in the chat completion.
+ *
+ * The total length of input tokens and generated tokens is limited by the model's
+ * context length.
+ * [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken)
+ * for counting tokens.
+ */
+ max_tokens?: number | null;
+
+ /**
+ * How many chat completion choices to generate for each input message.
+ */
+ n?: number | null;
+
+ /**
+ * Number between -2.0 and 2.0. Positive values penalize new tokens based on
+ * whether they appear in the text so far, increasing the model's likelihood to
+ * talk about new topics.
+ *
+ * [See more information about frequency and presence penalties.](https://platform.openai.com/docs/guides/gpt/parameter-details)
+ */
+ presence_penalty?: number | null;
+
+ /**
+ * This feature is in Beta. If specified, our system will make a best effort to
+ * sample deterministically, such that repeated requests with the same `seed` and
+ * parameters should return the same result. Determinism is not guaranteed, and you
+ * should refer to the `system_fingerprint` response parameter to monitor changes
+ * in the backend.
+ */
+ seed?: number | null;
+
+ /**
+ * Up to 4 sequences where the API will stop generating further tokens.
+ */
+ stop?: string | null | Array<string>;
+
+ /**
+ * If set, partial message deltas will be sent, like in ChatGPT. Tokens will be
+ * sent as data-only
+ * [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format)
+ * as they become available, with the stream terminated by a `data: [DONE]`
+ * message.
+ * [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
+ */
+ stream?: boolean | null;
+
+ /**
+ * What sampling temperature to use, between 0 and 2. Higher values like 0.8 will
+ * make the output more random, while lower values like 0.2 will make it more
+ * focused and deterministic.
+ *
+ * We generally recommend altering this or `top_p` but not both.
+ */
+ temperature?: number | null;
+
+ /**
+ * An alternative to sampling with temperature, called nucleus sampling, where the
+ * model considers the results of the tokens with top_p probability mass. So 0.1
+ * means only the tokens comprising the top 10% probability mass are considered.
+ *
+ * We generally recommend altering this or `temperature` but not both.
+ */
+ top_p?: number | null;
+
+ /**
+ * A unique identifier representing your end-user, which can help OpenAI to monitor
+ * and detect abuse.
+ * [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
+ */
+ user?: string;
+ }
+}
+
+export namespace OpenAIOutput {
+ export interface ChatCompletion {
+ /**
+ * A unique identifier for the chat completion.
+ */
+ id: string;
+
+ /**
+ * A list of chat completion choices. Can be more than one if `n` is greater
+ * than 1.
+ */
+ choices: Array<Choice>;
+
+ /**
+ * The Unix timestamp (in seconds) of when the chat completion was created.
+ */
+ created: number;
+
+ /**
+ * The model used for the chat completion.
+ */
+ model: string;
+
+ /**
+ * The object type, which is always `chat.completion`.
+ */
+ object: "chat.completion";
+
+ /**
+ * This fingerprint represents the backend configuration that the model runs with.
+ *
+ * Can be used in conjunction with the `seed` request parameter to understand when
+ * backend changes have been made that might impact determinism.
+ */
+ system_fingerprint?: string;
+
+ /**
+ * Usage statistics for the completion request.
+ */
+ usage?: CompletionUsage;
+ }
+ export interface Choice {
+ /**
+ * The reason the model stopped generating tokens. This will be `stop` if the model
+ * hit a natural stop point or a provided stop sequence, `length` if the maximum
+ * number of tokens specified in the request was reached, `content_filter` if
+ * content was omitted due to a flag from our content filters, `tool_calls` if the
+ * model called a tool, or `function_call` (deprecated) if the model called a
+ * function.
+ */
+ finish_reason:
+ | "stop"
+ | "length"
+ | "tool_calls"
+ | "content_filter"
+ | "function_call";
+
+ /**
+ * The index of the choice in the list of choices.
+ */
+ index: number;
+
+ /**
+ * A chat completion message generated by the model.
+ */
+ message: ChatCompletionMessage;
+ }
+
+ interface ChatCompletionMessage {
+ /**
+ * The contents of the message.
+ */
+ content: string | null;
+
+ /**
+ * The role of the author of this message.
+ */
+ role: "assistant";
+ }
+
+ /**
+ * Usage statistics for the completion request.
+ */
+ interface CompletionUsage {
+ /**
+ * Number of tokens in the generated completion.
+ */
+ completion_tokens: number;
+
+ /**
+ * Number of tokens in the prompt.
+ */
+ prompt_tokens: number;
+
+ /**
+ * Total number of tokens used in the request (prompt + completion).
+ */
+ total_tokens: number;
+ }
+
+ export interface APIError {
+ readonly status: 400 | 401 | 403 | 404 | 409 | 422 | 429 | 500 | undefined;
+ readonly headers: Headers | undefined;
+ readonly error: { message: string } | undefined;
+
+ readonly code: string | null | undefined;
+ readonly param: string | null | undefined;
+ readonly type: string | undefined;
+ }
+}
diff --git a/src/data/index.ts b/src/data/index.ts
index 7554c1cbbb..3a95d4db92 100644
--- a/src/data/index.ts
+++ b/src/data/index.ts
@@ -3,10 +3,11 @@ import {
copyTextToSystemClipboard,
} from "../clipboard";
import { DEFAULT_EXPORT_PADDING, isFirefox, MIME_TYPES } from "../constants";
-import { getNonDeletedElements, isFrameElement } from "../element";
+import { getNonDeletedElements } from "../element";
+import { isFrameLikeElement } from "../element/typeChecks";
import {
ExcalidrawElement,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
NonDeletedExcalidrawElement,
} from "../element/types";
import { t } from "../i18n";
@@ -38,7 +39,7 @@ export const prepareElementsForExport = (
exportSelectionOnly &&
isSomeElementSelected(elements, { selectedElementIds });
- let exportingFrame: ExcalidrawFrameElement | null = null;
+ let exportingFrame: ExcalidrawFrameLikeElement | null = null;
let exportedElements = isExportingSelection
? getSelectedElements(
elements,
@@ -50,7 +51,10 @@ export const prepareElementsForExport = (
: elements;
if (isExportingSelection) {
- if (exportedElements.length === 1 && isFrameElement(exportedElements[0])) {
+ if (
+ exportedElements.length === 1 &&
+ isFrameLikeElement(exportedElements[0])
+ ) {
exportingFrame = exportedElements[0];
exportedElements = elementsOverlappingBBox({
elements,
@@ -93,7 +97,7 @@ export const exportCanvas = async (
viewBackgroundColor: string;
name: string;
fileHandle?: FileSystemHandle | null;
- exportingFrame: ExcalidrawFrameElement | null;
+ exportingFrame: ExcalidrawFrameLikeElement | null;
},
) => {
if (elements.length === 0) {
diff --git a/src/data/magic.ts b/src/data/magic.ts
new file mode 100644
index 0000000000..26c38ca659
--- /dev/null
+++ b/src/data/magic.ts
@@ -0,0 +1,104 @@
+import { Theme } from "../element/types";
+import { DataURL } from "../types";
+import { OpenAIInput, OpenAIOutput } from "./ai/types";
+
+export type MagicCacheData =
+ | {
+ status: "pending";
+ }
+ | { status: "done"; html: string }
+ | {
+ status: "error";
+ message?: string;
+ code: "ERR_GENERATION_INTERRUPTED" | string;
+ };
+
+const SYSTEM_PROMPT = `You are a skilled front-end developer who builds interactive prototypes from wireframes, and is an expert at CSS Grid and Flex design.
+Your role is to transform low-fidelity wireframes into working front-end HTML code.
+
+YOU MUST FOLLOW FOLLOWING RULES:
+
+- Use HTML, CSS, JavaScript to build a responsive, accessible, polished prototype
+- Leverage Tailwind for styling and layout (import as script <script src="https://cdn.tailwindcss.com"></script>)
+- Inline JavaScript when needed
+- Fetch dependencies from CDNs when needed (using unpkg or skypack)
+- Source images from Unsplash or create applicable placeholders
+- Interpret annotations as intended vs literal UI
+- Fill gaps using your expertise in UX and business logic
+- generate primarily for desktop UI, but make it responsive.
+- Use grid and flexbox wherever applicable.
+- Convert the wireframe in its entirety, don't omit elements if possible.
+
+If the wireframes, diagrams, or text is unclear or unreadable, refer to provided text for clarification.
+
+Your goal is a production-ready prototype that brings the wireframes to life.
+
+Please output JUST THE HTML file containing your best attempt at implementing the provided wireframes.`;
+
+export async function diagramToHTML({
+ image,
+ apiKey,
+ text,
+ theme = "light",
+}: {
+ image: DataURL;
+ apiKey: string;
+ text: string;
+ theme?: Theme;
+}) {
+ const body: OpenAIInput.ChatCompletionCreateParamsBase = {
+ model: "gpt-4-vision-preview",
+ // 4096 are max output tokens allowed for `gpt-4-vision-preview` currently
+ max_tokens: 4096,
+ temperature: 0.1,
+ messages: [
+ {
+ role: "system",
+ content: SYSTEM_PROMPT,
+ },
+ {
+ role: "user",
+ content: [
+ {
+ type: "image_url",
+ image_url: {
+ url: image,
+ detail: "high",
+ },
+ },
+ {
+ type: "text",
+ text: `Above is the reference wireframe. Please make a new website based on these and return just the HTML file. Also, please make it for the ${theme} theme. What follows are the wireframe's text annotations (if any)...`,
+ },
+ {
+ type: "text",
+ text,
+ },
+ ],
+ },
+ ],
+ };
+
+ let result:
+ | ({ ok: true } & OpenAIOutput.ChatCompletion)
+ | ({ ok: false } & OpenAIOutput.APIError);
+
+ const resp = await fetch("https://api.openai.com/v1/chat/completions", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${apiKey}`,
+ },
+ body: JSON.stringify(body),
+ });
+
+ if (resp.ok) {
+ const json: OpenAIOutput.ChatCompletion = await resp.json();
+ result = { ...json, ok: true };
+ } else {
+ const json: OpenAIOutput.APIError = await resp.json();
+ result = { ...json, ok: false };
+ }
+
+ return result;
+}
diff --git a/src/data/restore.ts b/src/data/restore.ts
index d9ea999ffc..76a8c32cab 100644
--- a/src/data/restore.ts
+++ b/src/data/restore.ts
@@ -1,5 +1,6 @@
import {
ExcalidrawElement,
+ ExcalidrawElementType,
ExcalidrawSelectionElement,
ExcalidrawTextElement,
FontFamilyValues,
@@ -68,6 +69,7 @@ export const AllowedExcalidrawActiveTools: Record<
embeddable: true,
hand: true,
laser: false,
+ magicframe: false,
};
export type RestoredDataState = {
@@ -111,7 +113,7 @@ const restoreElementWithProperties = <
// @ts-ignore TS complains here but type checks the call sites fine.
keyof K
> &
- Partial<Pick<ExcalidrawElement, "type" | "x" | "y">>,
+ Partial<Pick<ExcalidrawElement, "type" | "x" | "y" | "customData">>,
): T => {
const base: Pick<T, keyof ExcalidrawElement> & {
[PRECEDING_ELEMENT_KEY]?: string;
@@ -159,8 +161,9 @@ const restoreElementWithProperties = <
locked: element.locked ?? false,
};
- if ("customData" in element) {
- base.customData = element.customData;
+ if ("customData" in element || "customData" in extra) {
+ base.customData =
+ "customData" in extra ? extra.customData : element.customData;
}
if (PRECEDING_ELEMENT_KEY in element) {
@@ -273,7 +276,7 @@ const restoreElement = (
return restoreElementWithProperties(element, {
type:
- (element.type as ExcalidrawElement["type"] | "draw") === "draw"
+ (element.type as ExcalidrawElementType | "draw") === "draw"
? "line"
: element.type,
startBinding: repairBinding(element.startBinding),
@@ -289,15 +292,15 @@ const restoreElement = (
// generic elements
case "ellipse":
- return restoreElementWithProperties(element, {});
case "rectangle":
- return restoreElementWithProperties(element, {});
case "diamond":
+ case "iframe":
return restoreElementWithProperties(element, {});
case "embeddable":
return restoreElementWithProperties(element, {
validated: null,
});
+ case "magicframe":
case "frame":
return restoreElementWithProperties(element, {
name: element.name ?? null,
diff --git a/src/data/transform.ts b/src/data/transform.ts
index 954ab5f7d6..7b5286923c 100644
--- a/src/data/transform.ts
+++ b/src/data/transform.ts
@@ -15,6 +15,7 @@ import {
ElementConstructorOpts,
newFrameElement,
newImageElement,
+ newMagicFrameElement,
newTextElement,
} from "../element/newElement";
import {
@@ -26,12 +27,13 @@ import {
ExcalidrawArrowElement,
ExcalidrawBindableElement,
ExcalidrawElement,
- ExcalidrawEmbeddableElement,
ExcalidrawFrameElement,
ExcalidrawFreeDrawElement,
ExcalidrawGenericElement,
+ ExcalidrawIframeLikeElement,
ExcalidrawImageElement,
ExcalidrawLinearElement,
+ ExcalidrawMagicFrameElement,
ExcalidrawSelectionElement,
ExcalidrawTextElement,
FileId,
@@ -61,7 +63,12 @@ export type ValidLinearElement = {
| {
type: Exclude<
ExcalidrawBindableElement["type"],
- "image" | "text" | "frame" | "embeddable"
+ | "image"
+ | "text"
+ | "frame"
+ | "magicframe"
+ | "embeddable"
+ | "iframe"
>;
id?: ExcalidrawGenericElement["id"];
}
@@ -69,7 +76,12 @@ export type ValidLinearElement = {
id: ExcalidrawGenericElement["id"];
type?: Exclude<
ExcalidrawBindableElement["type"],
- "image" | "text" | "frame" | "embeddable"
+ | "image"
+ | "text"
+ | "frame"
+ | "magicframe"
+ | "embeddable"
+ | "iframe"
>;
}
)
@@ -93,7 +105,12 @@ export type ValidLinearElement = {
| {
type: Exclude<
ExcalidrawBindableElement["type"],
- "image" | "text" | "frame" | "embeddable"
+ | "image"
+ | "text"
+ | "frame"
+ | "magicframe"
+ | "embeddable"
+ | "iframe"
>;
id?: ExcalidrawGenericElement["id"];
}
@@ -101,7 +118,12 @@ export type ValidLinearElement = {
id: ExcalidrawGenericElement["id"];
type?: Exclude<
ExcalidrawBindableElement["type"],
- "image" | "text" | "frame" | "embeddable"
+ | "image"
+ | "text"
+ | "frame"
+ | "magicframe"
+ | "embeddable"
+ | "iframe"
>;
}
)
@@ -137,7 +159,7 @@ export type ValidContainer =
export type ExcalidrawElementSkeleton =
| Extract<
Exclude<ExcalidrawElement, ExcalidrawSelectionElement>,
- ExcalidrawEmbeddableElement | ExcalidrawFreeDrawElement
+ ExcalidrawIframeLikeElement | ExcalidrawFreeDrawElement
>
| ({
type: Extract<ExcalidrawLinearElement["type"], "line">;
@@ -163,7 +185,12 @@ export type ExcalidrawElementSkeleton =
type: "frame";
children: readonly ExcalidrawElement["id"][];
name?: string;
- } & Partial<ExcalidrawFrameElement>);
+ } & Partial<ExcalidrawFrameElement>)
+ | ({
+ type: "magicframe";
+ children: readonly ExcalidrawElement["id"][];
+ name?: string;
+ } & Partial<ExcalidrawMagicFrameElement>);
const DEFAULT_LINEAR_ELEMENT_PROPS = {
width: 100,
@@ -547,7 +574,16 @@ export const convertToExcalidrawElements = (
});
break;
}
+ case "magicframe": {
+ excalidrawElement = newMagicFrameElement({
+ x: 0,
+ y: 0,
+ ...element,
+ });
+ break;
+ }
case "freedraw":
+ case "iframe":
case "embeddable": {
excalidrawElement = element;
break;
@@ -656,7 +692,7 @@ export const convertToExcalidrawElements = (
// need to calculate coordinates and dimensions of frame which is possibe after all
// frame children are processed.
for (const [id, element] of elementsWithIds) {
- if (element.type !== "frame") {
+ if (element.type !== "frame" && element.type !== "magicframe") {
continue;
}
const frame = elementStore.getElement(id);
diff --git a/src/element/ElementCanvasButtons.scss b/src/element/ElementCanvasButtons.scss
new file mode 100644
index 0000000000..e69cb65211
--- /dev/null
+++ b/src/element/ElementCanvasButtons.scss
@@ -0,0 +1,14 @@
+.excalidraw {
+ .excalidraw-canvas-buttons {
+ position: absolute;
+
+ box-shadow: 0px 2px 4px 0 rgb(0 0 0 / 30%);
+ z-index: var(--zIndex-canvasButtons);
+ background: var(--island-bg-color);
+ border-radius: var(--border-radius-lg);
+
+ display: flex;
+ flex-direction: column;
+ gap: 0.375rem;
+ }
+}
diff --git a/src/element/ElementCanvasButtons.tsx b/src/element/ElementCanvasButtons.tsx
new file mode 100644
index 0000000000..99d9d55e1b
--- /dev/null
+++ b/src/element/ElementCanvasButtons.tsx
@@ -0,0 +1,60 @@
+import { AppState } from "../types";
+import { sceneCoordsToViewportCoords } from "../utils";
+import { NonDeletedExcalidrawElement } from "./types";
+import { getElementAbsoluteCoords } from ".";
+import { useExcalidrawAppState } from "../components/App";
+
+import "./ElementCanvasButtons.scss";
+
+const CONTAINER_PADDING = 5;
+
+const getContainerCoords = (
+ element: NonDeletedExcalidrawElement,
+ appState: AppState,
+) => {
+ const [x1, y1] = getElementAbsoluteCoords(element);
+ const { x: viewportX, y: viewportY } = sceneCoordsToViewportCoords(
+ { sceneX: x1 + element.width, sceneY: y1 },
+ appState,
+ );
+ const x = viewportX - appState.offsetLeft + 10;
+ const y = viewportY - appState.offsetTop;
+ return { x, y };
+};
+
+export const ElementCanvasButtons = ({
+ children,
+ element,
+}: {
+ children: React.ReactNode;
+ element: NonDeletedExcalidrawElement;
+}) => {
+ const appState = useExcalidrawAppState();
+
+ if (
+ appState.contextMenu ||
+ appState.draggingElement ||
+ appState.resizingElement ||
+ appState.isRotating ||
+ appState.openMenu ||
+ appState.viewModeEnabled
+ ) {
+ return null;
+ }
+
+ const { x, y } = getContainerCoords(element, appState);
+
+ return (
+ <div
+ className="excalidraw-canvas-buttons"
+ style={{
+ top: `${y}px`,
+ left: `${x}px`,
+ // width: CONTAINER_WIDTH,
+ padding: CONTAINER_PADDING,
+ }}
+ >
+ {children}
+ </div>
+ );
+};
diff --git a/src/element/Hyperlink.scss b/src/element/Hyperlink.scss
index b442c7d616..eeb2f46f60 100644
--- a/src/element/Hyperlink.scss
+++ b/src/element/Hyperlink.scss
@@ -6,7 +6,7 @@
justify-content: space-between;
position: absolute;
box-shadow: 0px 2px 4px 0 rgb(0 0 0 / 30%);
- z-index: 100;
+ z-index: var(--zIndex-hyperlinkContainer);
background: var(--island-bg-color);
border-radius: var(--border-radius-md);
box-sizing: border-box;
diff --git a/src/element/Hyperlink.tsx b/src/element/Hyperlink.tsx
index 0e20661c3d..caed8fe374 100644
--- a/src/element/Hyperlink.tsx
+++ b/src/element/Hyperlink.tsx
@@ -121,7 +121,7 @@ export const Hyperlink = ({
setToast({ message: embedLink.warning, closable: true });
}
const ar = embedLink
- ? embedLink.aspectRatio.w / embedLink.aspectRatio.h
+ ? embedLink.intrinsicSize.w / embedLink.intrinsicSize.h
: 1;
const hasLinkChanged =
embeddableLinkCache.get(element.id) !== element.link;
@@ -210,6 +210,7 @@ export const Hyperlink = ({
};
const { x, y } = getCoordsForPopover(element, appState);
if (
+ appState.contextMenu ||
appState.draggingElement ||
appState.resizingElement ||
appState.isRotating ||
diff --git a/src/element/collision.ts b/src/element/collision.ts
index b1fdea606b..709781b229 100644
--- a/src/element/collision.ts
+++ b/src/element/collision.ts
@@ -18,7 +18,6 @@ import {
ExcalidrawBindableElement,
ExcalidrawElement,
ExcalidrawRectangleElement,
- ExcalidrawEmbeddableElement,
ExcalidrawDiamondElement,
ExcalidrawTextElement,
ExcalidrawEllipseElement,
@@ -27,7 +26,8 @@ import {
ExcalidrawImageElement,
ExcalidrawLinearElement,
StrokeRoundness,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
+ ExcalidrawIframeLikeElement,
} from "./types";
import {
@@ -41,7 +41,8 @@ import { Drawable } from "roughjs/bin/core";
import { AppState } from "../types";
import {
hasBoundTextElement,
- isEmbeddableElement,
+ isFrameLikeElement,
+ isIframeLikeElement,
isImageElement,
} from "./typeChecks";
import { isTextElement } from ".";
@@ -64,7 +65,7 @@ const isElementDraggableFromInside = (
const isDraggableFromInside =
!isTransparent(element.backgroundColor) ||
hasBoundTextElement(element) ||
- isEmbeddableElement(element);
+ isIframeLikeElement(element);
if (element.type === "line") {
return isDraggableFromInside && isPathALoop(element.points);
}
@@ -186,7 +187,7 @@ export const isPointHittingElementBoundingBox = (
// by its frame, whether it has been selected or not
// this logic here is not ideal
// TODO: refactor it later...
- if (element.type === "frame") {
+ if (isFrameLikeElement(element)) {
return hitTestPointAgainstElement({
element,
point: [x, y],
@@ -255,6 +256,7 @@ type HitTestArgs = {
const hitTestPointAgainstElement = (args: HitTestArgs): boolean => {
switch (args.element.type) {
case "rectangle":
+ case "iframe":
case "embeddable":
case "image":
case "text":
@@ -282,7 +284,8 @@ const hitTestPointAgainstElement = (args: HitTestArgs): boolean => {
"This should not happen, we need to investigate why it does.",
);
return false;
- case "frame": {
+ case "frame":
+ case "magicframe": {
// check distance to frame element first
if (
args.check(
@@ -314,8 +317,10 @@ export const distanceToBindableElement = (
case "rectangle":
case "image":
case "text":
+ case "iframe":
case "embeddable":
case "frame":
+ case "magicframe":
return distanceToRectangle(element, point);
case "diamond":
return distanceToDiamond(element, point);
@@ -346,8 +351,8 @@ const distanceToRectangle = (
| ExcalidrawTextElement
| ExcalidrawFreeDrawElement
| ExcalidrawImageElement
- | ExcalidrawEmbeddableElement
- | ExcalidrawFrameElement,
+ | ExcalidrawIframeLikeElement
+ | ExcalidrawFrameLikeElement,
point: Point,
): number => {
const [, pointRel, hwidth, hheight] = pointRelativeToElement(element, point);
@@ -662,8 +667,10 @@ export const determineFocusDistance = (
case "rectangle":
case "image":
case "text":
+ case "iframe":
case "embeddable":
case "frame":
+ case "magicframe":
ret = c / (hwidth * (nabs + q * mabs));
break;
case "diamond":
@@ -700,8 +707,10 @@ export const determineFocusPoint = (
case "image":
case "text":
case "diamond":
+ case "iframe":
case "embeddable":
case "frame":
+ case "magicframe":
point = findFocusPointForRectangulars(element, focus, adjecentPointRel);
break;
case "ellipse":
@@ -752,8 +761,10 @@ const getSortedElementLineIntersections = (
case "image":
case "text":
case "diamond":
+ case "iframe":
case "embeddable":
case "frame":
+ case "magicframe":
const corners = getCorners(element);
intersections = corners
.flatMap((point, i) => {
@@ -788,8 +799,8 @@ const getCorners = (
| ExcalidrawImageElement
| ExcalidrawDiamondElement
| ExcalidrawTextElement
- | ExcalidrawEmbeddableElement
- | ExcalidrawFrameElement,
+ | ExcalidrawIframeLikeElement
+ | ExcalidrawFrameLikeElement,
scale: number = 1,
): GA.Point[] => {
const hx = (scale * element.width) / 2;
@@ -798,8 +809,10 @@ const getCorners = (
case "rectangle":
case "image":
case "text":
+ case "iframe":
case "embeddable":
case "frame":
+ case "magicframe":
return [
GA.point(hx, hy),
GA.point(hx, -hy),
@@ -948,8 +961,8 @@ export const findFocusPointForRectangulars = (
| ExcalidrawImageElement
| ExcalidrawDiamondElement
| ExcalidrawTextElement
- | ExcalidrawEmbeddableElement
- | ExcalidrawFrameElement,
+ | ExcalidrawIframeLikeElement
+ | ExcalidrawFrameLikeElement,
// Between -1 and 1 for how far away should the focus point be relative
// to the size of the element. Sign determines orientation.
relativeDistance: number,
diff --git a/src/element/dragElements.ts b/src/element/dragElements.ts
index 89f30bcbcb..c91ad64c67 100644
--- a/src/element/dragElements.ts
+++ b/src/element/dragElements.ts
@@ -11,7 +11,7 @@ import Scene from "../scene/Scene";
import {
isArrowElement,
isBoundToContainer,
- isFrameElement,
+ isFrameLikeElement,
} from "./typeChecks";
export const dragSelectedElements = (
@@ -33,7 +33,7 @@ export const dragSelectedElements = (
selectedElements,
);
const frames = selectedElements
- .filter((e) => isFrameElement(e))
+ .filter((e) => isFrameLikeElement(e))
.map((f) => f.id);
if (frames.length > 0) {
diff --git a/src/element/embeddable.ts b/src/element/embeddable.ts
index e03ea7a610..c129d39270 100644
--- a/src/element/embeddable.ts
+++ b/src/element/embeddable.ts
@@ -6,25 +6,19 @@ import { getFontString, updateActiveTool } from "../utils";
import { setCursorForShape } from "../cursor";
import { newTextElement } from "./newElement";
import { getContainerElement, wrapText } from "./textElement";
-import { isEmbeddableElement } from "./typeChecks";
+import {
+ isFrameLikeElement,
+ isIframeElement,
+ isIframeLikeElement,
+} from "./typeChecks";
import {
ExcalidrawElement,
- ExcalidrawEmbeddableElement,
+ ExcalidrawIframeLikeElement,
+ IframeData,
NonDeletedExcalidrawElement,
- Theme,
} from "./types";
-type EmbeddedLink =
- | ({
- aspectRatio: { w: number; h: number };
- warning?: string;
- } & (
- | { type: "video" | "generic"; link: string }
- | { type: "document"; srcdoc: (theme: Theme) => string }
- ))
- | null;
-
-const embeddedLinkCache = new Map<string, EmbeddedLink>();
+const embeddedLinkCache = new Map<string, IframeData>();
const RE_YOUTUBE =
/^(?:http(?:s)?:\/\/)?(?:www\.)?youtu(?:be\.com|\.be)\/(embed\/|watch\?v=|shorts\/|playlist\?list=|embed\/videoseries\?list=)?([a-zA-Z0-9_-]+)(?:\?t=|&t=|\?start=|&start=)?([a-zA-Z0-9_-]+)?[^\s]*$/;
@@ -67,11 +61,13 @@ const ALLOWED_DOMAINS = new Set([
"dddice.com",
]);
-const createSrcDoc = (body: string) => {
+export const createSrcDoc = (body: string) => {
return `<html><body>${body}</body></html>`;
};
-export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
+export const getEmbedLink = (
+ link: string | null | undefined,
+): IframeData | null => {
if (!link) {
return null;
}
@@ -104,8 +100,12 @@ export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
break;
}
aspectRatio = isPortrait ? { w: 315, h: 560 } : { w: 560, h: 315 };
- embeddedLinkCache.set(originalLink, { link, aspectRatio, type });
- return { link, aspectRatio, type };
+ embeddedLinkCache.set(originalLink, {
+ link,
+ intrinsicSize: aspectRatio,
+ type,
+ });
+ return { link, intrinsicSize: aspectRatio, type };
}
const vimeoLink = link.match(RE_VIMEO);
@@ -119,8 +119,12 @@ export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
aspectRatio = { w: 560, h: 315 };
//warning deliberately ommited so it is displayed only once per link
//same link next time will be served from cache
- embeddedLinkCache.set(originalLink, { link, aspectRatio, type });
- return { link, aspectRatio, type, warning };
+ embeddedLinkCache.set(originalLink, {
+ link,
+ intrinsicSize: aspectRatio,
+ type,
+ });
+ return { link, intrinsicSize: aspectRatio, type, warning };
}
const figmaLink = link.match(RE_FIGMA);
@@ -130,27 +134,35 @@ export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
link,
)}`;
aspectRatio = { w: 550, h: 550 };
- embeddedLinkCache.set(originalLink, { link, aspectRatio, type });
- return { link, aspectRatio, type };
+ embeddedLinkCache.set(originalLink, {
+ link,
+ intrinsicSize: aspectRatio,
+ type,
+ });
+ return { link, intrinsicSize: aspectRatio, type };
}
const valLink = link.match(RE_VALTOWN);
if (valLink) {
link =
valLink[1] === "embed" ? valLink[0] : valLink[0].replace("/v", "/embed");
- embeddedLinkCache.set(originalLink, { link, aspectRatio, type });
- return { link, aspectRatio, type };
+ embeddedLinkCache.set(originalLink, {
+ link,
+ intrinsicSize: aspectRatio,
+ type,
+ });
+ return { link, intrinsicSize: aspectRatio, type };
}
if (RE_TWITTER.test(link)) {
- let ret: EmbeddedLink;
+ let ret: IframeData;
// assume embed code
if (/<blockquote/.test(link)) {
const srcDoc = createSrcDoc(link);
ret = {
type: "document",
srcdoc: () => srcDoc,
- aspectRatio: { w: 480, h: 480 },
+ intrinsicSize: { w: 480, h: 480 },
};
// assume regular tweet url
} else {
@@ -160,7 +172,7 @@ export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
createSrcDoc(
`<blockquote class="twitter-tweet" data-dnt="true" data-theme="${theme}"><a href="${link}"></a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>`,
),
- aspectRatio: { w: 480, h: 480 },
+ intrinsicSize: { w: 480, h: 480 },
};
}
embeddedLinkCache.set(originalLink, ret);
@@ -168,14 +180,14 @@ export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
}
if (RE_GH_GIST.test(link)) {
- let ret: EmbeddedLink;
+ let ret: IframeData;
// assume embed code
if (/<script>/.test(link)) {
const srcDoc = createSrcDoc(link);
ret = {
type: "document",
srcdoc: () => srcDoc,
- aspectRatio: { w: 550, h: 720 },
+ intrinsicSize: { w: 550, h: 720 },
};
// assume regular url
} else {
@@ -190,26 +202,26 @@ export const getEmbedLink = (link: string | null | undefined): EmbeddedLink => {
.gist .gist-file { height: calc(100vh - 2px); padding: 0px; display: grid; grid-template-rows: 1fr auto; }
</style>
`),
- aspectRatio: { w: 550, h: 720 },
+ intrinsicSize: { w: 550, h: 720 },
};
}
embeddedLinkCache.set(link, ret);
return ret;
}
- embeddedLinkCache.set(link, { link, aspectRatio, type });
- return { link, aspectRatio, type };
+ embeddedLinkCache.set(link, { link, intrinsicSize: aspectRatio, type });
+ return { link, intrinsicSize: aspectRatio, type };
};
-export const isEmbeddableOrLabel = (
+export const isIframeLikeOrItsLabel = (
element: NonDeletedExcalidrawElement,
): Boolean => {
- if (isEmbeddableElement(element)) {
+ if (isIframeLikeElement(element)) {
return true;
}
if (element.type === "text") {
const container = getContainerElement(element);
- if (container && isEmbeddableElement(container)) {
+ if (container && isFrameLikeElement(container)) {
return true;
}
}
@@ -217,10 +229,16 @@ export const isEmbeddableOrLabel = (
};
export const createPlaceholderEmbeddableLabel = (
- element: ExcalidrawEmbeddableElement,
+ element: ExcalidrawIframeLikeElement,
): ExcalidrawElement => {
- const text =
- !element.link || element?.link === "" ? "Empty Web-Embed" : element.link;
+ let text: string;
+ if (isIframeElement(element)) {
+ text = "IFrame element";
+ } else {
+ text =
+ !element.link || element?.link === "" ? "Empty Web-Embed" : element.link;
+ }
+
const fontSize = Math.max(
Math.min(element.width / 2, element.width / text.length),
element.width / 30,
diff --git a/src/element/index.ts b/src/element/index.ts
index 72375bfea8..37d6a077b0 100644
--- a/src/element/index.ts
+++ b/src/element/index.ts
@@ -2,7 +2,6 @@ import {
ExcalidrawElement,
NonDeletedExcalidrawElement,
NonDeleted,
- ExcalidrawFrameElement,
} from "./types";
import { isInvisiblySmallElement } from "./sizeHelpers";
import { isLinearElementType } from "./typeChecks";
@@ -50,11 +49,7 @@ export {
getDragOffsetXY,
dragNewElement,
} from "./dragElements";
-export {
- isTextElement,
- isExcalidrawElement,
- isFrameElement,
-} from "./typeChecks";
+export { isTextElement, isExcalidrawElement } from "./typeChecks";
export { textWysiwyg } from "./textWysiwyg";
export { redrawTextBoundingBox } from "./textElement";
export {
@@ -74,17 +69,10 @@ export const getVisibleElements = (elements: readonly ExcalidrawElement[]) =>
(el) => !el.isDeleted && !isInvisiblySmallElement(el),
) as readonly NonDeletedExcalidrawElement[];
-export const getNonDeletedElements = (elements: readonly ExcalidrawElement[]) =>
- elements.filter(
- (element) => !element.isDeleted,
- ) as readonly NonDeletedExcalidrawElement[];
-
-export const getNonDeletedFrames = (
- frames: readonly ExcalidrawFrameElement[],
+export const getNonDeletedElements = <T extends ExcalidrawElement>(
+ elements: readonly T[],
) =>
- frames.filter(
- (frame) => !frame.isDeleted,
- ) as readonly NonDeleted<ExcalidrawFrameElement>[];
+ elements.filter((element) => !element.isDeleted) as readonly NonDeleted<T>[];
export const isNonDeletedElement = <T extends ExcalidrawElement>(
element: T,
diff --git a/src/element/newElement.ts b/src/element/newElement.ts
index 0ffd9df54f..026373f88f 100644
--- a/src/element/newElement.ts
+++ b/src/element/newElement.ts
@@ -14,6 +14,8 @@ import {
ExcalidrawTextContainer,
ExcalidrawFrameElement,
ExcalidrawEmbeddableElement,
+ ExcalidrawMagicFrameElement,
+ ExcalidrawIframeElement,
} from "../element/types";
import {
arrayToMap,
@@ -143,6 +145,16 @@ export const newEmbeddableElement = (
};
};
+export const newIframeElement = (
+ opts: {
+ type: "iframe";
+ } & ElementConstructorOpts,
+): NonDeleted<ExcalidrawIframeElement> => {
+ return {
+ ..._newElementBase<ExcalidrawIframeElement>("iframe", opts),
+ };
+};
+
export const newFrameElement = (
opts: {
name?: string;
@@ -160,6 +172,23 @@ export const newFrameElement = (
return frameElement;
};
+export const newMagicFrameElement = (
+ opts: {
+ name?: string;
+ } & ElementConstructorOpts,
+): NonDeleted<ExcalidrawMagicFrameElement> => {
+ const frameElement = newElementWith(
+ {
+ ..._newElementBase<ExcalidrawMagicFrameElement>("magicframe", opts),
+ type: "magicframe",
+ name: opts?.name || null,
+ },
+ {},
+ );
+
+ return frameElement;
+};
+
/** computes element x/y offset based on textAlign/verticalAlign */
const getTextElementPositionOffsets = (
opts: {
diff --git a/src/element/resizeElements.ts b/src/element/resizeElements.ts
index b4974848e9..883382933d 100644
--- a/src/element/resizeElements.ts
+++ b/src/element/resizeElements.ts
@@ -27,7 +27,7 @@ import {
import {
isArrowElement,
isBoundToContainer,
- isFrameElement,
+ isFrameLikeElement,
isFreeDrawElement,
isImageElement,
isLinearElement,
@@ -163,7 +163,7 @@ const rotateSingleElement = (
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
let angle: number;
- if (isFrameElement(element)) {
+ if (isFrameLikeElement(element)) {
angle = 0;
} else {
angle = (5 * Math.PI) / 2 + Math.atan2(pointerY - cy, pointerX - cx);
@@ -900,7 +900,7 @@ const rotateMultipleElements = (
}
elements
- .filter((element) => element.type !== "frame")
+ .filter((element) => !isFrameLikeElement(element))
.forEach((element) => {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element);
const cx = (x1 + x2) / 2;
diff --git a/src/element/textElement.ts b/src/element/textElement.ts
index b038294a51..e42839eaa4 100644
--- a/src/element/textElement.ts
+++ b/src/element/textElement.ts
@@ -1,6 +1,7 @@
import { getFontString, arrayToMap, isTestEnv } from "../utils";
import {
ExcalidrawElement,
+ ExcalidrawElementType,
ExcalidrawTextContainer,
ExcalidrawTextElement,
ExcalidrawTextElementWithContainer,
@@ -867,7 +868,7 @@ const VALID_CONTAINER_TYPES = new Set([
]);
export const isValidTextContainer = (element: {
- type: ExcalidrawElement["type"];
+ type: ExcalidrawElementType;
}) => VALID_CONTAINER_TYPES.has(element.type);
export const computeContainerDimensionForBoundText = (
diff --git a/src/element/transformHandles.ts b/src/element/transformHandles.ts
index 9ad49c5b01..00ebfacfd7 100644
--- a/src/element/transformHandles.ts
+++ b/src/element/transformHandles.ts
@@ -8,7 +8,7 @@ import { Bounds, getElementAbsoluteCoords } from "./bounds";
import { rotate } from "../math";
import { InteractiveCanvasAppState, Zoom } from "../types";
import { isTextElement } from ".";
-import { isFrameElement, isLinearElement } from "./typeChecks";
+import { isFrameLikeElement, isLinearElement } from "./typeChecks";
import { DEFAULT_SPACING } from "../renderer/renderScene";
export type TransformHandleDirection =
@@ -257,7 +257,7 @@ export const getTransformHandles = (
}
} else if (isTextElement(element)) {
omitSides = OMIT_SIDES_FOR_TEXT_ELEMENT;
- } else if (isFrameElement(element)) {
+ } else if (isFrameLikeElement(element)) {
omitSides = {
rotation: true,
};
diff --git a/src/element/typeChecks.ts b/src/element/typeChecks.ts
index 6d581a490d..ef1bcd3dbc 100644
--- a/src/element/typeChecks.ts
+++ b/src/element/typeChecks.ts
@@ -1,5 +1,5 @@
import { ROUNDNESS } from "../constants";
-import { AppState } from "../types";
+import { ElementOrToolType } from "../types";
import { MarkNonNullable } from "../utility-types";
import { assertNever } from "../utils";
import {
@@ -8,7 +8,6 @@ import {
ExcalidrawEmbeddableElement,
ExcalidrawLinearElement,
ExcalidrawBindableElement,
- ExcalidrawGenericElement,
ExcalidrawFreeDrawElement,
InitializedExcalidrawImageElement,
ExcalidrawImageElement,
@@ -16,21 +15,13 @@ import {
ExcalidrawTextContainer,
ExcalidrawFrameElement,
RoundnessType,
+ ExcalidrawFrameLikeElement,
+ ExcalidrawElementType,
+ ExcalidrawIframeElement,
+ ExcalidrawIframeLikeElement,
+ ExcalidrawMagicFrameElement,
} from "./types";
-export const isGenericElement = (
- element: ExcalidrawElement | null,
-): element is ExcalidrawGenericElement => {
- return (
- element != null &&
- (element.type === "selection" ||
- element.type === "rectangle" ||
- element.type === "diamond" ||
- element.type === "ellipse" ||
- element.type === "embeddable")
- );
-};
-
export const isInitializedImageElement = (
element: ExcalidrawElement | null,
): element is InitializedExcalidrawImageElement => {
@@ -49,6 +40,20 @@ export const isEmbeddableElement = (
return !!element && element.type === "embeddable";
};
+export const isIframeElement = (
+ element: ExcalidrawElement | null,
+): element is ExcalidrawIframeElement => {
+ return !!element && element.type === "iframe";
+};
+
+export const isIframeLikeElement = (
+ element: ExcalidrawElement | null,
+): element is ExcalidrawIframeLikeElement => {
+ return (
+ !!element && (element.type === "iframe" || element.type === "embeddable")
+ );
+};
+
export const isTextElement = (
element: ExcalidrawElement | null,
): element is ExcalidrawTextElement => {
@@ -61,6 +66,21 @@ export const isFrameElement = (
return element != null && element.type === "frame";
};
+export const isMagicFrameElement = (
+ element: ExcalidrawElement | null,
+): element is ExcalidrawMagicFrameElement => {
+ return element != null && element.type === "magicframe";
+};
+
+export const isFrameLikeElement = (
+ element: ExcalidrawElement | null,
+): element is ExcalidrawFrameLikeElement => {
+ return (
+ element != null &&
+ (element.type === "frame" || element.type === "magicframe")
+ );
+};
+
export const isFreeDrawElement = (
element?: ExcalidrawElement | null,
): element is ExcalidrawFreeDrawElement => {
@@ -68,7 +88,7 @@ export const isFreeDrawElement = (
};
export const isFreeDrawElementType = (
- elementType: ExcalidrawElement["type"],
+ elementType: ExcalidrawElementType,
): boolean => {
return elementType === "freedraw";
};
@@ -86,7 +106,7 @@ export const isArrowElement = (
};
export const isLinearElementType = (
- elementType: AppState["activeTool"]["type"],
+ elementType: ElementOrToolType,
): boolean => {
return (
elementType === "arrow" || elementType === "line" // || elementType === "freedraw"
@@ -105,7 +125,7 @@ export const isBindingElement = (
};
export const isBindingElementType = (
- elementType: AppState["activeTool"]["type"],
+ elementType: ElementOrToolType,
): boolean => {
return elementType === "arrow";
};
@@ -121,8 +141,10 @@ export const isBindableElement = (
element.type === "diamond" ||
element.type === "ellipse" ||
element.type === "image" ||
+ element.type === "iframe" ||
element.type === "embeddable" ||
element.type === "frame" ||
+ element.type === "magicframe" ||
(element.type === "text" && !element.containerId))
);
};
@@ -144,7 +166,7 @@ export const isTextBindableContainer = (
export const isExcalidrawElement = (
element: any,
): element is ExcalidrawElement => {
- const type: ExcalidrawElement["type"] | undefined = element?.type;
+ const type: ExcalidrawElementType | undefined = element?.type;
if (!type) {
return false;
}
@@ -152,12 +174,14 @@ export const isExcalidrawElement = (
case "text":
case "diamond":
case "rectangle":
+ case "iframe":
case "embeddable":
case "ellipse":
case "arrow":
case "freedraw":
case "line":
case "frame":
+ case "magicframe":
case "image":
case "selection": {
return true;
@@ -190,7 +214,7 @@ export const isBoundToContainer = (
};
export const isUsingAdaptiveRadius = (type: string) =>
- type === "rectangle" || type === "embeddable";
+ type === "rectangle" || type === "embeddable" || type === "iframe";
export const isUsingProportionalRadius = (type: string) =>
type === "line" || type === "arrow" || type === "diamond";
diff --git a/src/element/types.ts b/src/element/types.ts
index e8d71cae5e..b863fb429f 100644
--- a/src/element/types.ts
+++ b/src/element/types.ts
@@ -7,6 +7,7 @@ import {
VERTICAL_ALIGN,
} from "../constants";
import { MarkNonNullable, ValueOf } from "../utility-types";
+import { MagicCacheData } from "../data/magic";
export type ChartType = "bar" | "line";
export type FillStyle = "hachure" | "cross-hatch" | "solid" | "zigzag";
@@ -97,6 +98,26 @@ export type ExcalidrawEmbeddableElement = _ExcalidrawElementBase &
validated: boolean | null;
}>;
+export type ExcalidrawIframeElement = _ExcalidrawElementBase &
+ Readonly<{
+ type: "iframe";
+ // TODO move later to AI-specific frame
+ customData?: { generationData?: MagicCacheData };
+ }>;
+
+export type ExcalidrawIframeLikeElement =
+ | ExcalidrawIframeElement
+ | ExcalidrawEmbeddableElement;
+
+export type IframeData =
+ | {
+ intrinsicSize: { w: number; h: number };
+ warning?: string;
+ } & (
+ | { type: "video" | "generic"; link: string }
+ | { type: "document"; srcdoc: (theme: Theme) => string }
+ );
+
export type ExcalidrawImageElement = _ExcalidrawElementBase &
Readonly<{
type: "image";
@@ -117,6 +138,15 @@ export type ExcalidrawFrameElement = _ExcalidrawElementBase & {
name: string | null;
};
+export type ExcalidrawMagicFrameElement = _ExcalidrawElementBase & {
+ type: "magicframe";
+ name: string | null;
+};
+
+export type ExcalidrawFrameLikeElement =
+ | ExcalidrawFrameElement
+ | ExcalidrawMagicFrameElement;
+
/**
* These are elements that don't have any additional properties.
*/
@@ -138,6 +168,8 @@ export type ExcalidrawElement =
| ExcalidrawFreeDrawElement
| ExcalidrawImageElement
| ExcalidrawFrameElement
+ | ExcalidrawMagicFrameElement
+ | ExcalidrawIframeElement
| ExcalidrawEmbeddableElement;
export type NonDeleted<TElement extends ExcalidrawElement> = TElement & {
@@ -170,8 +202,10 @@ export type ExcalidrawBindableElement =
| ExcalidrawEllipseElement
| ExcalidrawTextElement
| ExcalidrawImageElement
+ | ExcalidrawIframeElement
| ExcalidrawEmbeddableElement
- | ExcalidrawFrameElement;
+ | ExcalidrawFrameElement
+ | ExcalidrawMagicFrameElement;
export type ExcalidrawTextContainer =
| ExcalidrawRectangleElement
@@ -217,3 +251,5 @@ export type ExcalidrawFreeDrawElement = _ExcalidrawElementBase &
}>;
export type FileId = string & { _brand: "FileId" };
+
+export type ExcalidrawElementType = ExcalidrawElement["type"];
diff --git a/src/frame.ts b/src/frame.ts
index eda384f654..ed14ae821a 100644
--- a/src/frame.ts
+++ b/src/frame.ts
@@ -5,7 +5,7 @@ import {
} from "./element";
import {
ExcalidrawElement,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
NonDeleted,
NonDeletedExcalidrawElement,
} from "./element/types";
@@ -18,11 +18,11 @@ import { arrayToMap } from "./utils";
import { mutateElement } from "./element/mutateElement";
import { AppClassProperties, AppState, StaticCanvasAppState } from "./types";
import { getElementsWithinSelection, getSelectedElements } from "./scene";
-import { isFrameElement } from "./element";
import { getElementsInGroup, selectGroupsFromGivenElements } from "./groups";
import Scene, { ExcalidrawElementsIncludingDeleted } from "./scene/Scene";
import { getElementLineSegments } from "./element/bounds";
import { doLineSegmentsIntersect } from "./packages/utils";
+import { isFrameElement, isFrameLikeElement } from "./element/typeChecks";
// --------------------------- Frame State ------------------------------------
export const bindElementsToFramesAfterDuplication = (
@@ -58,7 +58,7 @@ export const bindElementsToFramesAfterDuplication = (
export function isElementIntersectingFrame(
element: ExcalidrawElement,
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) {
const frameLineSegments = getElementLineSegments(frame);
@@ -75,20 +75,20 @@ export function isElementIntersectingFrame(
export const getElementsCompletelyInFrame = (
elements: readonly ExcalidrawElement[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) =>
- omitGroupsContainingFrames(
+ omitGroupsContainingFrameLikes(
getElementsWithinSelection(elements, frame, false),
).filter(
(element) =>
- (element.type !== "frame" && !element.frameId) ||
+ (!isFrameLikeElement(element) && !element.frameId) ||
element.frameId === frame.id,
);
export const isElementContainingFrame = (
elements: readonly ExcalidrawElement[],
element: ExcalidrawElement,
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
return getElementsWithinSelection(elements, element).some(
(e) => e.id === frame.id,
@@ -97,12 +97,12 @@ export const isElementContainingFrame = (
export const getElementsIntersectingFrame = (
elements: readonly ExcalidrawElement[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => elements.filter((element) => isElementIntersectingFrame(element, frame));
export const elementsAreInFrameBounds = (
elements: readonly ExcalidrawElement[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
const [selectionX1, selectionY1, selectionX2, selectionY2] =
getElementAbsoluteCoords(frame);
@@ -120,7 +120,7 @@ export const elementsAreInFrameBounds = (
export const elementOverlapsWithFrame = (
element: ExcalidrawElement,
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
return (
elementsAreInFrameBounds([element], frame) ||
@@ -134,7 +134,7 @@ export const isCursorInFrame = (
x: number;
y: number;
},
- frame: NonDeleted<ExcalidrawFrameElement>,
+ frame: NonDeleted<ExcalidrawFrameLikeElement>,
) => {
const [fx1, fy1, fx2, fy2] = getElementAbsoluteCoords(frame);
@@ -148,7 +148,7 @@ export const isCursorInFrame = (
export const groupsAreAtLeastIntersectingTheFrame = (
elements: readonly NonDeletedExcalidrawElement[],
groupIds: readonly string[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
const elementsInGroup = groupIds.flatMap((groupId) =>
getElementsInGroup(elements, groupId),
@@ -168,7 +168,7 @@ export const groupsAreAtLeastIntersectingTheFrame = (
export const groupsAreCompletelyOutOfFrame = (
elements: readonly NonDeletedExcalidrawElement[],
groupIds: readonly string[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
const elementsInGroup = groupIds.flatMap((groupId) =>
getElementsInGroup(elements, groupId),
@@ -192,14 +192,14 @@ export const groupsAreCompletelyOutOfFrame = (
/**
* Returns a map of frameId to frame elements. Includes empty frames.
*/
-export const groupByFrames = (elements: readonly ExcalidrawElement[]) => {
+export const groupByFrameLikes = (elements: readonly ExcalidrawElement[]) => {
const frameElementsMap = new Map<
ExcalidrawElement["id"],
ExcalidrawElement[]
>();
for (const element of elements) {
- const frameId = isFrameElement(element) ? element.id : element.frameId;
+ const frameId = isFrameLikeElement(element) ? element.id : element.frameId;
if (frameId && !frameElementsMap.has(frameId)) {
frameElementsMap.set(frameId, getFrameChildren(elements, frameId));
}
@@ -213,12 +213,12 @@ export const getFrameChildren = (
frameId: string,
) => allElements.filter((element) => element.frameId === frameId);
-export const getFrameElements = (
+export const getFrameLikeElements = (
allElements: ExcalidrawElementsIncludingDeleted,
-): ExcalidrawFrameElement[] => {
- return allElements.filter((element) =>
- isFrameElement(element),
- ) as ExcalidrawFrameElement[];
+): ExcalidrawFrameLikeElement[] => {
+ return allElements.filter((element): element is ExcalidrawFrameLikeElement =>
+ isFrameLikeElement(element),
+ );
};
/**
@@ -232,7 +232,7 @@ export const getFrameElements = (
export const getRootElements = (
allElements: ExcalidrawElementsIncludingDeleted,
) => {
- const frameElements = arrayToMap(getFrameElements(allElements));
+ const frameElements = arrayToMap(getFrameLikeElements(allElements));
return allElements.filter(
(element) =>
frameElements.has(element.id) ||
@@ -243,7 +243,7 @@ export const getRootElements = (
export const getElementsInResizingFrame = (
allElements: ExcalidrawElementsIncludingDeleted,
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
appState: AppState,
): ExcalidrawElement[] => {
const prevElementsInFrame = getFrameChildren(allElements, frame.id);
@@ -336,9 +336,9 @@ export const getElementsInResizingFrame = (
export const getElementsInNewFrame = (
allElements: ExcalidrawElementsIncludingDeleted,
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
- return omitGroupsContainingFrames(
+ return omitGroupsContainingFrameLikes(
allElements,
getElementsCompletelyInFrame(allElements, frame),
);
@@ -356,12 +356,12 @@ export const getContainingFrame = (
if (element.frameId) {
if (elementsMap) {
return (elementsMap.get(element.frameId) ||
- null) as null | ExcalidrawFrameElement;
+ null) as null | ExcalidrawFrameLikeElement;
}
return (
(Scene.getScene(element)?.getElement(
element.frameId,
- ) as ExcalidrawFrameElement) || null
+ ) as ExcalidrawFrameLikeElement) || null
);
}
return null;
@@ -377,7 +377,7 @@ export const getContainingFrame = (
export const addElementsToFrame = (
allElements: ExcalidrawElementsIncludingDeleted,
elementsToAdd: NonDeletedExcalidrawElement[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
) => {
const { currTargetFrameChildrenMap } = allElements.reduce(
(acc, element, index) => {
@@ -397,7 +397,7 @@ export const addElementsToFrame = (
// - add bound text elements if not already in the array
// - filter out elements that are already in the frame
- for (const element of omitGroupsContainingFrames(
+ for (const element of omitGroupsContainingFrameLikes(
allElements,
elementsToAdd,
)) {
@@ -438,7 +438,7 @@ export const removeElementsFromFrame = (
>();
const toRemoveElementsByFrame = new Map<
- ExcalidrawFrameElement["id"],
+ ExcalidrawFrameLikeElement["id"],
ExcalidrawElement[]
>();
@@ -474,7 +474,7 @@ export const removeElementsFromFrame = (
export const removeAllElementsFromFrame = (
allElements: ExcalidrawElementsIncludingDeleted,
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
appState: AppState,
) => {
const elementsInFrame = getFrameChildren(allElements, frame.id);
@@ -484,7 +484,7 @@ export const removeAllElementsFromFrame = (
export const replaceAllElementsInFrame = (
allElements: ExcalidrawElementsIncludingDeleted,
nextElementsInFrame: ExcalidrawElement[],
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
appState: AppState,
) => {
return addElementsToFrame(
@@ -524,7 +524,7 @@ export const updateFrameMembershipOfSelectedElements = (
elementsToFilter.forEach((element) => {
if (
element.frameId &&
- !isFrameElement(element) &&
+ !isFrameLikeElement(element) &&
!isElementInFrame(element, allElements, appState)
) {
elementsToRemove.add(element);
@@ -540,7 +540,7 @@ export const updateFrameMembershipOfSelectedElements = (
* filters out elements that are inside groups that contain a frame element
* anywhere in the group tree
*/
-export const omitGroupsContainingFrames = (
+export const omitGroupsContainingFrameLikes = (
allElements: ExcalidrawElementsIncludingDeleted,
/** subset of elements you want to filter. Optional perf optimization so we
* don't have to filter all elements unnecessarily
@@ -558,7 +558,9 @@ export const omitGroupsContainingFrames = (
const rejectedGroupIds = new Set<string>();
for (const groupId of uniqueGroupIds) {
if (
- getElementsInGroup(allElements, groupId).some((el) => isFrameElement(el))
+ getElementsInGroup(allElements, groupId).some((el) =>
+ isFrameLikeElement(el),
+ )
) {
rejectedGroupIds.add(groupId);
}
@@ -636,7 +638,7 @@ export const isElementInFrame = (
}
for (const elementInGroup of allElementsInGroup) {
- if (isFrameElement(elementInGroup)) {
+ if (isFrameLikeElement(elementInGroup)) {
return false;
}
}
@@ -650,3 +652,15 @@ export const isElementInFrame = (
return false;
};
+
+export const getFrameLikeTitle = (
+ element: ExcalidrawFrameLikeElement,
+ frameIdx: number,
+) => {
+ const existingName = element.name?.trim();
+ if (existingName) {
+ return existingName;
+ }
+ // TODO name frames AI only is specific to AI frames
+ return isFrameElement(element) ? `Frame ${frameIdx}` : `AI Frame ${frameIdx}`;
+};
diff --git a/src/locales/en.json b/src/locales/en.json
index 3b4eba6de9..9855e4294c 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -11,6 +11,8 @@
"copyAsPng": "Copy to clipboard as PNG",
"copyAsSvg": "Copy to clipboard as SVG",
"copyText": "Copy to clipboard as text",
+ "copySource": "Copy source to clipboard",
+ "convertToCode": "Convert to code",
"bringForward": "Bring forward",
"sendToBack": "Send to back",
"bringToFront": "Bring to front",
@@ -218,6 +220,7 @@
},
"libraryElementTypeError": {
"embeddable": "Embeddable elements cannot be added to the library.",
+ "iframe": "IFrame elements cannot be added to the library.",
"image": "Support for adding images to the library coming soon!"
},
"asyncPasteFailedOnRead": "Couldn't paste (couldn't read from system clipboard).",
@@ -240,11 +243,13 @@
"link": "Add/ Update link for a selected shape",
"eraser": "Eraser",
"frame": "Frame tool",
+ "magicframe": "Wireframe to code",
"embeddable": "Web Embed",
"laser": "Laser pointer",
"hand": "Hand (panning tool)",
"extraTools": "More tools",
- "mermaidToExcalidraw": "Mermaid to Excalidraw"
+ "mermaidToExcalidraw": "Mermaid to Excalidraw",
+ "magicSettings": "AI settings"
},
"headings": {
"canvasActions": "Canvas actions",
diff --git a/src/packages/excalidraw/index.tsx b/src/packages/excalidraw/index.tsx
index c4b58b5c09..bf82817aea 100644
--- a/src/packages/excalidraw/index.tsx
+++ b/src/packages/excalidraw/index.tsx
@@ -44,6 +44,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
children,
validateEmbeddable,
renderEmbeddable,
+ aiEnabled,
} = props;
const canvasActions = props.UIOptions?.canvasActions;
@@ -122,6 +123,7 @@ const ExcalidrawBase = (props: ExcalidrawProps) => {
onScrollChange={onScrollChange}
validateEmbeddable={validateEmbeddable}
renderEmbeddable={renderEmbeddable}
+ aiEnabled={aiEnabled !== false}
>
{children}
</App>
diff --git a/src/packages/utils.ts b/src/packages/utils.ts
index b23cab3263..d021f9ed37 100644
--- a/src/packages/utils.ts
+++ b/src/packages/utils.ts
@@ -6,7 +6,7 @@ import { getDefaultAppState } from "../appState";
import { AppState, BinaryFiles } from "../types";
import {
ExcalidrawElement,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
NonDeleted,
} from "../element/types";
import { restore } from "../data/restore";
@@ -26,7 +26,7 @@ type ExportOpts = {
appState?: Partial<Omit<AppState, "offsetTop" | "offsetLeft">>;
files: BinaryFiles | null;
maxWidthOrHeight?: number;
- exportingFrame?: ExcalidrawFrameElement | null;
+ exportingFrame?: ExcalidrawFrameLikeElement | null;
getDimensions?: (
width: number,
height: number,
diff --git a/src/renderer/renderElement.ts b/src/renderer/renderElement.ts
index d346fa6ab6..24eaf64b80 100644
--- a/src/renderer/renderElement.ts
+++ b/src/renderer/renderElement.ts
@@ -13,6 +13,7 @@ import {
isInitializedImageElement,
isArrowElement,
hasBoundTextElement,
+ isMagicFrameElement,
} from "../element/typeChecks";
import { getElementAbsoluteCoords } from "../element/bounds";
import type { RoughCanvas } from "roughjs/bin/canvas";
@@ -272,6 +273,7 @@ const drawElementOnCanvas = (
((getContainingFrame(element)?.opacity ?? 100) * element.opacity) / 10000;
switch (element.type) {
case "rectangle":
+ case "iframe":
case "embeddable":
case "diamond":
case "ellipse": {
@@ -594,6 +596,7 @@ export const renderElement = (
appState: StaticCanvasAppState,
) => {
switch (element.type) {
+ case "magicframe":
case "frame": {
if (appState.frameRendering.enabled && appState.frameRendering.outline) {
context.save();
@@ -606,6 +609,12 @@ export const renderElement = (
context.lineWidth = FRAME_STYLE.strokeWidth / appState.zoom.value;
context.strokeStyle = FRAME_STYLE.strokeColor;
+ // TODO change later to only affect AI frames
+ if (isMagicFrameElement(element)) {
+ context.strokeStyle =
+ appState.theme === "light" ? "#7affd7" : "#1d8264";
+ }
+
if (FRAME_STYLE.radius && context.roundRect) {
context.beginPath();
context.roundRect(
@@ -666,6 +675,7 @@ export const renderElement = (
case "arrow":
case "image":
case "text":
+ case "iframe":
case "embeddable": {
// TODO investigate if we can do this in situ. Right now we need to call
// beforehand because math helpers (such as getElementAbsoluteCoords)
@@ -951,6 +961,7 @@ export const renderElementToSvg = (
addToRoot(g || node, element);
break;
}
+ case "iframe":
case "embeddable": {
// render placeholder rectangle
const shape = ShapeCache.generateElementShape(element, true);
@@ -1252,7 +1263,8 @@ export const renderElementToSvg = (
break;
}
// frames are not rendered and only acts as a container
- case "frame": {
+ case "frame":
+ case "magicframe": {
if (
renderConfig.frameRendering.enabled &&
renderConfig.frameRendering.outline
diff --git a/src/renderer/renderScene.ts b/src/renderer/renderScene.ts
index 2a183777bb..671e90d062 100644
--- a/src/renderer/renderScene.ts
+++ b/src/renderer/renderScene.ts
@@ -16,7 +16,7 @@ import {
NonDeleted,
GroupId,
ExcalidrawBindableElement,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
} from "../element/types";
import {
getElementAbsoluteCoords,
@@ -70,11 +70,12 @@ import {
import { renderSnaps } from "./renderSnaps";
import {
isEmbeddableElement,
- isFrameElement,
+ isFrameLikeElement,
+ isIframeLikeElement,
isLinearElement,
} from "../element/typeChecks";
import {
- isEmbeddableOrLabel,
+ isIframeLikeOrItsLabel,
createPlaceholderEmbeddableLabel,
} from "../element/embeddable";
import {
@@ -362,7 +363,7 @@ const renderLinearElementPointHighlight = (
};
const frameClip = (
- frame: ExcalidrawFrameElement,
+ frame: ExcalidrawFrameLikeElement,
context: CanvasRenderingContext2D,
renderConfig: StaticCanvasRenderConfig,
appState: StaticCanvasAppState,
@@ -515,7 +516,7 @@ const _renderInteractiveScene = ({
}
const isFrameSelected = selectedElements.some((element) =>
- isFrameElement(element),
+ isFrameLikeElement(element),
);
// Getting the element using LinearElementEditor during collab mismatches version - being one head of visible elements due to
@@ -963,7 +964,7 @@ const _renderStaticScene = ({
// Paint visible elements
visibleElements
- .filter((el) => !isEmbeddableOrLabel(el))
+ .filter((el) => !isIframeLikeOrItsLabel(el))
.forEach((element) => {
try {
const frameId = element.frameId || appState.frameToHighlight?.id;
@@ -996,15 +997,16 @@ const _renderStaticScene = ({
// render embeddables on top
visibleElements
- .filter((el) => isEmbeddableOrLabel(el))
+ .filter((el) => isIframeLikeOrItsLabel(el))
.forEach((element) => {
try {
const render = () => {
renderElement(element, rc, context, renderConfig, appState);
if (
- isEmbeddableElement(element) &&
- (isExporting || !element.validated) &&
+ isIframeLikeElement(element) &&
+ (isExporting ||
+ (isEmbeddableElement(element) && !element.validated)) &&
element.width &&
element.height
) {
@@ -1242,8 +1244,10 @@ const renderBindingHighlightForBindableElement = (
case "rectangle":
case "text":
case "image":
+ case "iframe":
case "embeddable":
case "frame":
+ case "magicframe":
strokeRectWithRotation(
context,
x1 - padding,
@@ -1284,7 +1288,7 @@ const renderBindingHighlightForBindableElement = (
const renderFrameHighlight = (
context: CanvasRenderingContext2D,
appState: InteractiveCanvasAppState,
- frame: NonDeleted<ExcalidrawFrameElement>,
+ frame: NonDeleted<ExcalidrawFrameLikeElement>,
) => {
const [x1, y1, x2, y2] = getElementAbsoluteCoords(frame);
const width = x2 - x1;
@@ -1469,7 +1473,7 @@ export const renderSceneToSvg = (
};
// render elements
elements
- .filter((el) => !isEmbeddableOrLabel(el))
+ .filter((el) => !isIframeLikeOrItsLabel(el))
.forEach((element) => {
if (!element.isDeleted) {
try {
@@ -1490,7 +1494,7 @@ export const renderSceneToSvg = (
// render embeddables on top
elements
- .filter((el) => isEmbeddableElement(el))
+ .filter((el) => isIframeLikeElement(el))
.forEach((element) => {
if (!element.isDeleted) {
try {
diff --git a/src/scene/Scene.ts b/src/scene/Scene.ts
index 9c1b3d3298..814638e7e3 100644
--- a/src/scene/Scene.ts
+++ b/src/scene/Scene.ts
@@ -2,15 +2,11 @@ import {
ExcalidrawElement,
NonDeletedExcalidrawElement,
NonDeleted,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
} from "../element/types";
-import {
- getNonDeletedElements,
- getNonDeletedFrames,
- isNonDeletedElement,
-} from "../element";
+import { getNonDeletedElements, isNonDeletedElement } from "../element";
import { LinearElementEditor } from "../element/linearElementEditor";
-import { isFrameElement } from "../element/typeChecks";
+import { isFrameLikeElement } from "../element/typeChecks";
import { getSelectedElements } from "./selection";
import { AppState } from "../types";
import { Assert, SameType } from "../utility-types";
@@ -107,8 +103,9 @@ class Scene {
private nonDeletedElements: readonly NonDeletedExcalidrawElement[] = [];
private elements: readonly ExcalidrawElement[] = [];
- private nonDeletedFrames: readonly NonDeleted<ExcalidrawFrameElement>[] = [];
- private frames: readonly ExcalidrawFrameElement[] = [];
+ private nonDeletedFramesLikes: readonly NonDeleted<ExcalidrawFrameLikeElement>[] =
+ [];
+ private frames: readonly ExcalidrawFrameLikeElement[] = [];
private elementsMap = new Map<ExcalidrawElement["id"], ExcalidrawElement>();
private selectedElementsCache: {
selectedElementIds: AppState["selectedElementIds"] | null;
@@ -179,8 +176,8 @@ class Scene {
return selectedElements;
}
- getNonDeletedFrames(): readonly NonDeleted<ExcalidrawFrameElement>[] {
- return this.nonDeletedFrames;
+ getNonDeletedFramesLikes(): readonly NonDeleted<ExcalidrawFrameLikeElement>[] {
+ return this.nonDeletedFramesLikes;
}
getElement<T extends ExcalidrawElement>(id: T["id"]): T | null {
@@ -235,18 +232,18 @@ class Scene {
mapElementIds = true,
) {
this.elements = nextElements;
- const nextFrames: ExcalidrawFrameElement[] = [];
+ const nextFrameLikes: ExcalidrawFrameLikeElement[] = [];
this.elementsMap.clear();
nextElements.forEach((element) => {
- if (isFrameElement(element)) {
- nextFrames.push(element);
+ if (isFrameLikeElement(element)) {
+ nextFrameLikes.push(element);
}
this.elementsMap.set(element.id, element);
Scene.mapElementToScene(element, this);
});
this.nonDeletedElements = getNonDeletedElements(this.elements);
- this.frames = nextFrames;
- this.nonDeletedFrames = getNonDeletedFrames(this.frames);
+ this.frames = nextFrameLikes;
+ this.nonDeletedFramesLikes = getNonDeletedElements(this.frames);
this.informMutation();
}
@@ -277,7 +274,7 @@ class Scene {
destroy() {
this.nonDeletedElements = [];
this.elements = [];
- this.nonDeletedFrames = [];
+ this.nonDeletedFramesLikes = [];
this.frames = [];
this.elementsMap.clear();
this.selectedElementsCache.selectedElementIds = null;
diff --git a/src/scene/Shape.ts b/src/scene/Shape.ts
index cf4681542f..4d928e9490 100644
--- a/src/scene/Shape.ts
+++ b/src/scene/Shape.ts
@@ -14,7 +14,12 @@ import { generateFreeDrawShape } from "../renderer/renderElement";
import { isTransparent, assertNever } from "../utils";
import { simplify } from "points-on-curve";
import { ROUGHNESS } from "../constants";
-import { isLinearElement } from "../element/typeChecks";
+import {
+ isEmbeddableElement,
+ isIframeElement,
+ isIframeLikeElement,
+ isLinearElement,
+} from "../element/typeChecks";
import { canChangeRoundness } from "./comparisons";
const getDashArrayDashed = (strokeWidth: number) => [8, 8 + strokeWidth];
@@ -78,6 +83,7 @@ export const generateRoughOptions = (
switch (element.type) {
case "rectangle":
+ case "iframe":
case "embeddable":
case "diamond":
case "ellipse": {
@@ -109,13 +115,13 @@ export const generateRoughOptions = (
}
};
-const modifyEmbeddableForRoughOptions = (
+const modifyIframeLikeForRoughOptions = (
element: NonDeletedExcalidrawElement,
isExporting: boolean,
) => {
if (
- element.type === "embeddable" &&
- (isExporting || !element.validated) &&
+ isIframeLikeElement(element) &&
+ (isExporting || (isEmbeddableElement(element) && !element.validated)) &&
isTransparent(element.backgroundColor) &&
isTransparent(element.strokeColor)
) {
@@ -125,6 +131,16 @@ const modifyEmbeddableForRoughOptions = (
backgroundColor: "#d3d3d3",
fillStyle: "solid",
} as const;
+ } else if (isIframeElement(element)) {
+ return {
+ ...element,
+ strokeColor: isTransparent(element.strokeColor)
+ ? "#000000"
+ : element.strokeColor,
+ backgroundColor: isTransparent(element.backgroundColor)
+ ? "#f4f4f6"
+ : element.backgroundColor,
+ };
}
return element;
};
@@ -143,6 +159,7 @@ export const _generateElementShape = (
): Drawable | Drawable[] | null => {
switch (element.type) {
case "rectangle":
+ case "iframe":
case "embeddable": {
let shape: ElementShapes[typeof element.type];
// this is for rendering the stroke/bg of the embeddable, especially
@@ -159,7 +176,7 @@ export const _generateElementShape = (
h - r
} L 0 ${r} Q 0 0, ${r} 0`,
generateRoughOptions(
- modifyEmbeddableForRoughOptions(element, isExporting),
+ modifyIframeLikeForRoughOptions(element, isExporting),
true,
),
);
@@ -170,7 +187,7 @@ export const _generateElementShape = (
element.width,
element.height,
generateRoughOptions(
- modifyEmbeddableForRoughOptions(element, isExporting),
+ modifyIframeLikeForRoughOptions(element, isExporting),
false,
),
);
@@ -373,6 +390,7 @@ export const _generateElementShape = (
return shape;
}
case "frame":
+ case "magicframe":
case "text":
case "image": {
const shape: ElementShapes[typeof element.type] = null;
diff --git a/src/scene/comparisons.ts b/src/scene/comparisons.ts
index a6a2302438..551aa2e6e5 100644
--- a/src/scene/comparisons.ts
+++ b/src/scene/comparisons.ts
@@ -1,22 +1,25 @@
-import { isEmbeddableElement } from "../element/typeChecks";
+import { isIframeElement } from "../element/typeChecks";
import {
- ExcalidrawEmbeddableElement,
+ ExcalidrawIframeElement,
NonDeletedExcalidrawElement,
} from "../element/types";
+import { ElementOrToolType } from "../types";
-export const hasBackground = (type: string) =>
+export const hasBackground = (type: ElementOrToolType) =>
type === "rectangle" ||
+ type === "iframe" ||
type === "embeddable" ||
type === "ellipse" ||
type === "diamond" ||
type === "line" ||
type === "freedraw";
-export const hasStrokeColor = (type: string) =>
- type !== "image" && type !== "frame";
+export const hasStrokeColor = (type: ElementOrToolType) =>
+ type !== "image" && type !== "frame" && type !== "magicframe";
-export const hasStrokeWidth = (type: string) =>
+export const hasStrokeWidth = (type: ElementOrToolType) =>
type === "rectangle" ||
+ type === "iframe" ||
type === "embeddable" ||
type === "ellipse" ||
type === "diamond" ||
@@ -24,22 +27,24 @@ export const hasStrokeWidth = (type: string) =>
type === "arrow" ||
type === "line";
-export const hasStrokeStyle = (type: string) =>
+export const hasStrokeStyle = (type: ElementOrToolType) =>
type === "rectangle" ||
+ type === "iframe" ||
type === "embeddable" ||
type === "ellipse" ||
type === "diamond" ||
type === "arrow" ||
type === "line";
-export const canChangeRoundness = (type: string) =>
+export const canChangeRoundness = (type: ElementOrToolType) =>
type === "rectangle" ||
+ type === "iframe" ||
type === "embeddable" ||
type === "arrow" ||
type === "line" ||
type === "diamond";
-export const canHaveArrowheads = (type: string) => type === "arrow";
+export const canHaveArrowheads = (type: ElementOrToolType) => type === "arrow";
export const getElementAtPosition = (
elements: readonly NonDeletedExcalidrawElement[],
@@ -67,7 +72,7 @@ export const getElementsAtPosition = (
elements: readonly NonDeletedExcalidrawElement[],
isAtPositionFn: (element: NonDeletedExcalidrawElement) => boolean,
) => {
- const embeddables: ExcalidrawEmbeddableElement[] = [];
+ const iframeLikes: ExcalidrawIframeElement[] = [];
// The parameter elements comes ordered from lower z-index to higher.
// We want to preserve that order on the returned array.
// Exception being embeddables which should be on top of everything else in
@@ -75,13 +80,13 @@ export const getElementsAtPosition = (
const elsAtPos = elements.filter((element) => {
const hit = !element.isDeleted && isAtPositionFn(element);
if (hit) {
- if (isEmbeddableElement(element)) {
- embeddables.push(element);
+ if (isIframeElement(element)) {
+ iframeLikes.push(element);
return false;
}
return true;
}
return false;
});
- return elsAtPos.concat(embeddables);
+ return elsAtPos.concat(iframeLikes);
};
diff --git a/src/scene/export.ts b/src/scene/export.ts
index 26fe7ff436..54ede380c4 100644
--- a/src/scene/export.ts
+++ b/src/scene/export.ts
@@ -1,7 +1,7 @@
import rough from "roughjs/bin/rough";
import {
ExcalidrawElement,
- ExcalidrawFrameElement,
+ ExcalidrawFrameLikeElement,
ExcalidrawTextElement,
NonDeletedExcalidrawElement,
} from "../element/types";
@@ -27,11 +27,16 @@ import {
updateImageCache,
} from "../element/image";
import { elementsOverlappingBBox } from "../packages/withinBounds";
-import { getFrameElements, getRootElements } from "../frame";
-import { isFrameElement, newTextElement } from "../element";
+import {
+ getFrameLikeElements,
+ getFrameLikeTitle,
+ getRootElements,
+} from "../frame";
+import { newTextElement } from "../element";
import { Mutable } from "../utility-types";
import { newElementWith } from "../element/mutateElement";
import Scene from "./Scene";
+import { isFrameElement, isFrameLikeElement } from "../element/typeChecks";
const SVG_EXPORT_TAG = `<!-- svg-source:excalidraw -->`;
@@ -100,10 +105,15 @@ const addFrameLabelsAsTextElements = (
opts: Pick<AppState, "exportWithDarkMode">,
) => {
const nextElements: NonDeletedExcalidrawElement[] = [];
- let frameIdx = 0;
+ let frameIndex = 0;
+ let magicFrameIndex = 0;
for (const element of elements) {
- if (isFrameElement(element)) {
- frameIdx++;
+ if (isFrameLikeElement(element)) {
+ if (isFrameElement(element)) {
+ frameIndex++;
+ } else {
+ magicFrameIndex++;
+ }
let textElement: Mutable<ExcalidrawTextElement> = newTextElement({
x: element.x,
y: element.y - FRAME_STYLE.nameOffsetY,
@@ -114,7 +124,10 @@ const addFrameLabelsAsTextElements = (
strokeColor: opts.exportWithDarkMode
? FRAME_STYLE.nameColorDarkTheme
: FRAME_STYLE.nameColorLightTheme,
- text: element.name || `Frame ${frameIdx}`,
+ text: getFrameLikeTitle(
+ element,
+ isFrameElement(element) ? frameIndex : magicFrameIndex,
+ ),
});
textElement.y -= textElement.height;
@@ -129,7 +142,7 @@ const addFrameLabelsAsTextElements = (
};
const getFrameRenderingConfig = (
- exportingFrame: ExcalidrawFrameElement | null,
+ exportingFrame: ExcalidrawFrameLikeElement | null,
frameRendering: AppState["frameRendering"] | null,
): AppState["frameRendering"] => {
frameRendering = frameRendering || getDefaultAppState().frameRendering;
@@ -148,7 +161,7 @@ const prepareElementsForRender = ({
exportWithDarkMode,
}: {
elements: readonly ExcalidrawElement[];
- exportingFrame: ExcalidrawFrameElement | null | undefined;
+ exportingFrame: ExcalidrawFrameLikeElement | null | undefined;
frameRendering: AppState["frameRendering"];
exportWithDarkMode: AppState["exportWithDarkMode"];
}) => {
@@ -184,7 +197,7 @@ export const exportToCanvas = async (
exportBackground: boolean;
exportPadding?: number;
viewBackgroundColor: string;
- exportingFrame?: ExcalidrawFrameElement | null;
+ exportingFrame?: ExcalidrawFrameLikeElement | null;
},
createCanvas: (
width: number,
@@ -274,7 +287,7 @@ export const exportToSvg = async (
files: BinaryFiles | null,
opts?: {
renderEmbeddables?: boolean;
- exportingFrame?: ExcalidrawFrameElement | null;
+ exportingFrame?: ExcalidrawFrameLikeElement | null;
},
): Promise<SVGSVGElement> => {
const tempScene = __createSceneForElementsHack__(elements);
@@ -360,7 +373,7 @@ export const exportToSvg = async (
const offsetX = -minX + exportPadding;
const offsetY = -minY + exportPadding;
- const frameElements = getFrameElements(elements);
+ const frameElements = getFrameLikeElements(elements);
let exportingFrameClipPath = "";
for (const frame of frameElements) {
diff --git a/src/scene/selection.ts b/src/scene/selection.ts
index 510dd9ef59..7a620155f5 100644
--- a/src/scene/selection.ts
+++ b/src/scene/selection.ts
@@ -4,7 +4,7 @@ import {
} from "../element/types";
import { getElementAbsoluteCoords, getElementBounds } from "../element";
import { AppState, InteractiveCanvasAppState } from "../types";
-import { isBoundToContainer } from "../element/typeChecks";
+import { isBoundToContainer, isFrameLikeElement } from "../element/typeChecks";
import {
elementOverlapsWithFrame,
getContainingFrame,
@@ -27,7 +27,7 @@ export const excludeElementsInFramesFromSelection = <
const framesInSelection = new Set<T["id"]>();
selectedElements.forEach((element) => {
- if (element.type === "frame") {
+ if (isFrameLikeElement(element)) {
framesInSelection.add(element.id);
}
});
@@ -190,7 +190,7 @@ export const getSelectedElements = (
if (opts?.includeElementsInFrames) {
const elementsToInclude: ExcalidrawElement[] = [];
selectedElements.forEach((element) => {
- if (element.type === "frame") {
+ if (isFrameLikeElement(element)) {
getFrameChildren(elements, element.id).forEach((e) =>
elementsToInclude.push(e),
);
diff --git a/src/scene/types.ts b/src/scene/types.ts
index f1c861a9f7..dc709a22ae 100644
--- a/src/scene/types.ts
+++ b/src/scene/types.ts
@@ -98,6 +98,7 @@ export type ElementShapes = {
rectangle: Drawable;
ellipse: Drawable;
diamond: Drawable;
+ iframe: Drawable;
embeddable: Drawable;
freedraw: Drawable | null;
arrow: Drawable[];
@@ -105,4 +106,5 @@ export type ElementShapes = {
text: null;
image: null;
frame: null;
+ magicframe: null;
};
diff --git a/src/shapes.tsx b/src/shapes.tsx
index bb827995be..a8b9b53012 100644
--- a/src/shapes.tsx
+++ b/src/shapes.tsx
@@ -83,14 +83,6 @@ export const SHAPES = [
numericKey: KEYS["0"],
fillable: false,
},
- // TODO: frame, create icon and set up numeric key
- // {
- // icon: RectangleIcon,
- // value: "frame",
- // key: KEYS.F,
- // numericKey: KEYS.SUBTRACT,
- // fillable: false,
- // },
] as const;
export const findShapeByKey = (key: string) => {
diff --git a/src/snapping.ts b/src/snapping.ts
index d3617e8024..e7ff9b7876 100644
--- a/src/snapping.ts
+++ b/src/snapping.ts
@@ -1,3 +1,4 @@
+import { TOOL_TYPE } from "./constants";
import {
Bounds,
getCommonBounds,
@@ -5,7 +6,7 @@ import {
getElementAbsoluteCoords,
} from "./element/bounds";
import { MaybeTransformHandleType } from "./element/transformHandles";
-import { isBoundToContainer, isFrameElement } from "./element/typeChecks";
+import { isBoundToContainer, isFrameLikeElement } from "./element/typeChecks";
import {
ExcalidrawElement,
NonDeletedExcalidrawElement,
@@ -262,7 +263,7 @@ const getReferenceElements = (
appState: AppState,
) => {
const selectedFrames = selectedElements
- .filter((element) => isFrameElement(element))
+ .filter((element) => isFrameLikeElement(element))
.map((frame) => frame.id);
return getVisibleAndNonSelectedElements(
@@ -1352,10 +1353,11 @@ export const isActiveToolNonLinearSnappable = (
activeToolType: AppState["activeTool"]["type"],
) => {
return (
- activeToolType === "rectangle" ||
- activeToolType === "ellipse" ||
- activeToolType === "diamond" ||
- activeToolType === "frame" ||
- activeToolType === "image"
+ activeToolType === TOOL_TYPE.rectangle ||
+ activeToolType === TOOL_TYPE.ellipse ||
+ activeToolType === TOOL_TYPE.diamond ||
+ activeToolType === TOOL_TYPE.frame ||
+ activeToolType === TOOL_TYPE.magicframe ||
+ activeToolType === TOOL_TYPE.image
);
};
diff --git a/src/tests/helpers/api.ts b/src/tests/helpers/api.ts
index cdf95de22f..2a18805ab0 100644
--- a/src/tests/helpers/api.ts
+++ b/src/tests/helpers/api.ts
@@ -7,6 +7,8 @@ import {
ExcalidrawImageElement,
FileId,
ExcalidrawFrameElement,
+ ExcalidrawElementType,
+ ExcalidrawMagicFrameElement,
} from "../../element/types";
import { newElement, newTextElement, newLinearElement } from "../../element";
import { DEFAULT_VERTICAL_ALIGN, ROUNDNESS } from "../../constants";
@@ -20,7 +22,9 @@ import {
newEmbeddableElement,
newFrameElement,
newFreeDrawElement,
+ newIframeElement,
newImageElement,
+ newMagicFrameElement,
} from "../../element/newElement";
import { Point } from "../../types";
import { getSelectedElements } from "../../scene/selection";
@@ -74,7 +78,7 @@ export class API {
};
static createElement = <
- T extends Exclude<ExcalidrawElement["type"], "selection"> = "rectangle",
+ T extends Exclude<ExcalidrawElementType, "selection"> = "rectangle",
>({
// @ts-ignore
type = "rectangle",
@@ -139,6 +143,8 @@ export class API {
? ExcalidrawImageElement
: T extends "frame"
? ExcalidrawFrameElement
+ : T extends "magicframe"
+ ? ExcalidrawMagicFrameElement
: ExcalidrawGenericElement => {
let element: Mutable<ExcalidrawElement> = null!;
@@ -202,6 +208,12 @@ export class API {
validated: null,
});
break;
+ case "iframe":
+ element = newIframeElement({
+ type: "iframe",
+ ...base,
+ });
+ break;
case "text":
const fontSize = rest.fontSize ?? appState.currentItemFontSize;
const fontFamily = rest.fontFamily ?? appState.currentItemFontFamily;
@@ -253,6 +265,9 @@ export class API {
case "frame":
element = newFrameElement({ ...base, width, height });
break;
+ case "magicframe":
+ element = newMagicFrameElement({ ...base, width, height });
+ break;
default:
assertNever(
type,
diff --git a/src/tests/helpers/ui.ts b/src/tests/helpers/ui.ts
index 5ccf05069b..f37ac00194 100644
--- a/src/tests/helpers/ui.ts
+++ b/src/tests/helpers/ui.ts
@@ -1,4 +1,4 @@
-import type { Point } from "../../types";
+import type { Point, ToolType } from "../../types";
import type {
ExcalidrawElement,
ExcalidrawLinearElement,
@@ -20,15 +20,14 @@ import {
type TransformHandleDirection,
} from "../../element/transformHandles";
import { KEYS } from "../../keys";
-import { type ToolName } from "../queries/toolQueries";
import { fireEvent, GlobalTestState, screen } from "../test-utils";
import { mutateElement } from "../../element/mutateElement";
import { API } from "./api";
import {
- isFrameElement,
isLinearElement,
isFreeDrawElement,
isTextElement,
+ isFrameLikeElement,
} from "../../element/typeChecks";
import { getCommonBounds, getElementPointsCoords } from "../../element/bounds";
import { rotatePoint } from "../../math";
@@ -290,7 +289,7 @@ const transform = (
];
} else {
const [x1, y1, x2, y2] = getCommonBounds(elements);
- const isFrameSelected = elements.some(isFrameElement);
+ const isFrameSelected = elements.some(isFrameLikeElement);
const transformHandles = getTransformHandlesFromCoords(
[x1, y1, x2, y2, (x1 + x2) / 2, (y1 + y2) / 2],
0,
@@ -345,7 +344,7 @@ const proxy = <T extends ExcalidrawElement>(
};
/** Tools that can be used to draw shapes */
-type DrawingToolName = Exclude<ToolName, "lock" | "selection" | "eraser">;
+type DrawingToolName = Exclude<ToolType, "lock" | "selection" | "eraser">;
type Element<T extends DrawingToolName> = T extends "line" | "freedraw"
? ExcalidrawLinearElement
@@ -362,7 +361,7 @@ type Element<T extends DrawingToolName> = T extends "line" | "freedraw"
: ExcalidrawElement;
export class UI {
- static clickTool = (toolName: ToolName) => {
+ static clickTool = (toolName: ToolType | "lock") => {
fireEvent.click(GlobalTestState.renderResult.getByToolName(toolName));
};
diff --git a/src/tests/queries/toolQueries.ts b/src/tests/queries/toolQueries.ts
index c20e0d309f..a6a378e7ec 100644
--- a/src/tests/queries/toolQueries.ts
+++ b/src/tests/queries/toolQueries.ts
@@ -1,23 +1,9 @@
import { queries, buildQueries } from "@testing-library/react";
+import { ToolType } from "../../types";
+import { TOOL_TYPE } from "../../constants";
-const toolMap = {
- lock: "lock",
- selection: "selection",
- rectangle: "rectangle",
- diamond: "diamond",
- ellipse: "ellipse",
- arrow: "arrow",
- line: "line",
- freedraw: "freedraw",
- text: "text",
- eraser: "eraser",
- frame: "frame",
-};
-
-export type ToolName = keyof typeof toolMap;
-
-const _getAllByToolName = (container: HTMLElement, tool: string) => {
- const toolTitle = toolMap[tool as ToolName];
+const _getAllByToolName = (container: HTMLElement, tool: ToolType | "lock") => {
+ const toolTitle = tool === "lock" ? "lock" : TOOL_TYPE[tool];
return queries.getAllByTestId(container, `toolbar-${toolTitle}`);
};
@@ -32,7 +18,7 @@ export const [
getByToolName,
findAllByToolName,
findByToolName,
-] = buildQueries<string[]>(
+] = buildQueries<(ToolType | "lock")[]>(
_getAllByToolName,
getMultipleError,
getMissingError,
diff --git a/src/types.ts b/src/types.ts
index 32eb044b8d..a0690f955e 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -15,8 +15,10 @@ import {
ExcalidrawImageElement,
Theme,
StrokeRoundness,
- ExcalidrawFrameElement,
ExcalidrawEmbeddableElement,
+ ExcalidrawMagicFrameElement,
+ ExcalidrawFrameLikeElement,
+ ExcalidrawElementType,
} from "./element/types";
import { Action } from "./actions/types";
import { Point as RoughPoint } from "roughjs/bin/geometry";
@@ -102,9 +104,12 @@ export type ToolType =
| "eraser"
| "hand"
| "frame"
+ | "magicframe"
| "embeddable"
| "laser";
+export type ElementOrToolType = ExcalidrawElementType | ToolType | "custom";
+
export type ActiveTool =
| {
type: ToolType;
@@ -159,9 +164,6 @@ export type InteractiveCanvasAppState = Readonly<
suggestedBindings: AppState["suggestedBindings"];
isRotating: AppState["isRotating"];
elementsToHighlight: AppState["elementsToHighlight"];
- // App
- openSidebar: AppState["openSidebar"];
- showHyperlinkPopup: AppState["showHyperlinkPopup"];
// Collaborators
collaborators: AppState["collaborators"];
// SnapLines
@@ -170,7 +172,7 @@ export type InteractiveCanvasAppState = Readonly<
}
>;
-export type AppState = {
+export interface AppState {
contextMenu: {
items: ContextMenuItems;
top: number;
@@ -190,7 +192,7 @@ export type AppState = {
isBindingEnabled: boolean;
startBoundElement: NonDeleted<ExcalidrawBindableElement> | null;
suggestedBindings: SuggestedBinding[];
- frameToHighlight: NonDeleted<ExcalidrawFrameElement> | null;
+ frameToHighlight: NonDeleted<ExcalidrawFrameLikeElement> | null;
frameRendering: {
enabled: boolean;
name: boolean;
@@ -242,7 +244,13 @@ export type AppState = {
openMenu: "canvas" | "shape" | null;
openPopup: "canvasBackground" | "elementBackground" | "elementStroke" | null;
openSidebar: { name: SidebarName; tab?: SidebarTabName } | null;
- openDialog: "imageExport" | "help" | "jsonExport" | "mermaid" | null;
+ openDialog:
+ | "imageExport"
+ | "help"
+ | "jsonExport"
+ | "mermaid"
+ | "magicSettings"
+ | null;
/**
* Reflects user preference for whether the default sidebar should be docked.
*
@@ -297,7 +305,7 @@ export type AppState = {
y: number;
} | null;
objectsSnapModeEnabled: boolean;
-};
+}
export type UIAppState = Omit<
AppState,
@@ -437,6 +445,7 @@ export interface ExcalidrawProps {
element: NonDeleted<ExcalidrawEmbeddableElement>,
appState: AppState,
) => JSX.Element | null;
+ aiEnabled?: boolean;
}
export type SceneData = {
@@ -505,6 +514,7 @@ export type AppProps = Merge<
handleKeyboardGlobally: boolean;
isCollaborating: boolean;
children?: React.ReactNode;
+ aiEnabled: boolean;
}
>;
@@ -538,6 +548,8 @@ export type AppClassProperties = {
togglePenMode: App["togglePenMode"];
setActiveTool: App["setActiveTool"];
setOpenDialog: App["setOpenDialog"];
+ insertEmbeddableElement: App["insertEmbeddableElement"];
+ onMagicButtonSelect: App["onMagicButtonSelect"];
};
export type PointerDownState = Readonly<{
@@ -681,12 +693,14 @@ type FrameNameBounds = {
};
export type FrameNameBoundsCache = {
- get: (frameElement: ExcalidrawFrameElement) => FrameNameBounds | null;
+ get: (
+ frameElement: ExcalidrawFrameLikeElement | ExcalidrawMagicFrameElement,
+ ) => FrameNameBounds | null;
_cache: Map<
string,
FrameNameBounds & {
zoom: AppState["zoom"]["value"];
- versionNonce: ExcalidrawFrameElement["versionNonce"];
+ versionNonce: ExcalidrawFrameLikeElement["versionNonce"];
}
>;
};
@@ -706,3 +720,5 @@ export type Primitive =
| symbol
| null
| undefined;
+
+export type JSONValue = string | number | boolean | null | object;
diff --git a/src/utils.ts b/src/utils.ts
index d5fb432ed1..223cbab9dc 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -6,11 +6,7 @@ import {
isDarwin,
WINDOWS_EMOJI_FALLBACK_FONT,
} from "./constants";
-import {
- FontFamilyValues,
- FontString,
- NonDeletedExcalidrawElement,
-} from "./element/types";
+import { FontFamilyValues, FontString } from "./element/types";
import { ActiveTool, AppState, ToolType, Zoom } from "./types";
import { unstable_batchedUpdates } from "react-dom";
import { ResolutionType } from "./utility-types";
@@ -77,7 +73,9 @@ export const isWritableElement = (
target instanceof HTMLBRElement || // newline in wysiwyg
target instanceof HTMLTextAreaElement ||
(target instanceof HTMLInputElement &&
- (target.type === "text" || target.type === "number"));
+ (target.type === "text" ||
+ target.type === "number" ||
+ target.type === "password"));
export const getFontFamilyString = ({
fontFamily,
@@ -821,19 +819,6 @@ export const composeEventHandlers = <E>(
};
};
-export const isOnlyExportingSingleFrame = (
- elements: readonly NonDeletedExcalidrawElement[],
-) => {
- const frames = elements.filter((element) => element.type === "frame");
-
- return (
- frames.length === 1 &&
- elements.every(
- (element) => element.type === "frame" || element.frameId === frames[0].id,
- )
- );
-};
-
/**
* supply `null` as message if non-never value is valid, you just need to
* typecheck against it
diff --git a/src/zindex.ts b/src/zindex.ts
index 05dba7210f..a46c8b9a7c 100644
--- a/src/zindex.ts
+++ b/src/zindex.ts
@@ -1,6 +1,6 @@
import { bumpVersion } from "./element/mutateElement";
-import { isFrameElement } from "./element/typeChecks";
-import { ExcalidrawElement, ExcalidrawFrameElement } from "./element/types";
+import { isFrameLikeElement } from "./element/typeChecks";
+import { ExcalidrawElement, ExcalidrawFrameLikeElement } from "./element/types";
import { getElementsInGroup } from "./groups";
import { getSelectedElements } from "./scene";
import Scene from "./scene/Scene";
@@ -107,7 +107,7 @@ const getTargetIndexAccountingForBinding = (
const getContiguousFrameRangeElements = (
allElements: readonly ExcalidrawElement[],
- frameId: ExcalidrawFrameElement["id"],
+ frameId: ExcalidrawFrameLikeElement["id"],
) => {
let rangeStart = -1;
let rangeEnd = -1;
@@ -138,7 +138,7 @@ const getTargetIndex = (
* Frame id if moving frame children.
* If whole frame (including all children) is being moved, supply `null`.
*/
- containingFrame: ExcalidrawFrameElement["id"] | null,
+ containingFrame: ExcalidrawFrameLikeElement["id"] | null,
) => {
const sourceElement = elements[boundaryIndex];
@@ -189,7 +189,7 @@ const getTargetIndex = (
if (
!containingFrame &&
- (nextElement.frameId || nextElement.type === "frame")
+ (nextElement.frameId || isFrameLikeElement(nextElement))
) {
const frameElements = getContiguousFrameRangeElements(
elements,
@@ -252,9 +252,9 @@ const shiftElementsByOne = (
groupedIndices = groupedIndices.reverse();
}
- const selectedFrames = new Set<ExcalidrawFrameElement["id"]>(
+ const selectedFrames = new Set<ExcalidrawFrameLikeElement["id"]>(
indicesToMove
- .filter((idx) => elements[idx].type === "frame")
+ .filter((idx) => isFrameLikeElement(elements[idx]))
.map((idx) => elements[idx].id),
);
@@ -324,7 +324,7 @@ const shiftElementsToEnd = (
elements: readonly ExcalidrawElement[],
appState: AppState,
direction: "left" | "right",
- containingFrame: ExcalidrawFrameElement["id"] | null,
+ containingFrame: ExcalidrawFrameLikeElement["id"] | null,
elementsToBeMoved?: readonly ExcalidrawElement[],
) => {
const indicesToMove = getIndicesToMove(elements, appState, elementsToBeMoved);
@@ -413,7 +413,7 @@ function shiftElementsAccountingForFrames(
elements: readonly ExcalidrawElement[],
appState: AppState,
direction: "left" | "right",
- containingFrame: ExcalidrawFrameElement["id"] | null,
+ containingFrame: ExcalidrawFrameLikeElement["id"] | null,
elementsToBeMoved?: readonly ExcalidrawElement[],
) => ExcalidrawElement[] | readonly ExcalidrawElement[],
) {
@@ -426,13 +426,13 @@ function shiftElementsAccountingForFrames(
const frameAwareContiguousElementsToMove: {
regularElements: ExcalidrawElement[];
- frameChildren: Map<ExcalidrawFrameElement["id"], ExcalidrawElement[]>;
+ frameChildren: Map<ExcalidrawFrameLikeElement["id"], ExcalidrawElement[]>;
} = { regularElements: [], frameChildren: new Map() };
- const fullySelectedFrames = new Set<ExcalidrawFrameElement["id"]>();
+ const fullySelectedFrames = new Set<ExcalidrawFrameLikeElement["id"]>();
for (const element of allElements) {
- if (elementsToMove.has(element.id) && isFrameElement(element)) {
+ if (elementsToMove.has(element.id) && isFrameLikeElement(element)) {
fullySelectedFrames.add(element.id);
}
}
@@ -440,7 +440,7 @@ function shiftElementsAccountingForFrames(
for (const element of allElements) {
if (elementsToMove.has(element.id)) {
if (
- isFrameElement(element) ||
+ isFrameLikeElement(element) ||
(element.frameId && fullySelectedFrames.has(element.frameId))
) {
frameAwareContiguousElementsToMove.regularElements.push(element);