Skip to content

Commit

Permalink
feat(page-change): add the ability to change the page on selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ph1p committed May 1, 2020
1 parent 997e4c9 commit b01fe05
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 44 deletions.
42 changes: 38 additions & 4 deletions src/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ async function main() {
figma.root.setPluginData('history', history);
}

// Parse History
try {
history = typeof history === 'string' ? JSON.parse(history) : []
} catch { }

return {
roomName,
secret,
history: typeof history === 'string' ? JSON.parse(history) : [],
history,
instanceId,
};
}
Expand All @@ -71,7 +76,13 @@ const getSelectionIds = () => {
};

const sendSelection = () => {
postMessage('selection', getSelectionIds());
postMessage('selection', {
page: {
id: figma.currentPage.id,
name: figma.currentPage.name
},
nodes: getSelectionIds()
});
};

const sendRootData = async ({ roomName, secret, history, instanceId }) => {
Expand Down Expand Up @@ -100,6 +111,13 @@ const isValidShape = (node) =>
node.type === 'INSTANCE' ||
node.type === 'POLYGON';


function goToPage(id) {
if (figma.getNodeById(id)) {
figma.currentPage = figma.getNodeById(id) as PageNode;
}
}

let previousSelection = figma.currentPage.selection || [];

main().then(({ roomName, secret, history, instanceId }) => {
Expand Down Expand Up @@ -198,9 +216,19 @@ main().then(({ roomName, secret, history, instanceId }) => {
}
break;
case 'focus-nodes':
let selectedNodes = [];
triggerSelectionEvent = false;

// fallback for ids
if (message.payload.ids) {
selectedNodes = message.payload.ids
} else {
goToPage(message.payload?.page?.id);
selectedNodes = message.payload.nodes;
}

const nodes = figma.currentPage.findAll(
(n) => message.payload.ids.indexOf(n.id) !== -1
(n) => selectedNodes.indexOf(n.id) !== -1
);

figma.currentPage.selection = nodes;
Expand All @@ -217,7 +245,13 @@ main().then(({ roomName, secret, history, instanceId }) => {
if (isRelaunch && !alreadyAskedForRelaunchMessage) {
alreadyAskedForRelaunchMessage = true;
postMessage('relaunch-message', {
selection: getSelectionIds(),
selection: {
page: {
id: figma.currentPage.id,
name: figma.currentPage.name
},
nodes: getSelectionIds()
},
});
}
break;
Expand Down
32 changes: 9 additions & 23 deletions src/components/Chatbar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { FunctionComponent, useState, useEffect, useRef } from 'react';
import { useRouteMatch } from 'react-router-dom';
import { observer } from 'mobx-react';
import { autorun } from 'mobx';
import styled from 'styled-components';
import ColorPicker from './ColorPicker';
import { ConnectionEnum } from '../shared/interfaces'; // store
import { observer } from 'mobx-react';
import { useStore } from '../store';
import { ConnectionEnum } from '../shared/interfaces';

interface ChatProps {
sendMessage: (event: any) => void;
Expand All @@ -17,25 +18,16 @@ interface ChatProps {
const ChatBar: FunctionComponent<ChatProps> = (props) => {
const store = useStore();
const isSettings = useRouteMatch('/settings');
const selection = store.selection.length;
const hasSelection = Boolean(selection);
const [show, setShow] = useState(hasSelection);
const [hasSelection, setHasSelection] = useState(false);
const chatTextInput = useRef(null);

const isFailed = store.status === ConnectionEnum.ERROR;
const isConnected = store.status === ConnectionEnum.CONNECTED;

useEffect(() => {
if (hasSelection) {
setShow(true);
}
}, [hasSelection]);

const onAnimationEnd = () => {
if (!hasSelection) {
setShow(false);
}
};
useEffect(
() => autorun(() => setHasSelection(Boolean(store.selectionCount))),
[]
);

return (
<ChatBarForm
Expand All @@ -46,13 +38,7 @@ const ChatBar: FunctionComponent<ChatProps> = (props) => {
}}
>
<ConnectionInfo isConnected={isConnected}>
{isFailed ? (
<>
connection failed 🙈
</>
) : (
'connecting...'
)}
{isFailed ? <>connection failed 🙈</> : 'connecting...'}
</ConnectionInfo>

<ChatInputWrapper isConnected={isConnected}>
Expand Down
44 changes: 35 additions & 9 deletions src/components/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const Message: FunctionComponent<Props> = ({ data, instanceId }) => {
const colorClass = colors[data.user.color] || 'blue';
const selection = data?.message?.selection;
const isSelf = data.id === instanceId;
const pageName = selection?.page?.name || '';

const selectionCount = selection?.length || selection?.nodes?.length || 0;

return (
<MessageFlex isSelf={isSelf}>
Expand All @@ -35,16 +38,31 @@ const Message: FunctionComponent<Props> = ({ data, instanceId }) => {
)}
{selection ? (
<span
onClick={() =>
sendMainMessage('focus-nodes', {
ids: toJS(selection),
})
}
onClick={() => {
const messageSelection = toJS(selection);
let selectionData = null;

// fallback without page
if (messageSelection.length) {
selectionData = {
ids: messageSelection,
};
} else {
selectionData = {
...messageSelection,
};
}

sendMainMessage('focus-nodes', selectionData);
}}
>
{data.message.text}
{data.message.text && (
<div className="selection-text">{data.message.text}</div>
)}
<button className="selection button button--secondary">
focus {selection.length} element
{selection.length > 1 ? 's' : ''}
{pageName ? pageName + ' - ' : ''}
focus {selectionCount} element
{selectionCount > 1 ? 's' : ''}
</button>
</span>
) : (
Expand Down Expand Up @@ -119,6 +137,14 @@ const MessageContainer = styled.div`
background-color: transparent;
border-color: #fff;
color: #fff;
&:active {
border: 1px solid;
box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.2);
}
}
.selection-text {
margin-bottom: 10px;
}
&.lightblue header {
Expand Down Expand Up @@ -188,7 +214,7 @@ const MessageContainer = styled.div`
color: #1c4856;
}
&.purple {
background-color: #7b61ff;
background-color: #5751ff;
color: #ffffff;
}
&.green {
Expand Down
12 changes: 11 additions & 1 deletion src/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,17 @@ class RootStore {
disableAutoScroll = false;

@observable
selection = [];
selection = undefined;

@computed
get selectionCount() {
// fallback
if (this.selection?.length) {
return this.selection.length;
}

return this.selection?.nodes?.length || 0;
}

@action
scrollToBottom() {
Expand Down
22 changes: 15 additions & 7 deletions src/views/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const ChatView: FunctionComponent<ChatProps> = (props) => {
date: new Date(),
};

if (store.selection.length > 0) {
if (store.selectionCount > 0) {
if (chatState.selectionIsChecked) {
data = {
...data,
Expand Down Expand Up @@ -98,8 +98,10 @@ const ChatView: FunctionComponent<ChatProps> = (props) => {
if (pmessage) {
// set selection
if (pmessage.type === 'selection') {
const hasSelection = pmessage.payload.length > 0;
store.selection = hasSelection ? pmessage.payload : [];
const hasSelection =
pmessage.payload?.length > 0 || pmessage.payload?.nodes?.length > 0;

store.selection = hasSelection ? pmessage.payload : {};

if (!hasSelection) {
chatState.selectionIsChecked = false;
Expand All @@ -111,7 +113,10 @@ const ChatView: FunctionComponent<ChatProps> = (props) => {
roomName: dataRoomName = '',
secret: dataSecret = '',
history: messages = [],
selection = [],
selection = {
page: '',
nodes: [],
},
settings = {},
instanceId = '',
} = {
Expand All @@ -135,9 +140,12 @@ const ChatView: FunctionComponent<ChatProps> = (props) => {

if (pmessage.type === 'relaunch-message') {
chatState.selectionIsChecked = true;
store.selection = pmessage.payload.selection || [];

if (store.selection.length) {
console.log(pmessage.payload.selection);

store.selection = pmessage.payload.selection || {};

if (store.selectionCount) {
sendMessage();
sendMainMessage('notify', 'Selection sent successfully');
}
Expand All @@ -164,7 +172,7 @@ const ChatView: FunctionComponent<ChatProps> = (props) => {
<>
<Chat
color={store.settings.color}
hasSelection={store.selection.length > 0}
hasSelection={store.selectionCount > 0}
>
{isConnected && (
<FloatingButtonRight isSettings={isSettings}>
Expand Down

0 comments on commit b01fe05

Please sign in to comment.