Skip to content
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

Highlight write to code #409

Merged
merged 4 commits into from
Sep 26, 2024
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
2 changes: 1 addition & 1 deletion app/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const DefaultSettings = {
FRAME_DIMENSION: { width: 1536, height: 960 },
};

export const DOM_IGNORE_TAGS = ['SCRIPT', 'STYLE'];
export const DOM_IGNORE_TAGS = ['SCRIPT', 'STYLE', 'LINK', 'META', 'NOSCRIPT'];

export const INLINE_ONLY_CONTAINERS = new Set([
'a',
Expand Down
13 changes: 10 additions & 3 deletions app/electron/preload/webview/elements/move/drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { assignUniqueId, getDomElement, restoreElementStyle, saveTimestamp } fro
import { getDisplayDirection, moveElToIndex } from './helpers';
import { createStub, getCurrentStubIndex, moveStub, removeStub } from './stub';
import { EditorAttributes } from '/common/constants';
import { getOnlookUniqueSelector, getUniqueSelector } from '/common/helpers';
import { getOnlookUniqueSelector, getUniqueSelector, isValidHtmlElement } from '/common/helpers';

export function startDrag(selector: string): number {
const el = document.querySelector(selector) as HTMLElement | null;
if (!el) {
console.error(`Start drag element not found: ${selector}`);
return -1;
}
const originalIndex = Array.from(el.parentElement!.children).indexOf(el);
const parent = el.parentElement;
if (!parent) {
console.error('Start drag parent not found');
return -1;
}
const htmlChildren = Array.from(parent.children).filter(isValidHtmlElement);
const originalIndex = htmlChildren.indexOf(el);
prepareElementForDragging(el, originalIndex);
createStub(el);
return originalIndex;
Expand Down Expand Up @@ -52,7 +58,8 @@ export function endDrag(): { newSelector: string; newIndex: number } | undefined
}
removeStub();

const newIndex = Array.from(parent.children).indexOf(el);
const htmlChildren = Array.from(parent.children).filter(isValidHtmlElement);
const newIndex = htmlChildren.indexOf(el);

cleanUpElementAfterDragging(el, newIndex);

Expand Down
11 changes: 9 additions & 2 deletions app/electron/preload/webview/elements/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function editTextBySelector(selector: string, content: string): TextDomEl
if (!el) {
return null;
}
el.textContent = content;
updateTextContent(el, content);
return getTextEditElement(el);
}

Expand Down Expand Up @@ -64,7 +64,7 @@ export function editText(content: string): TextDomElement | null {
if (!el) {
return null;
}
el.textContent = content;
updateTextContent(el, content);
return getTextEditElement(el);
}

Expand Down Expand Up @@ -132,3 +132,10 @@ export function clearTextEditedElements() {
el.removeAttribute(EditorAttributes.DATA_ONLOOK_ORIGINAL_CONTENT);
}
}

function updateTextContent(el: HTMLElement, content: string): void {
if (!el.hasAttribute(EditorAttributes.DATA_ONLOOK_ORIGINAL_CONTENT)) {
el.setAttribute(EditorAttributes.DATA_ONLOOK_ORIGINAL_CONTENT, el.textContent || '');
}
el.textContent = content;
}
6 changes: 3 additions & 3 deletions app/src/lib/editor/engine/code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ export class CodeManager {
}

private async getInsertedElements(webview: Electron.WebviewTag): Promise<InsertedElement[]> {
return webview.executeJavaScript(`window.api?.getInsertedElements()`);
return webview.executeJavaScript(`window.api?.getInsertedElements()`) || [];
}

private async getMovedElements(webview: Electron.WebviewTag): Promise<MovedElement[]> {
return webview.executeJavaScript(`window.api?.getMovedElements()`);
return webview.executeJavaScript(`window.api?.getMovedElements()`) || [];
}

private async getTextEditElements(webview: Electron.WebviewTag): Promise<TextEditedElement[]> {
return webview.executeJavaScript(`window.api?.getTextEditedElements()`);
return webview.executeJavaScript(`window.api?.getTextEditedElements()`) || [];
}

private async getCodeDiffRequests(
Expand Down
62 changes: 45 additions & 17 deletions app/src/routes/editor/TopBar/OpenCode/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEditorEngine } from '@/components/Context';
import { useEditorEngine, useProjectsManager } from '@/components/Context';
import {
DropdownMenu,
DropdownMenuContent,
Expand All @@ -10,6 +10,7 @@ import {
CheckCircledIcon,
Component1Icon,
ComponentInstanceIcon,
FileIcon,
GearIcon,
} from '@radix-ui/react-icons';
import { observer } from 'mobx-react-lite';
Expand All @@ -22,10 +23,24 @@ import { UserSettings } from '/common/models/settings';

const OpenCode = observer(() => {
const editorEngine = useEditorEngine();
const projectManager = useProjectsManager();

const [folder, setFolder] = useState<TemplateNode | undefined>();
const [instance, setInstance] = useState<TemplateNode | undefined>();
const [root, setRoot] = useState<TemplateNode | undefined>();
const [ide, setIde] = useState<IDE>(IDE.VS_CODE);

useEffect(() => {
if (projectManager.project) {
const folder = projectManager.project.folderPath;
const folderTemplateNode: TemplateNode = {
path: folder,
startTag: { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } },
};
setFolder(folderTemplateNode);
}
}, []);

useEffect(() => {
window.api.invoke(MainChannels.GET_USER_SETTINGS).then((res) => {
const settings: UserSettings = res as UserSettings;
Expand Down Expand Up @@ -62,12 +77,12 @@ const OpenCode = observer(() => {
<DropdownMenuTrigger
className="flex flex-row items-center mr-2"
asChild
disabled={!instance}
disabled={!instance && !root}
>
<button
className="flex items-center text-smallPlus justify-center disabled:text-text h-full w-full min-w-[7.5rem] my-1 pl-2.5 hover:text-text-active/90"
disabled={!instance && !root}
onClick={() => viewSource(instance || root)}
disabled={!folder && !instance && !root}
onClick={() => viewSource(folder || instance || root)}
>
<span className="text-default h-3 w-3 mr-2">
<img src={ide.icon} alt={`${ide} Icon`} />
Expand All @@ -79,27 +94,40 @@ const OpenCode = observer(() => {
<DropdownMenuItem
className="text-xs"
onSelect={() => {
viewSource(instance);
}}
>
<ComponentInstanceIcon className="mr-2 w-3 h-3" />
Instance
</DropdownMenuItem>
<DropdownMenuItem
className="text-xs"
onSelect={() => {
viewSource(root);
viewSource(folder);
}}
>
<Component1Icon className="mr-2 w-3 h-3" />
Component
<FileIcon className="mr-2 w-3 h-3" />
Folder
</DropdownMenuItem>
{instance && (
<DropdownMenuItem
className="text-xs"
onSelect={() => {
viewSource(instance);
}}
>
<ComponentInstanceIcon className="mr-2 w-3 h-3" />
Instance
</DropdownMenuItem>
)}
{root && (
<DropdownMenuItem
className="text-xs"
onSelect={() => {
viewSource(root);
}}
>
<Component1Icon className="mr-2 w-3 h-3" />
Component
</DropdownMenuItem>
)}
</DropdownMenuContent>
</DropdownMenu>
</div>
</TooltipTrigger>
<TooltipContent side="bottom" className="mt-2">
<p>Open selected element in IDE</p>
<p>Open {instance || root ? 'selected element' : 'folder'} in IDE</p>
</TooltipContent>
</Tooltip>

Expand Down
46 changes: 33 additions & 13 deletions app/src/routes/editor/TopBar/PublishModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
import { useToast } from '@/components/ui/use-toast';
import { sendAnalytics } from '@/lib/utils';
import { CodeIcon, ExternalLinkIcon, ShadowIcon } from '@radix-ui/react-icons';
import clsx from 'clsx';
import { debounce } from 'lodash';
import { observer } from 'mobx-react-lite';
import { useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import ReactDiffViewer, { DiffMethod } from 'react-diff-viewer-continued';
import { MainChannels, WebviewChannels } from '/common/constants';
import { CodeDiff } from '/common/models/code';
Expand All @@ -22,21 +24,32 @@ import { TemplateNode } from '/common/models/element/templateNode';
const PublishModal = observer(() => {
const editorEngine = useEditorEngine();
const { toast } = useToast();

const [isOpen, setIsOpen] = useState(false);
const [isLoadingCodeDiff, setIsLoadingCodeDiff] = useState(false);
const [isWriting, setIsWriting] = useState(false);
const [codeDiffs, setCodeDiffs] = useState<CodeDiff[]>([]);

async function handleOpenChange(open: boolean) {
setIsOpen(open);
if (open) {
setIsLoadingCodeDiff(true);
const res = await editorEngine.code.generateCodeDiffs();
setIsLoadingCodeDiff(false);
setCodeDiffs(res);
const generateCodeDiffs = useCallback(async () => {
if (isLoadingCodeDiff) {
console.log('Already loading code diff');
return;
}
}
setIsLoadingCodeDiff(true);
const res = await editorEngine.code.generateCodeDiffs();
setCodeDiffs(res);
setIsLoadingCodeDiff(false);
}, [editorEngine.code, isLoadingCodeDiff]);

const debouncedGenerateCodeDiffs = useCallback(debounce(generateCodeDiffs, 1000), [
generateCodeDiffs,
]);

useEffect(() => {
debouncedGenerateCodeDiffs();
return () => {
debouncedGenerateCodeDiffs.cancel();
};
}, [editorEngine.history.length]);

function viewSource(path: string) {
const templateNode = createTemplateNodeFromPath(path);
Expand Down Expand Up @@ -96,10 +109,17 @@ const PublishModal = observer(() => {
);
}
return (
<Dialog open={isOpen} onOpenChange={handleOpenChange}>
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button variant="outline" size="sm" className="rounded-md text-smallPlus">
<CodeIcon className="mr-2" /> Review & Publish Code
<Button
variant="outline"
size="sm"
className={clsx(
'rounded-md text-smallPlus transition border-[0.5px] border-transparent',
codeDiffs.length === 0 ? '' : 'bg-teal-500 border-teal-200',
)}
>
<CodeIcon className="mr-2" /> Review & Write Code
</Button>
</DialogTrigger>
<DialogContent className="min-w-[60vw] max-h-[80vh]">
Expand Down