mirror of
https://github.com/bendtherules/piktochart-create-editor.git
synced 2026-08-18 22:02:56 +00:00
Add image preview plus image add from sidebart
This commit is contained in:
@@ -1,14 +1,16 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as uuid from 'uuid/v4';
|
import * as uuid from 'uuid/v4';
|
||||||
import './AddToCanvasInput.css';
|
import './AddToCanvasInput.css';
|
||||||
import { CanvasTextNode } from '../../Helpers';
|
import { CanvasTextNode, CanvasImageNode } from '../../Helpers';
|
||||||
|
|
||||||
interface AddToCanvasInputProps {
|
interface AddToCanvasInputProps {
|
||||||
addTextNode(nodeId: string, textNode: CanvasTextNode): void;
|
addTextNode(nodeId: string, textNode: CanvasTextNode): void;
|
||||||
|
addImageNode(nodeId: string, imgNode: CanvasImageNode): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface AddToCanvasInputState {
|
interface AddToCanvasInputState {
|
||||||
textInputValue: string;
|
textInputValue: string;
|
||||||
|
imageURLs: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AddToCanvasInput extends React.Component<AddToCanvasInputProps, AddToCanvasInputState> {
|
export class AddToCanvasInput extends React.Component<AddToCanvasInputProps, AddToCanvasInputState> {
|
||||||
@@ -16,11 +18,32 @@ export class AddToCanvasInput extends React.Component<AddToCanvasInputProps, Add
|
|||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
textInputValue: ''
|
textInputValue: '',
|
||||||
|
imageURLs: []
|
||||||
};
|
};
|
||||||
|
|
||||||
this.updateInputText = this.updateInputText.bind(this);
|
this.updateInputText = this.updateInputText.bind(this);
|
||||||
this.addTextNodeOnSubmit = this.addTextNodeOnSubmit.bind(this);
|
this.addTextNodeOnSubmit = this.addTextNodeOnSubmit.bind(this);
|
||||||
|
this.addImageNodeOnSubmit = this.addImageNodeOnSubmit.bind(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.fetchImages();
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchImages() {
|
||||||
|
fetch('http://localhost:8000/images/')
|
||||||
|
.then((response: Response) => {
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then((responseJSON: string[]) => {
|
||||||
|
this.setState({
|
||||||
|
imageURLs: responseJSON
|
||||||
|
});
|
||||||
|
})
|
||||||
|
// tslint:disable-next-line:no-console
|
||||||
|
.catch(error => console.error('Error loading image info:', error));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateInputText(ev: React.ChangeEvent<HTMLInputElement>) {
|
updateInputText(ev: React.ChangeEvent<HTMLInputElement>) {
|
||||||
@@ -44,6 +67,16 @@ export class AddToCanvasInput extends React.Component<AddToCanvasInputProps, Add
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addImageNodeOnSubmit(ev: React.MouseEvent<HTMLImageElement>) {
|
||||||
|
const imageURL = ev.currentTarget.src;
|
||||||
|
const nodeId = uuid();
|
||||||
|
|
||||||
|
this.props.addImageNode(nodeId, {
|
||||||
|
url: imageURL,
|
||||||
|
position: { x: 0, y: 0 }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="add-to-canvas-input">
|
<div className="add-to-canvas-input">
|
||||||
@@ -64,8 +97,19 @@ export class AddToCanvasInput extends React.Component<AddToCanvasInputProps, Add
|
|||||||
<div className="image">
|
<div className="image">
|
||||||
<h4>Images</h4>
|
<h4>Images</h4>
|
||||||
<ul className="list-unstyled">
|
<ul className="list-unstyled">
|
||||||
{/* List of images here
|
{
|
||||||
<li><img src="images/sample.jpeg" class="img-rounded" /></li> */}
|
this.state.imageURLs.map((imgURL) => {
|
||||||
|
return (
|
||||||
|
<li key={imgURL}>
|
||||||
|
<img
|
||||||
|
src={imgURL}
|
||||||
|
className="img-rounded"
|
||||||
|
onClick={this.addImageNodeOnSubmit}
|
||||||
|
/>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as uuid from 'uuid/v4';
|
import * as uuid from 'uuid/v4';
|
||||||
import './CanvasArea.css';
|
import './CanvasArea.css';
|
||||||
import { CanvasTextNode } from '../../Helpers';
|
import { CanvasTextNode, CanvasImageNode } from '../../Helpers';
|
||||||
import { DragContainer } from '../DragContainer';
|
import { DragContainer } from '../DragContainer';
|
||||||
import { DraggableNode } from '../DraggableNode';
|
import { DraggableNode } from '../DraggableNode';
|
||||||
|
|
||||||
@@ -10,6 +10,9 @@ interface CanvasAreaProps {
|
|||||||
text: {
|
text: {
|
||||||
[nodeId: string]: CanvasTextNode;
|
[nodeId: string]: CanvasTextNode;
|
||||||
}
|
}
|
||||||
|
image: {
|
||||||
|
[nodeId: string]: CanvasImageNode;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
interface CanvasAreaState {
|
interface CanvasAreaState {
|
||||||
@@ -24,7 +27,7 @@ export class CanvasArea extends React.Component<CanvasAreaProps, CanvasAreaState
|
|||||||
this.containerId = 'container-' + uuid();
|
this.containerId = 'container-' + uuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
renderDraggableNodes() {
|
renderTextNodes() {
|
||||||
return Object.keys(this.props.nodes.text).map((nodeId) => {
|
return Object.keys(this.props.nodes.text).map((nodeId) => {
|
||||||
const tmpTextNode = this.props.nodes.text[nodeId];
|
const tmpTextNode = this.props.nodes.text[nodeId];
|
||||||
return (
|
return (
|
||||||
@@ -35,6 +38,26 @@ export class CanvasArea extends React.Component<CanvasAreaProps, CanvasAreaState
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderImageNodes() {
|
||||||
|
return Object.keys(this.props.nodes.image).map((nodeId) => {
|
||||||
|
const tmpImageNode = this.props.nodes.image[nodeId];
|
||||||
|
return (
|
||||||
|
<DraggableNode key={nodeId} id={nodeId} containerId={this.containerId}>
|
||||||
|
<img draggable={false} src={tmpImageNode.url} width="100px" />
|
||||||
|
</DraggableNode>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
renderDraggableNodes() {
|
||||||
|
return (
|
||||||
|
<React.Fragment>
|
||||||
|
{this.renderTextNodes()}
|
||||||
|
{this.renderImageNodes()}
|
||||||
|
</React.Fragment>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="canvas-area col-sm-8 col-md-8 col-lg-8">
|
<div className="canvas-area col-sm-8 col-md-8 col-lg-8">
|
||||||
@@ -46,19 +69,6 @@ export class CanvasArea extends React.Component<CanvasAreaProps, CanvasAreaState
|
|||||||
</div>
|
</div>
|
||||||
</DragContainer>
|
</DragContainer>
|
||||||
</div>
|
</div>
|
||||||
// <DragContainer id="test">
|
|
||||||
// <header className="App-header">
|
|
||||||
// <DraggableNode id="test-img" containerId="test">
|
|
||||||
// <img src={logo} className="App-logo" alt="logo" draggable={false} />
|
|
||||||
// </DraggableNode>
|
|
||||||
// <DraggableNode id="test-text" containerId="test">
|
|
||||||
// <h1 className="App-title">Welcome to React</h1>
|
|
||||||
// </DraggableNode>
|
|
||||||
// </header>
|
|
||||||
// </DragContainer>
|
|
||||||
// <p className="App-intro">
|
|
||||||
// To get started, edit <code>src/App.tsx</code> and save to reload.
|
|
||||||
// </p>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,12 +2,15 @@ import * as React from 'react';
|
|||||||
import './CreateEditorApp.css';
|
import './CreateEditorApp.css';
|
||||||
import { Sidebar } from '../Sidebar';
|
import { Sidebar } from '../Sidebar';
|
||||||
import { CanvasArea } from '../CanvasArea';
|
import { CanvasArea } from '../CanvasArea';
|
||||||
import { deppCloneNaive, CanvasTextNode } from '../../Helpers';
|
import { deppCloneNaive, CanvasTextNode, CanvasImageNode } from '../../Helpers';
|
||||||
|
|
||||||
interface CreateEditorAppState {
|
interface CreateEditorAppState {
|
||||||
text: {
|
text: {
|
||||||
[nodeId: string]: CanvasTextNode;
|
[nodeId: string]: CanvasTextNode;
|
||||||
};
|
};
|
||||||
|
image: {
|
||||||
|
[nodeId: string]: CanvasImageNode;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export class CreateEditorApp extends React.Component<{}, CreateEditorAppState> {
|
export class CreateEditorApp extends React.Component<{}, CreateEditorAppState> {
|
||||||
@@ -15,10 +18,12 @@ export class CreateEditorApp extends React.Component<{}, CreateEditorAppState> {
|
|||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
text: {}
|
text: {},
|
||||||
|
image: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
this.addOrUpdateTextNode = this.addOrUpdateTextNode.bind(this);
|
this.addOrUpdateTextNode = this.addOrUpdateTextNode.bind(this);
|
||||||
|
this.addOrUpdateImageNode = this.addOrUpdateImageNode.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
addOrUpdateTextNode(nodeId: string, textNode: CanvasTextNode): void {
|
addOrUpdateTextNode(nodeId: string, textNode: CanvasTextNode): void {
|
||||||
@@ -30,10 +35,19 @@ export class CreateEditorApp extends React.Component<{}, CreateEditorAppState> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addOrUpdateImageNode(nodeId: string, imageNode: CanvasImageNode): void {
|
||||||
|
this.setState((prevState) => {
|
||||||
|
const newState = deppCloneNaive(prevState as CreateEditorAppState);
|
||||||
|
newState.image[nodeId] = imageNode;
|
||||||
|
|
||||||
|
return newState;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div className="create-editor-app">
|
<div className="create-editor-app">
|
||||||
<Sidebar addTextNode={this.addOrUpdateTextNode} />
|
<Sidebar addTextNode={this.addOrUpdateTextNode} addImageNode={this.addOrUpdateImageNode} />
|
||||||
<CanvasArea nodes={this.state} />
|
<CanvasArea nodes={this.state} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
.draggable-node {
|
.draggable-node {
|
||||||
cursor: move;
|
cursor: move;
|
||||||
user-select: none;
|
user-select: none;
|
||||||
|
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 0;
|
left: 0;
|
||||||
top: 0;
|
top: 0;
|
||||||
|
|||||||
@@ -2,10 +2,11 @@ import * as React from 'react';
|
|||||||
import './Sidebar.css';
|
import './Sidebar.css';
|
||||||
import { UploadResourcesInput } from '../UploadResourcesInput';
|
import { UploadResourcesInput } from '../UploadResourcesInput';
|
||||||
import { AddToCanvasInput } from '../AddToCanvasInput';
|
import { AddToCanvasInput } from '../AddToCanvasInput';
|
||||||
import { CanvasTextNode } from '../../Helpers';
|
import { CanvasTextNode, CanvasImageNode } from '../../Helpers';
|
||||||
|
|
||||||
interface SidebarProps {
|
interface SidebarProps {
|
||||||
addTextNode(nodeId: string, textNode: CanvasTextNode): void;
|
addTextNode(nodeId: string, textNode: CanvasTextNode): void;
|
||||||
|
addImageNode(nodeId: string, imgNode: CanvasImageNode): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SidebarState {
|
interface SidebarState {
|
||||||
@@ -20,7 +21,7 @@ export class Sidebar extends React.Component<SidebarProps, SidebarState> {
|
|||||||
return (
|
return (
|
||||||
<div className="sidebar col-sm-2 col-md-2 col-lg-2">
|
<div className="sidebar col-sm-2 col-md-2 col-lg-2">
|
||||||
<UploadResourcesInput />
|
<UploadResourcesInput />
|
||||||
<AddToCanvasInput addTextNode={this.props.addTextNode} />
|
<AddToCanvasInput addTextNode={this.props.addTextNode} addImageNode={this.props.addImageNode} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
export { DragContextProvider, DragStartFn, DragSharedInfo } from './DragContextProvider';
|
export { DragContextProvider, DragStartFn, DragSharedInfo } from './DragContextProvider';
|
||||||
export { XYPair, deppCloneNaive, CanvasTextNode } from './utils';
|
export { XYPair, deppCloneNaive, CanvasTextNode, CanvasImageNode } from './utils';
|
||||||
@@ -11,3 +11,8 @@ export interface CanvasTextNode {
|
|||||||
value: string;
|
value: string;
|
||||||
position: XYPair;
|
position: XYPair;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CanvasImageNode {
|
||||||
|
url: string;
|
||||||
|
position: XYPair;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user