-
Notifications
You must be signed in to change notification settings - Fork 0
playground store 생성 및 탭 추가/닫기, 탭 활성화, 탭 이동 히스토리 기능 구현 #9
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a6a34c6
feat: playground store 생성
Bori-github 599ba3d
feat: 탭 추가 기능 스토어 로직으로 변경 및 탭 닫기 기능 임시 주석처리
Bori-github f07e255
feat: 탭 닫기 기능 생성 및 playgrounds Map객체로 변경
Bori-github 2574e9c
feat: 탭 닫기 기능 스토어 로직으로 변경
Bori-github fad6d5b
feat: playground-page 컴포넌트 복제하여 playground widget 생성
Bori-github 4ef6a7d
feat: 활성화된 탭에 따른 playground widget 컴포넌트 렌더링
Bori-github ebed61a
feat: 클릭한 탭 제목 클릭 시 탭 활성화 기능
Bori-github 95004dd
refactor: 활성화된 탭 id 저장하여 탭 활성화 여부 로직 변경
Bori-github 58a0554
feat: 탭 이동 히스토리 스토어에 추가 및 탭 닫기 시 이전 탭 활성화 처리
Bori-github 5435b08
fix: 사용하지 않는 값 제거
Bori-github 6dcd82f
chore: 탭 히스토리의 마지막 탭과 현재 선택한 탭이 동일한 경우, 탭 히스토리에 중복으로 쌓이지 않도록 처리
Bori-github e385cec
chore: 불필요한 partialize 옵션 제거
Bori-github File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './store'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { JsExecutionResult } from '@/shared'; | ||
| import { create } from 'zustand'; | ||
| import { persist, createJSONStorage } from 'zustand/middleware'; | ||
|
|
||
| interface Tab { | ||
| id: string; | ||
| playgroundId: Playground['id']; | ||
| title: string; | ||
| } | ||
|
|
||
| export interface Playground { | ||
| id: string; | ||
| result: JsExecutionResult | null; | ||
| isExecuting: boolean; | ||
| // setExecuting: (executing: boolean) => void; | ||
| // executeCode: (code: string) => void; | ||
| // clearResult: () => void; | ||
| } | ||
|
|
||
| interface PlaygroundState { | ||
| tabs: Tab[]; | ||
| activeTabId: Tab['id']; | ||
| tabHistory: Tab['id'][]; | ||
| playgrounds: Map<Playground['id'], Playground>; | ||
| addTab: () => void; | ||
| closeTab: (tabId: Tab['id']) => void; | ||
| setActiveTab: (tabId: Tab['id']) => void; | ||
| } | ||
|
|
||
| const INITIAL_TAB_TITLE = '✨New Playground'; | ||
| const INITIAL_TAB_ID = 'first-playground-tab'; | ||
| const INITIAL_PLAYGROUND_ID = 'first-playground'; | ||
|
|
||
| const initialTab: Tab = { | ||
| id: INITIAL_TAB_ID, | ||
| playgroundId: INITIAL_PLAYGROUND_ID, | ||
| title: INITIAL_TAB_TITLE, | ||
| }; | ||
|
|
||
| export const usePlaygroundStore = create<PlaygroundState>()( | ||
| persist( | ||
| (set) => ({ | ||
| tabs: [initialTab], | ||
| activeTabId: INITIAL_TAB_ID, | ||
| tabHistory: [INITIAL_TAB_ID], | ||
| playgrounds: new Map([ | ||
| [ | ||
| INITIAL_PLAYGROUND_ID, | ||
| { id: INITIAL_PLAYGROUND_ID, result: null, isExecuting: false }, | ||
| ], | ||
| ]), | ||
|
|
||
| // 탭 추가 | ||
| addTab: () => { | ||
| set((state) => { | ||
| const date = new Date().valueOf(); | ||
|
|
||
| const newTabId = `playground-tab-${date}`; | ||
| const newPlaygroundId = `playground-${date}`; | ||
|
|
||
| const newTab: Tab = { | ||
| id: newTabId, | ||
| playgroundId: newPlaygroundId, | ||
| title: INITIAL_TAB_TITLE, | ||
| }; | ||
|
|
||
| const newPlaygrounds = new Map(state.playgrounds); | ||
| newPlaygrounds.set(newPlaygroundId, { | ||
| id: newPlaygroundId, | ||
| result: null, | ||
| isExecuting: false, | ||
| }); | ||
|
|
||
| return { | ||
| tabs: [...state.tabs, newTab], | ||
| activeTabId: newTabId, | ||
| tabHistory: [...state.tabHistory, newTabId], | ||
| playgrounds: newPlaygrounds, | ||
| }; | ||
| }); | ||
| }, | ||
|
|
||
| // 탭 닫기 | ||
| closeTab: (tabId: Tab['id']) => { | ||
| set((state) => { | ||
| const closingTab = state.tabs.find((tab) => tab.id === tabId); | ||
| const tabsLength = state.tabs.length; | ||
|
|
||
| if (!closingTab || tabsLength === 1) return state; | ||
|
|
||
| const tabs = state.tabs.filter((tab) => tab.id !== tabId); | ||
| const tabHistory = state.tabHistory.filter((id) => id !== tabId); | ||
| const lastActiveTabId = | ||
| tabHistory[tabHistory.length - 1] || tabs[0].id; | ||
Bori-github marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const playgrounds = new Map(state.playgrounds); | ||
|
|
||
| playgrounds.delete(closingTab.playgroundId); | ||
|
|
||
| return { | ||
| tabs, | ||
| activeTabId: lastActiveTabId, | ||
| tabHistory, | ||
| playgrounds, | ||
| }; | ||
| }); | ||
| }, | ||
|
|
||
| // 탭 활성화 | ||
| setActiveTab: (tabId: Tab['id']) => { | ||
| set((state) => { | ||
| const lastTabId = state.tabHistory[state.tabHistory.length - 1]; | ||
|
|
||
| if (lastTabId === tabId) { | ||
| return state; | ||
| } | ||
|
|
||
| return { | ||
| activeTabId: tabId, | ||
| tabHistory: [...state.tabHistory, tabId], | ||
| }; | ||
| }); | ||
| }, | ||
| }), | ||
| { | ||
| name: 'executejs-playground-store', | ||
| storage: createJSONStorage(() => localStorage, { | ||
| replacer: (_, value) => { | ||
| if (value instanceof Map) { | ||
| return { | ||
| _type: 'map', | ||
| value: Array.from(value.entries()), | ||
| }; | ||
| } | ||
| return value; | ||
| }, | ||
| reviver: (_, value: any) => { | ||
| if (value && value._type === 'map') { | ||
| return new Map(value.value); | ||
| } | ||
| return value; | ||
| }, | ||
| }), | ||
| } | ||
| ) | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './playground-widget'; |
138 changes: 138 additions & 0 deletions
138
apps/executeJS/src/widgets/playground/playground-widget.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import React, { useState } from 'react'; | ||
|
|
||
| import { Panel, PanelGroup, PanelResizeHandle } from 'react-resizable-panels'; | ||
| import { PlayIcon, StopIcon } from '@radix-ui/react-icons'; | ||
|
|
||
| import { CodeEditor } from '@/widgets/code-editor'; | ||
| import { OutputPanel } from '@/widgets/output-panel'; | ||
| import { useExecutionStore } from '@/features/execute-code'; | ||
| import { Playground } from '@/features/playground'; | ||
|
|
||
| const getInitialCode = (): string => { | ||
| try { | ||
| const executionStorage = localStorage.getItem( | ||
| 'executejs-execution-storage' | ||
| ); | ||
|
|
||
| if (executionStorage) { | ||
| const parsed = JSON.parse(executionStorage); | ||
| const code = parsed?.state?.result?.code; | ||
|
|
||
| if (code) { | ||
| console.log('result from executionStorage:', code); | ||
|
|
||
| return code; | ||
| } | ||
| } | ||
| } catch (error) { | ||
| console.error('error from executionStorage:', error); | ||
| } | ||
|
|
||
| return 'console.log("Hello, ExecuteJS!");'; | ||
| }; | ||
|
|
||
| interface PlaygroundProps { | ||
| playground: Playground; | ||
| } | ||
|
|
||
| export const PlaygroundWidget: React.FC<PlaygroundProps> = ({ playground }) => { | ||
| // TODO: playground prop 로직 추가 예정 @bori | ||
| console.log('PlaygroundWidget', playground); | ||
|
|
||
| // FIXME: tab이 여러개 생기거나 global store로 상태가 이동되면 수정되어야함 | ||
| const [code, setCode] = useState(getInitialCode); | ||
Bori-github marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const { | ||
| result: executionResult, | ||
| isExecuting, | ||
| executeCode, | ||
| } = useExecutionStore(); | ||
Bori-github marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // 코드 실행 핸들러 | ||
| const handleExecuteCode = (codeToExecute?: string) => { | ||
| const codeToRun = codeToExecute || code; | ||
| if (codeToRun.trim()) { | ||
| executeCode(codeToRun); | ||
| } | ||
| }; | ||
|
|
||
| // 코드 변경 핸들러 | ||
| const handleCodeChange = (newCode: string) => { | ||
| setCode(newCode); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="h-screen w-screen flex flex-col bg-slate-950 text-white"> | ||
| {/* 헤더 */} | ||
| <div className="flex items-center justify-between px-4 py-3 bg-slate-900 border-b border-slate-800"> | ||
| <div className="flex items-center gap-3"> | ||
| <div className="text-sm font-medium text-slate-300">ExecuteJS</div> | ||
| </div> | ||
|
|
||
| <div className="flex items-center gap-2"> | ||
| <button | ||
| onClick={() => handleExecuteCode()} | ||
| disabled={isExecuting || !code.trim()} | ||
| className="flex items-center gap-2 px-3 py-1.5 bg-blue-600 hover:bg-blue-700 disabled:bg-slate-700 disabled:cursor-not-allowed text-white text-sm font-medium rounded-md transition-colors" | ||
| > | ||
| {isExecuting ? ( | ||
| <> | ||
| <StopIcon className="w-4 h-4" /> | ||
| 실행 중... | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <PlayIcon className="w-4 h-4" /> | ||
| 실행 (Cmd+Enter) | ||
| </> | ||
| )} | ||
| </button> | ||
| </div> | ||
| </div> | ||
|
|
||
| {/* 메인 컨텐츠 영역 */} | ||
| <div className="flex-1 flex"> | ||
| <PanelGroup direction="horizontal" className="flex-1"> | ||
| {/* 왼쪽 패널 - 코드 에디터 */} | ||
| <Panel defaultSize={50} minSize={30}> | ||
| <div className="h-full bg-slate-900 border-r border-slate-800"> | ||
| <div className="h-8 bg-slate-800 border-b border-slate-700 flex items-center px-4"> | ||
| <span className="text-xs font-medium text-slate-400 uppercase tracking-wide"> | ||
| Editor | ||
| </span> | ||
| </div> | ||
| <div className="h-[calc(100%-2rem)]"> | ||
| <CodeEditor | ||
| value={code} | ||
| onChange={handleCodeChange} | ||
| onExecute={handleExecuteCode} | ||
| language="javascript" | ||
| theme="vs-dark" | ||
| /> | ||
| </div> | ||
| </div> | ||
| </Panel> | ||
|
|
||
| {/* 리사이즈 핸들 */} | ||
| <PanelResizeHandle className="w-1 bg-slate-800 hover:bg-slate-700 transition-colors" /> | ||
|
|
||
| {/* 오른쪽 패널 - 출력 결과 */} | ||
| <Panel defaultSize={50} minSize={30}> | ||
| <div className="h-full bg-slate-900"> | ||
| <div className="h-8 bg-slate-800 border-b border-slate-700 flex items-center px-4"> | ||
| <span className="text-xs font-medium text-slate-400 uppercase tracking-wide"> | ||
| Output | ||
| </span> | ||
| </div> | ||
| <div className="h-[calc(100%-2rem)]"> | ||
| <OutputPanel | ||
| result={executionResult} | ||
| isExecuting={isExecuting} | ||
| /> | ||
| </div> | ||
| </div> | ||
| </Panel> | ||
| </PanelGroup> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.