Skip to content

Commit

Permalink
Improve cut and paste
Browse files Browse the repository at this point in the history
  • Loading branch information
dabbott committed Mar 23, 2023
1 parent 09e7f47 commit b8691b4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 11 deletions.
5 changes: 4 additions & 1 deletion packages/noya-canvas/src/components/SimpleCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export const SimpleCanvas = memo(
useCopyHandler();

usePasteHandler<SupportedImageUploadType>({
onPasteLayers: actions.addLayer,
onPasteLayers: (layers) =>
actions.addLayer(layers, {
useOriginalDimensions: true,
}),
});

// When canvasSize changes, zoom to fit the isolated layer
Expand Down
17 changes: 14 additions & 3 deletions packages/noya-canvas/src/hooks/useCopyHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type NoyaClipboardData = {
};

export function useCopyHandler() {
const [state] = useApplicationState();
const [state, dispatch] = useApplicationState();
const selectedLayers = getSelectedLayers(state);

useEffect(() => {
Expand All @@ -31,10 +31,21 @@ export function useCopyHandler() {
'text/html',
ClipboardUtils.toEncodedHTML(data),
);

if (event.type === 'cut') {
dispatch(
'deleteLayer',
selectedLayers.map((layer) => layer.do_objectID),
);
}
};

document.addEventListener('copy', handler);
document.addEventListener('cut', handler);

return () => document.removeEventListener('copy', handler);
}, [selectedLayers]);
return () => {
document.removeEventListener('copy', handler);
document.removeEventListener('cut', handler);
};
}, [dispatch, selectedLayers]);
}
2 changes: 1 addition & 1 deletion packages/noya-canvas/src/hooks/useInteractionHandlers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function useInteractionHandlers({
setLayerY: (value: number, mode: SetNumberMode) =>
dispatch('setLayerY', value, mode),
selectAllLayers: () => dispatch('selectAllLayers'),
addLayer: (layer) => dispatch('addLayer', layer),
addLayer: (layer, options) => dispatch('addLayer', layer, options),
highlightLayer: (layerHighlight) =>
dispatch('highlightLayer', layerHighlight),
enterInsertMode: (layerType, method) =>
Expand Down
8 changes: 6 additions & 2 deletions packages/noya-canvas/src/interactions/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { ReactEventHandlers } from 'noya-designsystem';
import Sketch from 'noya-file-format';
import { handleActionType, InteractionState } from 'noya-state';
import {
AddLayerOptions,
handleActionType,
InteractionState,
} from 'noya-state';
import { ClipboardUtils } from 'noya-utils';
import { NoyaClipboardData } from '../hooks/useCopyHandler';
import { InteractionAPI } from './types';

export interface ClipboardActions {
addLayer: (layers: Sketch.AnyLayer[]) => void;
addLayer: (layers: Sketch.AnyLayer[], options?: AddLayerOptions) => void;
}

type MenuItemType = 'copy' | 'paste';
Expand Down
1 change: 1 addition & 0 deletions packages/noya-state/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './reducers/blockReducer';
export * from './reducers/canvasReducer';
export * from './reducers/historyReducer';
export * from './reducers/interactionReducer';
export type { AddLayerOptions } from './reducers/layerReducer';
export type { SelectedPoint } from './reducers/pointReducer';
export * from './reducers/styleReducer';
export * from './reducers/workspaceReducer';
Expand Down
17 changes: 15 additions & 2 deletions packages/noya-state/src/reducers/layerReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ import { createPage } from '../selectors/pageSelectors';
import { SelectionType, updateSelection } from '../utils/selection';
import type { ApplicationState } from './applicationReducer';

export type AddLayerOptions = {
point?: Point;
useOriginalDimensions?: boolean;
};

export type LayerAction =
| [
type: 'moveLayer',
Expand All @@ -62,7 +67,11 @@ export type LayerAction =
| [type: 'detachSymbol', layerId: string | string[]]
| [type: 'deleteSymbol', ids: string[]]
| [type: 'duplicateLayer', ids: string[]]
| [type: 'addLayer', data: Sketch.AnyLayer | Sketch.AnyLayer[], point?: Point]
| [
type: 'addLayer',
data: Sketch.AnyLayer | Sketch.AnyLayer[],
options?: AddLayerOptions,
]
| [
type: 'selectLayer',
layerId: string | string[] | undefined,
Expand Down Expand Up @@ -469,7 +478,9 @@ export function layerReducer(
});
}
case 'addLayer': {
const [, layer, point] = action;
const [, layer, options = {}] = action;
const { useOriginalDimensions = false, point } = options;

const layers = Array.isArray(layer) ? layer : [layer];
const currentPageIndex = getCurrentPageIndex(state);
const selectedLayerIndexPath =
Expand Down Expand Up @@ -556,6 +567,8 @@ export function layerReducer(
});

newLayer = produce(newLayer, (draftLayer) => {
if (useOriginalDimensions) return;

const targetPoint = point
? point
: { x: parentBounds.midX, y: parentBounds.midY };
Expand Down
2 changes: 1 addition & 1 deletion packages/site/src/ayon/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const Content = memo(function Content({
'canvas',
);

dispatch('addLayer', layers, point);
dispatch('addLayer', layers, { point });
};

useEffect(() => {
Expand Down
6 changes: 5 additions & 1 deletion packages/site/src/ayon/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,11 @@ export const Widget = forwardRef(function Widget(
layersToInsert.forEach((child) => {
const bounds = createBounds(child.frame);

actions.push(['addLayer', child, { x: bounds.midX, y: bounds.midY }]);
actions.push([
'addLayer',
child,
{ point: { x: bounds.midX, y: bounds.midY } },
]);
});

dispatch('batch', actions);
Expand Down

0 comments on commit b8691b4

Please sign in to comment.