提交 df8c2f01 编写于 作者: Q qq_41923622

Thu Feb 6 11:54:00 CST 2025 inscode

上级 7c2d9c59
// import { Popover } from '@casstime/bricks'; // import { Popover } from '@casstime/bricks';
import React, { forwardRef, useContext, useImperativeHandle } from 'react'; import React, { forwardRef, useContext, useImperativeHandle } from 'react';
import { ACTION, LANG, MENU_TYPE_ENUM, MENU_TYPE_TEXT } from '../constants'; import { ACTION, LANG, MENU_TYPE_ENUM, MENU_TYPE_TEXT } from '../constants';
import { EditorContext } from '../util'; import { EditorContext } from '../util';
import Popover from './setting/Popover'; import Popover from './setting/Popover';
import {fabric} from 'fabric'; import {fabric} from 'fabric';
function annotationsInit(annotations,canvas,historyRef){ function annotationsInit(annotations,canvas,historyRef){
// 遍历标注信息,重新创建元素并添加到画布上 // 遍历标注信息,重新创建元素并添加到画布上
annotations.forEach((annotation) => { annotations.forEach((annotation) => {
let newObject; let newObject;
switch (annotation.type) { switch (annotation.type) {
case 'rect': case 'rect':
newObject = new fabric.Rect({ newObject = new fabric.Rect({
left: annotation.left, left: annotation.left,
top: annotation.top, top: annotation.top,
originX: 'left', originX: 'left',
originY: 'top', originY: 'top',
width: annotation.width, width: annotation.width,
height: annotation.height, height: annotation.height,
angle: annotation.angle, angle: annotation.angle,
fill: annotation.fill, fill: annotation.fill,
stroke: annotation.stroke, stroke: annotation.stroke,
scaleX: annotation.scaleX, scaleX: annotation.scaleX,
scaleY: annotation.scaleY, scaleY: annotation.scaleY,
strokeWidth: annotation.size, strokeWidth: annotation.size,
transparentCorners: false, transparentCorners: false,
opacity: annotation.opacity || 1 opacity: annotation.opacity || 1
}); });
break; break;
case 'textbox': case 'textbox':
case 'text': case 'text':
newObject = new fabric.Textbox(annotation.text, { newObject = new fabric.Textbox(annotation.text, {
left: annotation.left, left: annotation.left,
top: annotation.top, top: annotation.top,
width: annotation.width, width: annotation.width,
angle: annotation.angle, angle: annotation.angle,
fill: annotation.fill, fill: annotation.fill,
stroke: annotation.stroke, stroke: annotation.stroke,
scaleX: annotation.scaleX, scaleX: annotation.scaleX,
scaleY: annotation.scaleY, scaleY: annotation.scaleY,
opacity: annotation.opacity || 1 opacity: annotation.opacity || 1
}); });
break; break;
case 'circle': case 'circle':
newObject = new fabric.Circle({ newObject = new fabric.Circle({
left: annotation.left, left: annotation.left,
top: annotation.top, top: annotation.top,
radius: annotation.width / 2, radius: annotation.width / 2,
angle: annotation.angle||0, angle: annotation.angle||0,
fill: annotation.fill||'', fill: annotation.fill||'',
originX: 'left', originX: 'left',
originY: 'top', originY: 'top',
stroke: annotation.stroke, stroke: annotation.stroke,
scaleX: annotation.scaleX, scaleX: annotation.scaleX,
scaleY: annotation.scaleY, scaleY: annotation.scaleY,
strokeWidth: annotation.size, strokeWidth: annotation.size,
opacity: annotation.opacity || 1 opacity: annotation.opacity || 1
}); });
break; break;
// 可以根据需要添加更多元素类型的处理 // 可以根据需要添加更多元素类型的处理
} }
if (newObject) { if (newObject) {
console.log(`annotation`,newObject,annotation,canvas) console.log(`annotation`,newObject,annotation,canvas)
canvas.add(newObject) canvas.add(newObject)
canvas.setActiveObject(newObject); canvas.setActiveObject(newObject);
historyRef.current.updateCanvasState(ACTION.add, true, true); historyRef.current.updateCanvasState(ACTION.add, true, true);
// canvas.setActiveObject(newObject); // canvas.setActiveObject(newObject);
} }
}); });
} }
export const useUpload = () => { export const useUpload = () => {
const { const {
canvasInstanceRef, canvasInstanceRef,
canvasEl, canvasEl,
currentMenuRef, currentMenuRef,
historyRef, historyRef,
setCurrentMenu, setCurrentMenu,
initBackGroundImage, initBackGroundImage,
} = useContext(EditorContext); } = useContext(EditorContext);
const handleUpload = (e: any,imgData?:any) => { const handleUpload = (e: any,imgData?:any) => {
console.log(`e`,e,imgData) console.log(`e`,e,imgData)
const canvas = canvasInstanceRef.current; const canvas = canvasInstanceRef.current;
if(canvasEl?.current&&typeof e === 'string'){ if(canvasEl?.current&&typeof e === 'string'){
let imgObj = new Image(); let imgObj = new Image();
imgObj.src = e as string; imgObj.src = e as string;
initBackGroundImage(e as string, true, () => { initBackGroundImage(e as string, true, () => {
historyRef.current.updateCanvasState(ACTION.add); historyRef.current.updateCanvasState(ACTION.add);
setTimeout(()=>{ setTimeout(()=>{
if(imgData){ if(imgData){
annotationsInit(imgData.annotations,canvas,historyRef); annotationsInit(imgData.annotations,canvas,historyRef);
} }
}) })
},5000); },5000);
return; return;
} }
if ( if (
!canvas || !canvas ||
!canvasEl.current || !canvasEl.current ||
!e.target.files.length || !e.target.files.length ||
!e.target.files[0]?.type?.includes('image') !e.target.files[0]?.type?.includes('image')
) { ) {
return; return;
} }
if (currentMenuRef.current !== MENU_TYPE_ENUM.upload) { if (currentMenuRef.current !== MENU_TYPE_ENUM.upload) {
setCurrentMenu(MENU_TYPE_ENUM.upload); setCurrentMenu(MENU_TYPE_ENUM.upload);
} }
const reader = new FileReader(); const reader = new FileReader();
reader.onload = function (event) { reader.onload = function (event) {
if (!event || !event.target) { if (!event || !event.target) {
return; return;
} }
let imgObj = new Image(); let imgObj = new Image();
imgObj.src = event.target.result as string; imgObj.src = event.target.result as string;
initBackGroundImage(event.target.result as string, true, () => { initBackGroundImage(event.target.result as string, true, () => {
historyRef.current.updateCanvasState(ACTION.add); historyRef.current.updateCanvasState(ACTION.add);
}); });
}; };
reader.readAsDataURL(e.target.files[0]); reader.readAsDataURL(e.target.files[0]);
}; };
return { handleUpload }; return { handleUpload };
}; };
/** 上传 */ /** 上传 */
export const Upload = forwardRef((props, ref) => { export const Upload = forwardRef((props, ref) => {
const { const {
lang = LANG.en lang = LANG.en
} = useContext(EditorContext); } = useContext(EditorContext);
const { handleUpload } = useUpload(); const { handleUpload } = useUpload();
useImperativeHandle( useImperativeHandle(
ref, ref,
() => ({ () => ({
handleUpload: handleUpload, handleUpload: handleUpload,
}), }),
[handleUpload], [handleUpload],
); );
return ( return (
<> <>
{/* <div className="tie-image-editor_tool-item tie-image-editor_tool-upload"> */} {/* <div className="tie-image-editor_tool-item tie-image-editor_tool-upload"> */}
{/* <Popover content={MENU_TYPE_TEXT.upload[lang]} placement="top"> */} {/* <Popover content={MENU_TYPE_TEXT.upload[lang]} placement="top"> */}
{/* <i className="tie-image-editor_icon"> */} {/* <i className="tie-image-editor_icon"> */}
{/* <input {/* <input
onChange={handleUpload} onChange={handleUpload}
type="file" type="file"
className="tie-image-editor_tool-upload-input" className="tie-image-editor_tool-upload-input"
accept="image/*" accept="image/*"
/> */} /> */}
{/* </i> */} {/* </i> */}
{/* </Popover> */} {/* </Popover> */}
{/* </div> */} {/* </div> */}
</> </>
); );
}) })
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册