-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2143 from wonderunit/sg-image-insertion
Shot-generator image insertion
- Loading branch information
Showing
6 changed files
with
108 additions
and
11 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
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,59 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import { useContext } from 'react' | ||
import FilepathsContext from '../contexts/filepaths' | ||
const useInsertImage = (initializeImage) => { | ||
const { getAssetPath } = useContext(FilepathsContext) | ||
const dragOver = (e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
} | ||
|
||
const imageDrop = (e) => { | ||
for (const f of e.dataTransfer.files) { | ||
let { name, ext } = path.parse( f.path) | ||
let imageId = THREE.Math.generateUUID() | ||
let imageProjectPath = path.join('models', 'images', name + ext) | ||
let imagePath = getAssetPath('image', imageProjectPath) | ||
fs.ensureDirSync(path.dirname(imagePath)) | ||
if(!fs.existsSync(imagePath)) | ||
fs.copyFileSync(f.path, imagePath) | ||
initializeImage(imageId, imageProjectPath) | ||
} | ||
} | ||
|
||
const createImageFromClipboard = (pasteEvent) => { | ||
if(pasteEvent.clipboardData == false) { | ||
return | ||
} | ||
let items = pasteEvent.clipboardData.items | ||
if(items == undefined) { | ||
return | ||
} | ||
for(let i = 0; i < items.length; i++) { | ||
let item = items[i] | ||
if(item.type.indexOf("image") == -1) continue | ||
let blob = item.getAsFile() | ||
|
||
let imageId = THREE.Math.generateUUID() | ||
let imageProjectPath = path.join('models', 'images', `${imageId}-texture.png`) | ||
let imagePath = getAssetPath('image', imageProjectPath) | ||
let reader = new FileReader() | ||
reader.onload = function() { | ||
if(reader.readyState == 2) { | ||
let buffer = Buffer.from(reader.result) | ||
fs.ensureDirSync(path.dirname(imagePath)) | ||
fs.writeFileSync(imagePath, buffer) | ||
initializeImage(imageId, imageProjectPath) | ||
} | ||
} | ||
reader.readAsArrayBuffer(blob) | ||
} | ||
|
||
} | ||
|
||
return { dragOver, imageDrop, createImageFromClipboard } | ||
|
||
} | ||
|
||
export { useInsertImage } |
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