Skip to content

[UI] Drag-n-drop entire pod/scope #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ui/src/components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,14 @@ function CanvasImpl() {
let scope = getScopeAtPos(mousePos, node.id);
let toScope = scope ? scope.id : "ROOT";
const parentScope = node.parentNode ? node.parentNode : "ROOT";
if (selectedPods.size > 0) {
if (selectedPods.size > 0 && parentScope !== toScope) {
moveIntoScope(Array.from(selectedPods), toScope);
// update view manually to remove the drag highlight.
updateView();
// run auto layout on drag stop
if (autoRunLayout) {
autoLayoutROOT();
}
}
// run auto layout on drag stop
if (autoRunLayout) {
autoLayoutROOT();
}
}}
onNodeDrag={(event, node) => {
Expand Down
12 changes: 11 additions & 1 deletion ui/src/components/MyMonaco.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
useEffect(() => {
if (focusedEditor === id) {
editor?.focus();
editor?.updateOptions({
readOnly: false,
});
} else {
editor?.updateOptions({
readOnly: true,
//cursorWidth: 0,
});
}
}, [focusedEditor]);

Expand Down Expand Up @@ -464,6 +472,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
};
editor.onDidBlurEditorText(() => {
setPodBlur(id);
setFocusedEditor(undefined);
});
editor.onDidFocusEditorText(() => {
setPodFocus(id);
Expand Down Expand Up @@ -495,6 +504,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
}
},
});

// editor.onDidChangeModelContent(async (e) => {
// // content is value?
// updateGitGutter(editor);
Expand Down Expand Up @@ -539,7 +549,7 @@ export const MyMonaco = memo<MyMonacoProps>(function MyMonaco({
theme="codepod"
options={{
selectOnLineNumbers: true,
readOnly: readOnly,
readOnly: focusedEditor !== id,
// This scrollBeyondLastLine is super important. Without this, it will
// try to adjust height infinitely.
scrollBeyondLastLine: false,
Expand Down
28 changes: 25 additions & 3 deletions ui/src/components/nodes/Code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,8 @@ export const CodeNode = memo<NodeProps>(function ({
const pod = getPod(id);
const isGuest = useStore(store, (state) => state.role === "GUEST");
const cursorNode = useStore(store, (state) => state.cursorNode);
const focusedEditor = useStore(store, (state) => state.focusedEditor);
const setFocusedEditor = useStore(store, (state) => state.setFocusedEditor);
const isPodFocused = useStore(store, (state) => state.pods[id]?.focus);
const inputRef = useRef<HTMLInputElement>(null);
const updateView = useStore(store, (state) => state.updateView);
Expand Down Expand Up @@ -748,10 +750,18 @@ export const CodeNode = memo<NodeProps>(function ({
onMouseLeave={() => {
setShowToolbar(false);
}}
onClick={(e) => {
switch (e.detail) {
case 2:
setFocusedEditor(id);
break;
}
}}
sx={{
cursor: "auto",
fontSize,
}}
className={focusedEditor === id ? "nodrag" : "custom-drag-handle"}
>
{Wrap(
<Box
Expand All @@ -770,9 +780,9 @@ export const CodeNode = memo<NodeProps>(function ({
? "green"
: selected
? "#003c8f"
: !isPodFocused
: focusedEditor !== id
? "#d6dee6"
: "#5e92f3",
: "#003c8f",
}}
>
<Box
Expand Down Expand Up @@ -881,8 +891,20 @@ export const CodeNode = memo<NodeProps>(function ({
py: 1,
}}
>
<Box
sx={{
// Put it 100% the width and height, above the following components.
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: "100%",
zIndex: focusedEditor === id ? -1 : 10,
}}
>
{/* Overlay */}
</Box>
<MyMonaco id={id} fontSize={fontSize} />

<ResultBlock id={id} layout={layout} />
</Box>
</Box>
Expand Down
37 changes: 27 additions & 10 deletions ui/src/components/nodes/Rich.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,18 +259,17 @@ function HotkeyControl({ id }) {
const setFocusedEditor = useStore(store, (state) => state.setFocusedEditor);

useKeymap("Escape", () => {
if (document.activeElement) {
(document.activeElement as any).blur();
setPodBlur(id);
setCursorNode(id);
setFocusedEditor(undefined);
}
setPodBlur(id);
setCursorNode(id);
setFocusedEditor(undefined);
return true;
});
const commands = useCommands();
useEffect(() => {
if (focusedEditor === id) {
commands.focus();
} else {
commands.blur();
}
}, [focusedEditor]);
return <></>;
Expand Down Expand Up @@ -299,8 +298,11 @@ const MyEditor = ({
const provider = useStore(store, (state) => state.provider)!;

const setPodBlur = useStore(store, (state) => state.setPodBlur);
const focusedEditor = useStore(store, (state) => state.focusedEditor);
const setFocusedEditor = useStore(store, (state) => state.setFocusedEditor);
const resetSelection = useStore(store, (state) => state.resetSelection);
const updateView = useStore(store, (state) => state.updateView);
const editable = !isGuest && focusedEditor === id;
const { manager, state, setState } = useRemirror({
extensions: () => [
new PlaceholderExtension({ placeholder }),
Expand All @@ -310,7 +312,10 @@ const MyEditor = ({
new SupExtension(),
new SubExtension(),
new MarkdownExtension(),
new MyYjsExtension({ getProvider: () => provider, id }),
// YjsExtension seems to be incompatible with editable=false, throwing console errors.
...(editable
? [new MyYjsExtension({ getProvider: () => provider, id })]
: []),
new MathInlineExtension(),
new MathBlockExtension(),
// new CalloutExtension({ defaultType: "warn" }),
Expand Down Expand Up @@ -386,10 +391,12 @@ const MyEditor = ({
className="remirror-theme"
onFocus={() => {
setPodFocus(id);
setFocusedEditor(id);
if (resetSelection()) updateView();
}}
onBlur={() => {
setPodBlur(id);
setFocusedEditor(undefined);
}}
sx={{
userSelect: "text",
Expand Down Expand Up @@ -422,7 +429,7 @@ const MyEditor = ({
// - [1] https://remirror.io/docs/controlled-editor
// - [2] demo that Chinese input method is not working:
// https://remirror.vercel.app/?path=/story/editors-controlled--editable
editable={!isGuest}
editable={editable}
>
<HotkeyControl id={id} />
{/* <WysiwygToolbar /> */}
Expand Down Expand Up @@ -520,6 +527,8 @@ export const RichNode = memo<Props>(function ({
const isGuest = useStore(store, (state) => state.role === "GUEST");
const width = useStore(store, (state) => state.pods[id]?.width);
const cursorNode = useStore(store, (state) => state.cursorNode);
const focusedEditor = useStore(store, (state) => state.focusedEditor);
const setFocusedEditor = useStore(store, (state) => state.setFocusedEditor);
const isPodFocused = useStore(store, (state) => state.pods[id]?.focus);
const devMode = useStore(store, (state) => state.devMode);
const inputRef = useRef<HTMLInputElement>(null);
Expand Down Expand Up @@ -685,10 +694,18 @@ export const RichNode = memo<Props>(function ({
(elem as HTMLElement).style.display = "none";
});
}}
onClick={(e) => {
switch (e.detail) {
case 2:
setFocusedEditor(id);
break;
}
}}
sx={{
cursor: "auto",
fontSize,
}}
className={focusedEditor === id ? "nodrag" : "custom-drag-handle"}
>
{" "}
{Wrap(
Expand All @@ -704,9 +721,9 @@ export const RichNode = memo<Props>(function ({
? "green"
: selected
? "#003c8f"
: !isPodFocused
: focusedEditor !== id
? "#d6dee6"
: "#5e92f3",
: "#003c8f",
}}
>
<Box
Expand Down
4 changes: 2 additions & 2 deletions ui/src/components/nodes/Scope.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ function MyFloatingToolbar({ id }: { id: string }) {
const store = useContext(RepoContext);
if (!store) throw new Error("Missing BearContext.Provider in the tree");
const reactFlowInstance = useReactFlow();
// const selected = useStore(store, (state) => state.pods[id]?.selected);
const isGuest = useStore(store, (state) => state.role === "GUEST");
const wsRunScope = useStore(store, (state) => state.wsRunScope);
const clonePod = useStore(store, (state) => state.clonePod);
Expand Down Expand Up @@ -210,7 +209,6 @@ export const ScopeNode = memo<NodeProps>(function ScopeNode({
if (!store) throw new Error("Missing BearContext.Provider in the tree");
const setPodName = useStore(store, (state) => state.setPodName);
const nodesMap = useStore(store, (state) => state.ydoc.getMap<Node>("pods"));
// const selected = useStore(store, (state) => state.pods[id]?.selected);
const isGuest = useStore(store, (state) => state.role === "GUEST");
const inputRef = useRef<HTMLInputElement>(null);
const getPod = useStore(store, (state) => state.getPod);
Expand Down Expand Up @@ -293,6 +291,7 @@ export const ScopeNode = memo<NodeProps>(function ScopeNode({
width: "100%",
height: "100%",
border: isCutting ? "dashed 2px red" : "solid 1px #d6dee6",
borderColor: selected ? "#003c8f" : undefined,
borderRadius: "4px",
cursor: "auto",
fontSize,
Expand All @@ -303,6 +302,7 @@ export const ScopeNode = memo<NodeProps>(function ScopeNode({
onMouseLeave={() => {
setShowToolbar(false);
}}
className="custom-drag-handle"
>
{/* <NodeResizer color="#ff0071" minWidth={100} minHeight={30} /> */}
<Box sx={{ opacity: showToolbar ? 1 : 0 }}>
Expand Down