Skip to content
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
60 changes: 30 additions & 30 deletions core/ai/src/features/code-blocks/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ import { createIcon } from '../../ui/icons'
type Badge = readonly [label: string, background: string, color: string]

const BADGES: Readonly<Record<string, Badge>> = {
javascript: ['JS', '#f7df1e', '#000'], typescript: ['TS', '#3178c6', '#fff'], python: ['PY', '#3776ab', '#ffd43b'],
bash: ['>_', '#3b8f2b', '#fff'], powershell: ['PS', '#012456', '#fff'], batch: ['BAT', '#4d4d4d', '#fff'],
json: ['{}', '#e9a620', '#000'], yaml: ['YML', '#cb171e', '#fff'], toml: ['TOML', '#9c4221', '#fff'],
html: ['</>', '#e34f26', '#fff'], xml: ['XML', '#0060ac', '#fff'], css: ['CSS', '#1572b6', '#fff'],
sql: ['SQL', '#e48e00', '#000'], graphql: ['GQL', '#e10098', '#fff'], protobuf: ['PB', '#4a7fb0', '#fff'],
go: ['GO', '#00add8', '#fff'], rust: ['RS', '#ce422b', '#fff'], zig: ['ZIG', '#f7a41d', '#000'], nim: ['NIM', '#ffe953', '#000'],
java: ['JV', '#e76f00', '#fff'], kotlin: ['KT', '#7f52ff', '#fff'], scala: ['SC', '#dc322f', '#fff'], groovy: ['GRV', '#4298b8', '#fff'],
swift: ['SW', '#f05138', '#fff'], objectivec: ['OBJ', '#438eff', '#fff'], dart: ['DRT', '#0175c2', '#fff'],
c: ['C', '#00599c', '#fff'], cpp: ['C++', '#00599c', '#fff'], csharp: ['C#', '#68217a', '#fff'],
php: ['PHP', '#777bb4', '#fff'], ruby: ['RB', '#cc342d', '#fff'], perl: ['PL', '#39457e', '#fff'], lua: ['LUA', '#000080', '#fff'],
r: ['R', '#276dc3', '#fff'], julia: ['JL', '#9558b2', '#fff'], matlab: ['M', '#e16737', '#fff'],
haskell: ['HS', '#5d4f85', '#fff'], elixir: ['EX', '#6e4a7e', '#fff'], clojure: ['CLJ', '#5881d8', '#fff'],
solidity: ['SOL', '#363636', '#fff'], assembly: ['ASM', '#6e4c13', '#fff'], vim: ['VIM', '#019733', '#fff'],
nginx: ['NGX', '#009639', '#fff'], makefile: ['MK', '#6d8086', '#fff'], dockerfile: ['DK', '#2496ed', '#fff'],
latex: ['TEX', '#008080', '#fff'], markdown: ['M↓', '#083fa1', '#fff'], diff: ['±', '#6f42c1', '#fff'],
regex: ['.*', '#6f42c1', '#fff'], http: ['HTTP', '#1f6feb', '#fff'], log: ['LOG', '#6e7681', '#fff'], csv: ['CSV', '#217346', '#fff']
javascript: ['JS', '#f7df1e', '#000'], typescript: ['TS', '#3178c6', '#fff'], python: ['PY', '#3776ab', '#ffd43b'],
bash: ['>_', '#3b8f2b', '#fff'], powershell: ['PS', '#012456', '#fff'], batch: ['BAT', '#4d4d4d', '#fff'],
json: ['{}', '#e9a620', '#000'], yaml: ['YML', '#cb171e', '#fff'], toml: ['TOML', '#9c4221', '#fff'],
html: ['</>', '#e34f26', '#fff'], xml: ['XML', '#0060ac', '#fff'], css: ['CSS', '#1572b6', '#fff'],
sql: ['SQL', '#e48e00', '#000'], graphql: ['GQL', '#e10098', '#fff'], protobuf: ['PB', '#4a7fb0', '#fff'],
go: ['GO', '#00add8', '#fff'], rust: ['RS', '#ce422b', '#fff'], zig: ['ZIG', '#f7a41d', '#000'], nim: ['NIM', '#ffe953', '#000'],
java: ['JV', '#e76f00', '#fff'], kotlin: ['KT', '#7f52ff', '#fff'], scala: ['SC', '#dc322f', '#fff'], groovy: ['GRV', '#4298b8', '#fff'],
swift: ['SW', '#f05138', '#fff'], objectivec: ['OBJ', '#438eff', '#fff'], dart: ['DRT', '#0175c2', '#fff'],
c: ['C', '#00599c', '#fff'], cpp: ['C++', '#00599c', '#fff'], csharp: ['C#', '#68217a', '#fff'],
php: ['PHP', '#777bb4', '#fff'], ruby: ['RB', '#cc342d', '#fff'], perl: ['PL', '#39457e', '#fff'], lua: ['LUA', '#000080', '#fff'],
r: ['R', '#276dc3', '#fff'], julia: ['JL', '#9558b2', '#fff'], matlab: ['M', '#e16737', '#fff'],
haskell: ['HS', '#5d4f85', '#fff'], elixir: ['EX', '#6e4a7e', '#fff'], clojure: ['CLJ', '#5881d8', '#fff'],
solidity: ['SOL', '#363636', '#fff'], assembly: ['ASM', '#6e4c13', '#fff'], vim: ['VIM', '#019733', '#fff'],
nginx: ['NGX', '#009639', '#fff'], makefile: ['MK', '#6d8086', '#fff'], dockerfile: ['DK', '#2496ed', '#fff'],
latex: ['TEX', '#008080', '#fff'], markdown: ['M↓', '#083fa1', '#fff'], diff: ['±', '#6f42c1', '#fff'],
regex: ['.*', '#6f42c1', '#fff'], http: ['HTTP', '#1f6feb', '#fff'], log: ['LOG', '#6e7681', '#fff'], csv: ['CSV', '#217346', '#fff']
}

export const createLanguageIcon = (language: string): HTMLElement => {
const badge = BADGES[language]
if (!badge) {
const plain = createElement('span', 'bp-lang-icon bp-lang-plain')
plain.title = 'Plain text'
plain.appendChild(createIcon('file', 16))
return plain
}
const [label, background, color] = badge
const element = createElement('span', 'bp-lang-icon bp-lang-badge', label)
element.style.setProperty('--c', background)
element.style.color = color
element.dataset['size'] = String(label.length)
return element
}
const badge = BADGES[language]
if (!badge) {
const plain = createElement('span', 'bp-lang-icon bp-lang-plain')
plain.title = 'Plain text'
plain.appendChild(createIcon('file', 16))
return plain
}
const [label, background, color] = badge
const element = createElement('span', 'bp-lang-icon bp-lang-badge', label)
element.style.setProperty('--c', background)
element.style.setProperty('--badge-fg', color)
element.dataset['size'] = String(label.length)
return element
}
6 changes: 5 additions & 1 deletion core/ai/src/features/code-blocks/styles.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { injectStyle } from '@nolly/core'
import { uiConfig } from '../../ui/config'
import { ensureCodeTheme } from './theme'

const CSS = `
.bp-code-v2 {
Expand Down Expand Up @@ -133,4 +134,7 @@ const CSS = `
}
`

export const ensureCodeStyles = (): void => injectStyle(`${uiConfig().id}-code-v2-styles`, CSS)
export const ensureCodeStyles = (): void => {
injectStyle(`${uiConfig().id}-code-v2-styles`, CSS)
ensureCodeTheme()
}
84 changes: 84 additions & 0 deletions core/ai/src/features/code-blocks/theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { injectStyle } from '@nolly/core'
import { uiConfig } from '../../ui/config'

let installed = false

const CSS = `
html[data-bp-code-theme='light'] .bp-code-v2 {
--bp-bg: #ffffff;
--bp-fg: #1f2328;
--bp-muted: #59636e;
--bp-border: #d1d9e0;
--bp-accent: #f6f8fa;
--bp-ring: #0969da;
--bp-code-keyword: #cf222e;
--bp-code-string: #0a3069;
--bp-code-number: #0550ae;
--bp-code-function: #8250df;
--bp-code-type: #953800;
--bp-code-tag: #116329;
--bp-code-attribute: #0550ae;
color-scheme: light;
}
html[data-bp-code-theme='dark'] .bp-code-v2 {
--bp-bg: #0d1117;
--bp-fg: #e6edf3;
--bp-muted: #9198a1;
--bp-border: #30363d;
--bp-accent: #161b22;
--bp-ring: #58a6ff;
--bp-code-keyword: #ff7b72;
--bp-code-string: #a5d6ff;
--bp-code-number: #79c0ff;
--bp-code-function: #d2a8ff;
--bp-code-type: #ffa657;
--bp-code-tag: #7ee787;
--bp-code-attribute: #79c0ff;
color-scheme: dark;
}
.bp-code-v2 .bp-tok-comment { color: var(--bp-muted); }
.bp-code-v2 .bp-tok-keyword { color: var(--bp-code-keyword); }
.bp-code-v2 .bp-tok-string { color: var(--bp-code-string); }
.bp-code-v2 .bp-tok-number { color: var(--bp-code-number); }
.bp-code-v2 .bp-tok-fn { color: var(--bp-code-function); }
.bp-code-v2 .bp-tok-type { color: var(--bp-code-type); }
.bp-code-v2 .bp-tok-tag { color: var(--bp-code-tag); }
.bp-code-v2 .bp-tok-attr,
.bp-code-v2 .bp-tok-key { color: var(--bp-code-attribute); }
.bp-code-v2 .bp-lang-badge {
background: var(--c);
color: var(--badge-fg);
border-color: transparent;
font-weight: 700;
}
.bp-code-v2 .bp-code-head { box-shadow: none; }
.bp-code-v2 .bp-code-btn { transition: background 120ms ease, color 120ms ease; }
`

const explicitTheme = (element: HTMLElement): 'dark' | 'light' | null => {
if (element.classList.contains('dark')) return 'dark'
if (element.classList.contains('light')) return 'light'
for (const attribute of ['data-theme', 'data-color-mode', 'data-color-scheme']) {
const value = element.getAttribute(attribute)?.toLowerCase()
if (value === 'dark' || value === 'light') return value
}
const scheme = getComputedStyle(element).colorScheme.trim()
return scheme === 'dark' || scheme === 'light' ? scheme : null
}

export const ensureCodeTheme = (): void => {
injectStyle(`${uiConfig().id}-code-v2-theme`, CSS)
if (installed) return
installed = true
const media = window.matchMedia('(prefers-color-scheme: dark)')
const update = (): void => {
const theme = explicitTheme(document.documentElement) ?? (document.body ? explicitTheme(document.body) : null) ?? (media.matches ? 'dark' : 'light')
if (document.documentElement.dataset['bpCodeTheme'] !== theme) document.documentElement.dataset['bpCodeTheme'] = theme
}
const observer = new MutationObserver(update)
const options: MutationObserverInit = { attributes: true, attributeFilter: ['class', 'data-theme', 'data-color-mode', 'data-color-scheme', 'style'] }
observer.observe(document.documentElement, options)
if (document.body) observer.observe(document.body, options)
media.addEventListener('change', update)
update()
}
30 changes: 7 additions & 23 deletions core/ai/src/features/enter-as-newline/config-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createElement, injectStyle } from '@nolly/core'
import { markUi, uiConfig } from '../../ui/config'
import { ensureUiStyles } from '../../ui/styles'
import { Tooltip } from '../../ui/tooltip'
import { mountComposerTool, unmountComposerTool } from '../../ui/composer-toolbar'

const SVG_NS = 'http://www.w3.org/2000/svg'

Expand Down Expand Up @@ -31,7 +32,6 @@ const createIcon = (): SVGSVGElement => {
export class ConfigButton {
readonly element = markUi(createElement('button', 'bp-config-button'))
private readonly tooltip: Tooltip
private appliedClass = ''

constructor(onClick: () => void, describe: () => string) {
ensureUiStyles()
Expand All @@ -50,27 +50,11 @@ export class ConfigButton {
}

mount(): void {
const { first, submit } = uiConfig().composerActions?.() ?? { first: null, submit: null }
if (!first && !submit) {
this.unmount()
return
}
if (first) this.syncClass(first)
const placed = first ? first.nextElementSibling === this.element : submit?.previousElementSibling === this.element
if (placed) return
if (first) first.insertAdjacentElement('afterend', this.element)
else submit?.insertAdjacentElement('beforebegin', this.element)
}

unmount(): void {
this.tooltip.hide()
this.element.remove()
}
if (!mountComposerTool(this.element, 'config')) this.tooltip.hide()
}

private syncClass(reference: HTMLElement): void {
const next = `${reference.className} bp-config-button`
if (next === this.appliedClass) return
this.element.className = next
this.appliedClass = next
}
unmount(): void {
this.tooltip.hide()
unmountComposerTool(this.element, 'config')
}
}
Loading
Loading