fix a bunch of things around import and export

This commit is contained in:
DD 2017-10-16 19:31:30 -04:00
parent c49a469eda
commit 05d7b806a6
3 changed files with 39 additions and 17 deletions

View file

@ -29,8 +29,9 @@ class PaintEditor extends React.Component {
document.removeEventListener('keydown', this.props.onKeyPress); document.removeEventListener('keydown', this.props.onKeyPress);
} }
handleUpdateSvg (skipSnapshot) { handleUpdateSvg (skipSnapshot) {
// Hide bounding box // Hide guide layer
getGuideLayer().visible = false; const guideLayer = getGuideLayer();
guideLayer.remove();
const bounds = paper.project.activeLayer.bounds; const bounds = paper.project.activeLayer.bounds;
this.props.onUpdateSvg( this.props.onUpdateSvg(
paper.project.exportSVG({ paper.project.exportSVG({
@ -42,7 +43,7 @@ class PaintEditor extends React.Component {
if (!skipSnapshot) { if (!skipSnapshot) {
performSnapshot(this.props.undoSnapshot); performSnapshot(this.props.undoSnapshot);
} }
getGuideLayer().visible = true; paper.project.addLayer(guideLayer);
} }
handleUndo () { handleUndo () {
performUndo(this.props.undoState, this.props.onUndo, this.handleUpdateSvg); performUndo(this.props.undoState, this.props.onUndo, this.handleUpdateSvg);

View file

@ -5,7 +5,8 @@ import {connect} from 'react-redux';
import paper from '@scratch/paper'; import paper from '@scratch/paper';
import {performSnapshot} from '../helper/undo'; import {performSnapshot} from '../helper/undo';
import {undoSnapshot} from '../reducers/undo'; import {undoSnapshot, clearUndoState} from '../reducers/undo';
import {isGroup, ungroupItems} from '../helper/group';
import styles from './paper-canvas.css'; import styles from './paper-canvas.css';
@ -23,17 +24,24 @@ class PaperCanvas extends React.Component {
paper.settings.handleSize = 0; paper.settings.handleSize = 0;
if (this.props.svg) { if (this.props.svg) {
this.importSvg(this.props.svg, this.props.rotationCenterX, this.props.rotationCenterY); this.importSvg(this.props.svg, this.props.rotationCenterX, this.props.rotationCenterY);
} else {
performSnapshot(this.props.undoSnapshot);
} }
performSnapshot(this.props.undoSnapshot);
} }
componentWillReceiveProps (newProps) { componentWillReceiveProps (newProps) {
paper.project.activeLayer.removeChildren(); for (const layer of paper.project.layers) {
this.importSvg(newProps.svg, newProps.rotationCenterX, newProps.rotationCenterY); layer.removeChildren();
}
this.props.clearUndo();
if (newProps.svg) {
this.importSvg(newProps.svg, newProps.rotationCenterX, newProps.rotationCenterY);
}
} }
componentWillUnmount () { componentWillUnmount () {
paper.remove(); paper.remove();
} }
importSvg (svg, rotationCenterX, rotationCenterY) { importSvg (svg, rotationCenterX, rotationCenterY) {
const paperCanvas = this;
paper.project.importSVG(svg, { paper.project.importSVG(svg, {
expandShapes: true, expandShapes: true,
onLoad: function (item) { onLoad: function (item) {
@ -67,7 +75,11 @@ class PaperCanvas extends React.Component {
// Center // Center
item.position = paper.project.view.center; item.position = paper.project.view.center;
} }
if (isGroup(item)) {
ungroupItems([item]);
}
performSnapshot(paperCanvas.props.undoSnapshot);
paper.project.view.update(); paper.project.view.update();
} }
}); });
@ -92,6 +104,7 @@ class PaperCanvas extends React.Component {
PaperCanvas.propTypes = { PaperCanvas.propTypes = {
canvasRef: PropTypes.func, canvasRef: PropTypes.func,
clearUndo: PropTypes.func.isRequired,
rotationCenterX: PropTypes.number, rotationCenterX: PropTypes.number,
rotationCenterY: PropTypes.number, rotationCenterY: PropTypes.number,
svg: PropTypes.string, svg: PropTypes.string,
@ -100,6 +113,9 @@ PaperCanvas.propTypes = {
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = dispatch => ({
undoSnapshot: snapshot => { undoSnapshot: snapshot => {
dispatch(undoSnapshot(snapshot)); dispatch(undoSnapshot(snapshot));
},
clearUndo: () => {
dispatch(clearUndoState());
} }
}); });

View file

@ -23,37 +23,41 @@ const groupSelection = function (clearSelectedItems) {
return false; return false;
}; };
const ungroupLoop = function (group, recursive) { const ungroupLoop = function (group, recursive, selectUngroupedItems) {
// don't ungroup items that are not groups // Can't ungroup items that are not groups
if (!group || !group.children || !isGroup(group)) return; if (!group || !group.children || !isGroup(group)) return;
group.applyMatrix = true; group.applyMatrix = true;
// iterate over group children recursively // iterate over group children recursively
for (let i = 0; i < group.children.length; i++) { for (let i = 0; i < group.children.length; i++) {
const groupChild = group.children[i]; let groupChild = group.children[i];
if (groupChild.hasChildren()) { if (groupChild.hasChildren()) {
// recursion (groups can contain groups, ie. from SVG import) // recursion (groups can contain groups, ie. from SVG import)
if (recursive) { if (recursive) {
ungroupLoop(groupChild, true /* recursive */); ungroupLoop(groupChild, recursive, selectUngroupedItems);
continue; continue;
} }
if (groupChild.children.length === 1) {
groupChild = groupChild.reduce();
}
} }
groupChild.applyMatrix = true; groupChild.applyMatrix = true;
// move items from the group to the activeLayer (ungrouping) // move items from the group to the activeLayer (ungrouping)
groupChild.insertBelow(group); groupChild.insertBelow(group);
groupChild.selected = true; if (selectUngroupedItems) {
groupChild.selected = true;
}
i--; i--;
} }
}; };
// ungroup items (only top hierarchy) // ungroup items (only top hierarchy)
const ungroupItems = function (items, clearSelectedItems) { const ungroupItems = function (items, selectUngroupedItems) {
clearSelection(clearSelectedItems);
const emptyGroups = []; const emptyGroups = [];
for (let i = 0; i < items.length; i++) { for (let i = 0; i < items.length; i++) {
const item = items[i]; const item = items[i];
if (isGroup(item) && !item.data.isPGTextItem) { if (isGroup(item) && !item.data.isPGTextItem) {
ungroupLoop(item, false /* recursive */); ungroupLoop(item, false /* recursive */, selectUngroupedItems /* selectUngroupedItems */);
if (!item.hasChildren()) { if (!item.hasChildren()) {
emptyGroups.push(item); emptyGroups.push(item);
@ -70,9 +74,10 @@ const ungroupItems = function (items, clearSelectedItems) {
// pg.undo.snapshot('ungroupItems'); // pg.undo.snapshot('ungroupItems');
}; };
const ungroupSelection = function () { const ungroupSelection = function (clearSelectedItems) {
const items = getSelectedRootItems(); const items = getSelectedRootItems();
ungroupItems(items); clearSelection(clearSelectedItems);
ungroupItems(items, true /* selectUngroupedItems */);
}; };