mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
feat: feedback (#156)
* minimal tiptap * wip * img edit block * wip * fix
This commit is contained in:
81
web/components/minimal-tiptap/utils.ts
Normal file
81
web/components/minimal-tiptap/utils.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import type { Editor } from '@tiptap/core'
|
||||
import type { MinimalTiptapProps } from './minimal-tiptap'
|
||||
|
||||
let isMac: boolean | undefined
|
||||
|
||||
interface Navigator {
|
||||
userAgentData?: {
|
||||
brands: { brand: string; version: string }[]
|
||||
mobile: boolean
|
||||
platform: string
|
||||
getHighEntropyValues: (hints: string[]) => Promise<{
|
||||
platform: string
|
||||
platformVersion: string
|
||||
uaFullVersion: string
|
||||
}>
|
||||
}
|
||||
}
|
||||
|
||||
function getPlatform(): string {
|
||||
const nav = navigator as Navigator
|
||||
|
||||
if (nav.userAgentData) {
|
||||
if (nav.userAgentData.platform) {
|
||||
return nav.userAgentData.platform
|
||||
}
|
||||
|
||||
nav.userAgentData.getHighEntropyValues(['platform']).then(highEntropyValues => {
|
||||
if (highEntropyValues.platform) {
|
||||
return highEntropyValues.platform
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (typeof navigator.platform === 'string') {
|
||||
return navigator.platform
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export function isMacOS() {
|
||||
if (isMac === undefined) {
|
||||
isMac = getPlatform().toLowerCase().includes('mac')
|
||||
}
|
||||
|
||||
return isMac
|
||||
}
|
||||
|
||||
interface ShortcutKeyResult {
|
||||
symbol: string
|
||||
readable: string
|
||||
}
|
||||
|
||||
export function getShortcutKey(key: string): ShortcutKeyResult {
|
||||
const lowercaseKey = key.toLowerCase()
|
||||
if (lowercaseKey === 'mod') {
|
||||
return isMacOS() ? { symbol: '⌘', readable: 'Command' } : { symbol: 'Ctrl', readable: 'Control' }
|
||||
} else if (lowercaseKey === 'alt') {
|
||||
return isMacOS() ? { symbol: '⌥', readable: 'Option' } : { symbol: 'Alt', readable: 'Alt' }
|
||||
} else if (lowercaseKey === 'shift') {
|
||||
return isMacOS() ? { symbol: '⇧', readable: 'Shift' } : { symbol: 'Shift', readable: 'Shift' }
|
||||
} else {
|
||||
return { symbol: key, readable: key }
|
||||
}
|
||||
}
|
||||
|
||||
export function getShortcutKeys(keys: string[]): ShortcutKeyResult[] {
|
||||
return keys.map(key => getShortcutKey(key))
|
||||
}
|
||||
|
||||
export function getOutput(editor: Editor, format: MinimalTiptapProps['output']) {
|
||||
if (format === 'json') {
|
||||
return editor.getJSON()
|
||||
}
|
||||
|
||||
if (format === 'html') {
|
||||
return editor.getText() ? editor.getHTML() : ''
|
||||
}
|
||||
|
||||
return editor.getText()
|
||||
}
|
||||
Reference in New Issue
Block a user