-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathHelpImageHandler.tsx
More file actions
100 lines (80 loc) · 3.34 KB
/
HelpImageHandler.tsx
File metadata and controls
100 lines (80 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { PropertyRoute } from "@framework/Lines";
import { getSymbol } from "@framework/Reflection";
import { useAPI } from "@framework/Hooks";
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { FileImage } from "../../Signum.Files/Components/FileImage";
import { toFileEntity } from "../../Signum.Files/Components/FileUploader";
import { FilePathEmbedded, FileTypeSymbol } from "../../Signum.Files/Signum.Files";
import { ImageHandlerBase, ImageInfo } from "../../Signum.HtmlEditor/Extensions/ImageExtension/ImageHandlerBase";
import { HelpImageEntity, HelpMessage } from "../Signum.Help";
import { HelpImageNode } from "./HelpImageNode";
import { ImageNodeBase } from "../../Signum.HtmlEditor/Extensions/ImageExtension/ImageNodeBase";
import { HelpClient } from "../HelpClient";
export class HelpImageHandler implements ImageHandlerBase {
private _pr?: PropertyRoute;
get pr(): PropertyRoute {
return this._pr ??= HelpImageEntity.propertyRouteAssert(a => a.file)
}
getNodeType(): typeof ImageNodeBase { return HelpImageNode }
toElement(val: ImageInfo): HTMLElement | undefined {
const img = document.createElement("img");
val.binaryFile && img.setAttribute("data-binary-file", val.binaryFile);
img.setAttribute("data-file-name", val.fileName || "");
val.key && img.setAttribute("data-help-image-guid", val.key);
return img;
}
uploadData(blob: Blob): Promise<ImageInfo> {
var file = blob instanceof File ? blob :
new File([blob], "pastedImage." + blob.type.after("/"));
return toFileEntity(file, {
type: FilePathEmbedded, accept: "image/*",
maxSizeInBytes: this.pr.member!.defaultFileTypeInfo!.maxSizeInBytes ?? undefined
})
.then(att => ({
binaryFile: att.binaryFile ?? undefined,
fileName: att.fileName ?? undefined,
}));
}
renderImage(info: ImageInfo): React.ReactElement {
return <InlineImage info={info} pr={this.pr} />;
}
toHtml(val: ImageInfo): string | undefined {
if (val.binaryFile)
return `<img data-binary-file="${val.binaryFile}" data-file-name="${val.fileName}" />`;
if (val.key)
return `<img data-help-image-guid="${val.key}" />`;
return undefined;
}
fromElement(element: HTMLDivElement): ImageInfo | undefined {
if (element.tagName == "IMG") {
return {
binaryFile: element.getAttribute("data-binary-file") ?? undefined,
fileName: element.getAttribute("data-file-name") ?? undefined,
key: element.getAttribute("data-help-image-guid") ?? undefined,
};
}
return undefined;
}
}
function InlineImage(p: { info: ImageInfo, pr: PropertyRoute }): React.ReactElement | undefined
{
const imageId = useAPI(() => p.info.key && HelpClient.API.getImageId(p.info.key), []);
if (!imageId && !p.info.binaryFile)
return (
<div className="alert alert-info d-inline-block" >
<span>
<FontAwesomeIcon icon="gear" className="fa-fw me-2" style={{ fontSize: "larger" }} spin />
{HelpMessage.LoadingImage.niceToString()}...
</span>
</div>
);
const fp = FilePathEmbedded.New({
binaryFile: p.info.binaryFile,
entityId: imageId,
mListRowId: null,
fileType: getSymbol(FileTypeSymbol, p.pr.member!.defaultFileTypeInfo!.key),
rootType: p.pr.findRootType().name,
propertyRoute: p.pr.propertyPath()
});
return <FileImage file={fp} />;
}