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

refactor(playground-nextjs): remove dynamic load #71

Merged
merged 1 commit into from
Aug 2, 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 packages/extensions/ai-widget/src/placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AIPlaceholderWidget extends WidgetType {
}

toDOM(view: EditorView): HTMLElement {
const cmd = isAppleOs ? 'Command' : 'Ctrl'
const cmd = isAppleOs() ? 'Command' : 'Ctrl'

const root = document.createElement('span')
root.className = 'cm-ai-placeholder'
Expand Down
2 changes: 1 addition & 1 deletion packages/extensions/ai-widget/src/tooltip-hint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const cursorTooltipBaseTheme = EditorView.baseTheme({
//------------------------------------------

function getCursorTooltips(state: EditorState): readonly Tooltip[] {
const cmd = isAppleOs ? 'Command' : 'Ctrl'
const cmd = isAppleOs() ? 'Command' : 'Ctrl'

return state.selection.ranges
.filter((range) => !range.empty)
Expand Down
42 changes: 25 additions & 17 deletions packages/extensions/ai-widget/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
function getOS() {
let userAgent = navigator.userAgent
if (/windows phone/i.test(userAgent)) {
return 'Windows Phone'
} else if (/android/i.test(userAgent)) {
return 'Android'
} else if (/iPad|iPhone|iPod/.test(userAgent)) {
return 'iOS'
} else if (/Macintosh|MacIntel|MacPPC|Mac68K/i.test(userAgent)) {
return 'macOS'
} else if (/Windows|Win16|Win32|WinCE|Win64/i.test(userAgent)) {
return 'Windows'
} else if (/Linux/i.test(userAgent)) {
return 'Linux'
} else {
return 'Unknown OS'
// ref: https://dev.to/vvo/how-to-solve-window-is-not-defined-errors-in-react-and-next-js-5f97
if (typeof window !== 'undefined') {
let userAgent = navigator.userAgent
if (/windows phone/i.test(userAgent)) {
return 'Windows Phone'
} else if (/android/i.test(userAgent)) {
return 'Android'
} else if (/iPad|iPhone|iPod/.test(userAgent)) {
return 'iOS'
} else if (/Macintosh|MacIntel|MacPPC|Mac68K/i.test(userAgent)) {
return 'macOS'
} else if (/Windows|Win16|Win32|WinCE|Win64/i.test(userAgent)) {
return 'Windows'
} else if (/Linux/i.test(userAgent)) {
return 'Linux'
} else {
return 'Unknown OS'
}
}
return 'Unknown OS'
}

export const os = getOS()
export function os() {
return getOS()
}

export const isAppleOs = ['macOS', 'iOS'].includes(os)
export function isAppleOs() {
return ['macOS', 'iOS'].includes(os())
}
7 changes: 2 additions & 5 deletions packages/playground-nextjs/app/example/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DynamicEditorExample } from '@/components/biz/dynamic-editor-example'
import { EditorExample } from '@/components/biz/editor-example'

export default function Page({
searchParams
Expand All @@ -10,10 +10,7 @@ export default function Page({
}) {
return (
<main className="h-screen">
<DynamicEditorExample
example={searchParams?.ex}
theme={searchParams?.theme}
/>
<EditorExample example={searchParams?.ex} theme={searchParams?.theme} />
</main>
)
}
8 changes: 2 additions & 6 deletions packages/playground-nextjs/app/examples/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GithubIcon, FullscreenIcon } from 'lucide-react'
import Link from 'next/link'

import { DynamicEditorExample } from '@/components/biz/dynamic-editor-example'
import { EditorExample } from '@/components/biz/editor-example'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'

Expand Down Expand Up @@ -59,11 +59,7 @@ export default function Page({
showOutputBox ? 'h-[600px]' : 'h-[400px]'
)}
>
<DynamicEditorExample
example={ex}
theme={editorTheme}
withSelect={true}
/>
<EditorExample example={ex} theme={editorTheme} withSelect={true} />
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,7 @@ function convertSchemaToSQLConfig(dbList: SchemaRes): SQLConfig {
return { schema, tables }
}

// wholly shit!
// when I use `export function Editor`, and dynamic load this file, next.js still reports prerender error, `ReferenceError: navigator is not defined` after running `pnpm build`.
// full code:
// const MyEditor = dynamic(() => import("./editor").then((mod) => mod.Editor), {
// ssr: false,
// });
// build logs: https://vercel.com/baurines-projects/tisqleditor/5Y4HjQY6xeDYVCBCaBrpLxawdYPD?filter=errors
//
// then I export Editor as default, aka `export default function Editor`, then, the error is dismissed, can't understand it totally.
// full code:
// const MyEditor = dynamic(() => import("./editor")), {
// ssr: false,
// });
export default function Editor() {
export function Editor() {
const {
api: { saveFile },
state: { activeFileId, openedFiles }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import dynamic from 'next/dynamic'

import { EditorActions } from './actions'
import { OpenedFilesTabs } from './opened-files'

const Editor = dynamic(() => import('./editor'), {
ssr: false
})
import { Editor } from './editor'

export function EditorPanel() {
return (
Expand Down

This file was deleted.