rename name to customType
This commit is contained in:
parent
3d0a1106ff
commit
8706277d14
@ -421,11 +421,11 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
renderCustomElement = (
|
renderCustomElement = (
|
||||||
coords: { x: number; y: number },
|
coords: { x: number; y: number },
|
||||||
name: string = "",
|
customType: string = "",
|
||||||
) => {
|
) => {
|
||||||
const config = getCustomElementConfig(
|
const config = getCustomElementConfig(
|
||||||
this.props.customElementsConfig,
|
this.props.customElementsConfig,
|
||||||
name,
|
customType,
|
||||||
);
|
);
|
||||||
if (!config) {
|
if (!config) {
|
||||||
return;
|
return;
|
||||||
@ -438,7 +438,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
|
|
||||||
const width = config.width || 40;
|
const width = config.width || 40;
|
||||||
const height = config.height || 40;
|
const height = config.height || 40;
|
||||||
const customElement = newCustomElement(name, {
|
const customElement = newCustomElement(customType, {
|
||||||
type: "custom",
|
type: "custom",
|
||||||
x: gridX - width / 2,
|
x: gridX - width / 2,
|
||||||
y: gridY - height / 2,
|
y: gridY - height / 2,
|
||||||
@ -3400,7 +3400,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
if (selectedElements[0].type === "custom") {
|
if (selectedElements[0].type === "custom") {
|
||||||
const config = getCustomElementConfig(
|
const config = getCustomElementConfig(
|
||||||
this.props.customElementsConfig,
|
this.props.customElementsConfig,
|
||||||
selectedElements[0].name,
|
selectedElements[0].customType,
|
||||||
);
|
);
|
||||||
if (!config?.transformHandles) {
|
if (!config?.transformHandles) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -154,7 +154,7 @@ export const DEFAULT_UI_OPTIONS: AppProps["UIOptions"] = {
|
|||||||
|
|
||||||
export const DEFAULT_CUSTOM_ELEMENT_CONFIG: Required<CustomElementConfig> = {
|
export const DEFAULT_CUSTOM_ELEMENT_CONFIG: Required<CustomElementConfig> = {
|
||||||
type: "custom",
|
type: "custom",
|
||||||
name: "custom",
|
customType: "custom",
|
||||||
transformHandles: true,
|
transformHandles: true,
|
||||||
svg: "",
|
svg: "",
|
||||||
width: 40,
|
width: 40,
|
||||||
|
@ -195,7 +195,7 @@ const restoreElement = (
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
case "custom":
|
case "custom":
|
||||||
return restoreElementWithProperties(element, { name: "custom" });
|
return restoreElementWithProperties(element, { customType: "custom" });
|
||||||
// generic elements
|
// generic elements
|
||||||
case "ellipse":
|
case "ellipse":
|
||||||
return restoreElementWithProperties(element, {});
|
return restoreElementWithProperties(element, {});
|
||||||
|
@ -320,14 +320,14 @@ export const newImageElement = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const newCustomElement = (
|
export const newCustomElement = (
|
||||||
name: string,
|
customType: string,
|
||||||
opts: {
|
opts: {
|
||||||
type: ExcalidrawCustomElement["type"];
|
type: ExcalidrawCustomElement["type"];
|
||||||
} & ElementConstructorOpts,
|
} & ElementConstructorOpts,
|
||||||
): NonDeleted<ExcalidrawCustomElement> => {
|
): NonDeleted<ExcalidrawCustomElement> => {
|
||||||
return {
|
return {
|
||||||
..._newElementBase<ExcalidrawCustomElement>("custom", opts),
|
..._newElementBase<ExcalidrawCustomElement>("custom", opts),
|
||||||
name,
|
customType,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
// Simplified deep clone for the purpose of cloning ExcalidrawElement only
|
// Simplified deep clone for the purpose of cloning ExcalidrawElement only
|
||||||
|
@ -84,7 +84,7 @@ export type ExcalidrawImageElement = _ExcalidrawElementBase &
|
|||||||
}>;
|
}>;
|
||||||
|
|
||||||
export type ExcalidrawCustomElement = _ExcalidrawElementBase &
|
export type ExcalidrawCustomElement = _ExcalidrawElementBase &
|
||||||
Readonly<{ type: "custom"; name: string }>;
|
Readonly<{ type: "custom"; customType: string }>;
|
||||||
|
|
||||||
export type InitializedExcalidrawImageElement = MarkNonNullable<
|
export type InitializedExcalidrawImageElement = MarkNonNullable<
|
||||||
ExcalidrawImageElement,
|
ExcalidrawImageElement,
|
||||||
|
@ -179,7 +179,7 @@ export default function App() {
|
|||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
type: "custom",
|
type: "custom",
|
||||||
name: "star",
|
customType: "star",
|
||||||
svg: `data:${
|
svg: `data:${
|
||||||
MIME_TYPES.svg
|
MIME_TYPES.svg
|
||||||
}, ${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512">
|
}, ${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512">
|
||||||
@ -190,7 +190,7 @@ export default function App() {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "custom",
|
type: "custom",
|
||||||
name: "comment",
|
customType: "comment",
|
||||||
svg: `data:${
|
svg: `data:${
|
||||||
MIME_TYPES.svg
|
MIME_TYPES.svg
|
||||||
}, ${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
|
}, ${encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
|
||||||
|
@ -197,7 +197,7 @@ const drawImagePlaceholder = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const customElementImgCache: {
|
const customElementImgCache: {
|
||||||
[key: ExcalidrawCustomElement["name"]]: HTMLImageElement;
|
[key: ExcalidrawCustomElement["customType"]]: HTMLImageElement;
|
||||||
} = {};
|
} = {};
|
||||||
const drawElementOnCanvas = (
|
const drawElementOnCanvas = (
|
||||||
element: NonDeletedExcalidrawElement,
|
element: NonDeletedExcalidrawElement,
|
||||||
@ -264,19 +264,19 @@ const drawElementOnCanvas = (
|
|||||||
case "custom": {
|
case "custom": {
|
||||||
const config = getCustomElementConfig(
|
const config = getCustomElementConfig(
|
||||||
renderConfig.customElementsConfig,
|
renderConfig.customElementsConfig,
|
||||||
element.name,
|
element.customType,
|
||||||
);
|
);
|
||||||
if (!config) {
|
if (!config) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!customElementImgCache[config.name]) {
|
if (!customElementImgCache[config.customType]) {
|
||||||
const img = document.createElement("img");
|
const img = document.createElement("img");
|
||||||
img.id = config.name;
|
img.id = config.customType;
|
||||||
img.src = config.svg;
|
img.src = config.svg;
|
||||||
customElementImgCache[img.id] = img;
|
customElementImgCache[img.id] = img;
|
||||||
}
|
}
|
||||||
context.drawImage(
|
context.drawImage(
|
||||||
customElementImgCache[config.name],
|
customElementImgCache[config.customType],
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
element.width,
|
element.width,
|
||||||
|
@ -315,7 +315,7 @@ export const renderScene = (
|
|||||||
if (element.type === "custom") {
|
if (element.type === "custom") {
|
||||||
config = getCustomElementConfig(
|
config = getCustomElementConfig(
|
||||||
renderConfig.customElementsConfig,
|
renderConfig.customElementsConfig,
|
||||||
element.name,
|
element.customType,
|
||||||
)!;
|
)!;
|
||||||
}
|
}
|
||||||
if (!isCustom || (isCustom && config!.transformHandles)) {
|
if (!isCustom || (isCustom && config!.transformHandles)) {
|
||||||
@ -389,7 +389,7 @@ export const renderScene = (
|
|||||||
if (locallySelectedElements[0].type === "custom") {
|
if (locallySelectedElements[0].type === "custom") {
|
||||||
const config = getCustomElementConfig(
|
const config = getCustomElementConfig(
|
||||||
renderConfig.customElementsConfig,
|
renderConfig.customElementsConfig,
|
||||||
locallySelectedElements[0].name,
|
locallySelectedElements[0].customType,
|
||||||
);
|
);
|
||||||
if (!config || !config.transformHandles) {
|
if (!config || !config.transformHandles) {
|
||||||
showTransformHandles = false;
|
showTransformHandles = false;
|
||||||
|
@ -19,7 +19,7 @@ export const getElementsWithinSelection = (
|
|||||||
getElementBounds(element);
|
getElementBounds(element);
|
||||||
const isCustom = element.type === "custom";
|
const isCustom = element.type === "custom";
|
||||||
const allowSelection = isCustom
|
const allowSelection = isCustom
|
||||||
? getCustomElementConfig(customElementConfig, element.name)
|
? getCustomElementConfig(customElementConfig, element.customType)
|
||||||
?.transformHandles
|
?.transformHandles
|
||||||
: true;
|
: true;
|
||||||
return (
|
return (
|
||||||
|
@ -208,7 +208,7 @@ export type ExcalidrawAPIRefValue =
|
|||||||
|
|
||||||
export type CustomElementConfig = {
|
export type CustomElementConfig = {
|
||||||
type: "custom";
|
type: "custom";
|
||||||
name: string;
|
customType: string;
|
||||||
transformHandles?: boolean;
|
transformHandles?: boolean;
|
||||||
svg: string;
|
svg: string;
|
||||||
width?: number;
|
width?: number;
|
||||||
|
@ -615,10 +615,10 @@ export const updateObject = <T extends Record<string, any>>(
|
|||||||
|
|
||||||
export const getCustomElementConfig = (
|
export const getCustomElementConfig = (
|
||||||
customElementConfig: ExcalidrawProps["customElementsConfig"],
|
customElementConfig: ExcalidrawProps["customElementsConfig"],
|
||||||
name: string,
|
customType: string,
|
||||||
) => {
|
) => {
|
||||||
if (!customElementConfig) {
|
if (!customElementConfig) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return customElementConfig.find((config) => config.name === name);
|
return customElementConfig.find((config) => config.customType === customType);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user