Skip to content

Commit

Permalink
feat: can render extension icon
Browse files Browse the repository at this point in the history
  • Loading branch information
0xzio committed May 15, 2024
1 parent d2c5a25 commit 3430a27
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 9 deletions.
24 changes: 24 additions & 0 deletions apps/cli/src/commands/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import yargs, { ArgumentsCamelCase } from 'yargs'
import fetch from 'node-fetch'
import * as tsup from 'tsup'
import { join } from 'path'
import fs from 'fs'

type Args = {}

Expand All @@ -11,6 +12,7 @@ type CommandItem = {
title: string
subtitle: string
description: string
icon?: string
code?: string
}

Expand All @@ -21,6 +23,7 @@ type Manifest = {
description: string
main: string
code: string
icon: string
commands: CommandItem[]
}

Expand Down Expand Up @@ -65,6 +68,21 @@ class Command {
})
}

private getLogo = async (iconName = '') => {
if (!iconName) return ''
try {
const cwd = process.cwd()
const iconPath = join(cwd, 'assets', iconName)
if (iconName.endsWith('.svg')) {
return jetpack.read(iconPath, 'utf8')
}

return fs.readFileSync(iconPath).toString('base64')
} catch (error) {
return ''
}
}

private handleBuildSuccess = async () => {
const manifest = getManifest()

Expand All @@ -74,8 +92,13 @@ class Command {
const codePath = join(process.cwd(), 'dist', `${command.name}.js`)
const code = jetpack.read(codePath, 'utf8')
command.code = code
command.icon = await this.getLogo(command.icon)
}

const logoString = await this.getLogo(manifest.icon)

// console.log('========logoString:', logoString)

try {
const res = await fetch(url, {
method: 'POST',
Expand All @@ -86,6 +109,7 @@ class Command {
id: manifest.id,
name: manifest.name,
version: manifest.version || '',
icon: logoString,
commands: JSON.stringify(manifest.commands),
}),
}).then((res) => res.json())
Expand Down
Binary file added apps/desktop/public/logo/128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions apps/desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ struct ExtensionInfo {
id: String,
name: String,
version: String,
icon: String,
commands: String,
}

Expand All @@ -46,6 +47,7 @@ struct UpsertExtensionInput {
id: String,
name: String,
version: String,
icon: String,
commands: String,
}

Expand Down Expand Up @@ -103,6 +105,7 @@ async fn upsert_extension(
id: input.id.to_string(),
name: input.name.to_string(),
version: input.version.to_string(),
icon: input.icon.to_string(),
commands: input.commands.to_string(),
};

Expand Down
37 changes: 34 additions & 3 deletions apps/desktop/src/components/CmdkRoot.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useRef, useState } from 'react'
import SVG from 'react-inlinesvg'
import Markdown from 'react-markdown'
import { Box, styled } from '@fower/react'
import { Box, css, styled } from '@fower/react'
import { open } from '@tauri-apps/api/shell'
import { Command } from 'cmdk'
import Image from 'next/image'
import { EventType, ListItem } from 'penx'
import clipboard from 'tauri-plugin-clipboard-api'
// import { Command } from '@penx/cmdk'
Expand All @@ -27,6 +28,37 @@ type CommandItem = {
code: string
}

interface ItemIconProps {
icon: string
}
function ItemIcon({ icon }: ItemIconProps) {
if (!icon) {
return <Box square5 bgNeutral300 rounded-6></Box>
}

if (icon.startsWith('/')) {
return (
<Image
src={icon}
alt=""
width={20}
height={20}
style={{ borderRadius: 6 }}
/>
)
}

const isSVG = icon.startsWith('<svg')
if (isSVG) {
return (
<SVG className={css({ square: 20, rounded: 6 })} src={icon as string} />
)
}
return (
<Box as="img" square5 rounded-6 src={`data:image/png;base64, ${icon}`} />
)
}

export const CmdkRoot = () => {
const [q, setQ] = useState('')
const { items, setItems } = useItems()
Expand Down Expand Up @@ -205,8 +237,7 @@ export const CmdkRoot = () => {
}}
>
<Box toCenterY gap2>
<SVG src="" />
<Box square5 bgNeutral300 rounded-6></Box>
<ItemIcon icon={item.icon as string}></ItemIcon>
<Box text-15>{title}</Box>
<Box textSM gray500>
{subtitle}
Expand Down
2 changes: 2 additions & 0 deletions apps/desktop/src/hooks/useInstallBuiltinExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function useInstallBuiltinExtension() {
{
name: 'clipboard-history',
title: 'Clipboard history',
icon: '/logo/128x128.png',
subtitle: '',
description: '',
code: '',
Expand All @@ -27,6 +28,7 @@ export function useInstallBuiltinExtension() {
{
name: 'store',
title: 'Store',
icon: '/logo/128x128.png',
subtitle: '',
description: '',
code: '',
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/hooks/useItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function useQueryCommands() {
...cur.commands.map<ListItem>((item) => ({
type: 'command',
title: item.title,
icon: item.icon,
icon: item.icon ? item.icon : cur.icon,
data: {
commandName: item.name,
extensionSlug: cur.slug,
Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ async function init() {
commands: string
id: string
name: string
icon: string
version: string
}

Expand All @@ -82,9 +83,11 @@ async function init() {
listen(AppEvent.UPSERT_EXTENSION, async (data) => {
const payload = data.payload as Payload
const commands = JSON.parse(payload.commands || '[]')

await db.upsertExtension(payload.id, {
commands: commands,
name: payload.name,
icon: payload.icon,
version: payload.version,
})
})
Expand Down
1 change: 1 addition & 0 deletions extension-samples/base64/assets/base64.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extension-samples/base64/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension-samples/base64/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "Base64",
"id": "base64",
"description": "",
"icon": "logo.png",
"commands": [
{
"name": "base64",
Expand Down
4 changes: 3 additions & 1 deletion extension-samples/base64/src/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { toString } from './libs/toString'
renderList([
{
title: 'Encode',
subtitle: toBase64('Hello World!'),
icon: 'base64.svg',
subtitle: toBase64('Hello World!!!!'),
actions: [
{
type: 'CopyToClipboard',
Expand All @@ -15,6 +16,7 @@ renderList([
},
{
title: 'Decode',
icon: 'base64.svg',
subtitle: toString('Hello World!'),
actions: [
{
Expand Down
1 change: 1 addition & 0 deletions extension-samples/hacker-news/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension-samples/hacker-news/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"id": "hack-news",
"version": "1.0.0",
"description": "",
"icon": "logo.svg",
"commands": [
{
"name": "index",
Expand Down
1 change: 1 addition & 0 deletions extension-samples/hello-word/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions extension-samples/hello-word/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "Hello world",
"id": "hello-world",
"description": "This is a hello wold demo.",
"icon": "logo.svg",
"commands": [
{
"name": "index",
Expand Down
2 changes: 1 addition & 1 deletion extension-samples/hello-word/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { renderMarkdown } from 'penx'

renderMarkdown('# hello world!')
renderMarkdown('# hello world!!!')
4 changes: 2 additions & 2 deletions packages/app/src/Workbench/PageExtensions/PageExtensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function ExtensionItem({ extension }: ExtensionItemProps) {
colorScheme="white"
disabled={isLoading}
gap1
onClick={() => {
mutateAsync()
onClick={async () => {
await mutateAsync()
refetch()
}}
>
Expand Down
2 changes: 1 addition & 1 deletion packages/local-db/src/penx-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class PenxDB extends Dexie {
constructor() {
// super('PenxDB')
super('penx-local')
this.version(13).stores({
this.version(14).stores({
// Primary key and indexed props
space: 'id, name, userId',
node: 'id, spaceId, databaseId, type, date, [type+spaceId+databaseId], [type+spaceId], [type+databaseId]',
Expand Down
2 changes: 2 additions & 0 deletions packages/model-types/src/interfaces/IExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface IExtension {

commands: Command[]

icon?: string

description?: string

author?: string
Expand Down

0 comments on commit 3430a27

Please sign in to comment.