-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { type IRect, normalizeRect } from '@suika/geo'; | ||
|
||
import { type SuikaEditor } from '../editor'; | ||
import { SuikaRect } from '../graphics'; | ||
import { PaintType } from '../paint'; | ||
import { DrawGraphicsTool } from './tool_draw_graphics'; | ||
import { type ITool } from './type'; | ||
|
||
interface ImgData { | ||
url: string; | ||
name: string; | ||
} | ||
|
||
const uploadImg = () => { | ||
return new Promise<ImgData>((resolve) => { | ||
const fileInput = document.createElement('input'); | ||
fileInput.type = 'file'; | ||
fileInput.accept = 'image/*'; // only image | ||
|
||
fileInput.onchange = (e) => { | ||
const file = (e.target as HTMLInputElement).files?.[0]; | ||
if (!file) return; | ||
const reader = new FileReader(); | ||
reader.onload = (e) => { | ||
const src = e.target?.result as string; | ||
setTimeout(() => { | ||
resolve({ url: src, name: file.name }); | ||
}); | ||
}; | ||
reader.readAsDataURL(file); | ||
}; | ||
|
||
fileInput.click(); | ||
}); | ||
}; | ||
|
||
const TYPE = 'drawImg'; | ||
const HOTKEY = ''; | ||
|
||
export class DrawImgTool extends DrawGraphicsTool implements ITool { | ||
static override readonly type = TYPE; | ||
static override readonly hotkey = HOTKEY; | ||
override readonly type = TYPE; | ||
override readonly hotkey = HOTKEY; | ||
|
||
private imgData: ImgData | null = null; | ||
|
||
constructor(editor: SuikaEditor) { | ||
super(editor); | ||
this.commandDesc = 'Add Image'; | ||
} | ||
|
||
async enableActive() { | ||
try { | ||
const imgData = await uploadImg(); | ||
await this.editor.imgManager.addImg(imgData.url); | ||
this.imgData = imgData; | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
protected override createGraphics(rect: IRect) { | ||
rect = normalizeRect(rect); | ||
const graphics = new SuikaRect( | ||
{ | ||
objectName: this.imgData!.name, | ||
width: rect.width, | ||
height: rect.height, | ||
fill: [ | ||
{ | ||
type: PaintType.Image, | ||
attrs: { | ||
src: this.imgData!.url, | ||
}, | ||
}, | ||
], | ||
}, | ||
{ | ||
advancedAttrs: { | ||
x: rect.x, | ||
y: rect.y, | ||
}, | ||
doc: this.editor.doc, | ||
}, | ||
); | ||
return graphics; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
|
||
export const ImageOutlined = React.memo(() => { | ||
return ( | ||
<svg | ||
width="24" | ||
height="24" | ||
viewBox="0 0 24 24" | ||
fill="currentColor" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
fillRule="evenodd" | ||
clipRule="evenodd" | ||
d="M18 5H5V13.6849L8.46807 9.78333L17.1983 18H18V5ZM5 18V15.1901L8.53193 11.2167L15.7392 18H5ZM4 4V19H19V4H4ZM16 9.5C16 10.3284 15.3284 11 14.5 11C13.6716 11 13 10.3284 13 9.5C13 8.67157 13.6716 8 14.5 8C15.3284 8 16 8.67157 16 9.5ZM17 9.5C17 10.8807 15.8807 12 14.5 12C13.1193 12 12 10.8807 12 9.5C12 8.11929 13.1193 7 14.5 7C15.8807 7 17 8.11929 17 9.5Z" | ||
/> | ||
</svg> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters