Skip to content
Closed
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
7 changes: 7 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import useHtmlDataset from '/@/composables/document/useHtmlDataset'
import { useThemeVariables } from '/@/composables/document/useThemeVariables'
import { useBeforeUnload } from '/@/composables/dom/useBeforeUnload'
import useResponsive from '/@/composables/useResponsive'
import { updateStampShadowTheme } from '/@/lib/stampShadowDirective'
import { useBrowserSettings } from '/@/store/app/browserSettings'
import { useThemeSettings } from '/@/store/app/themeSettings'
import { useTts } from '/@/store/app/tts'
Expand Down Expand Up @@ -54,6 +55,12 @@ const useThemeObserver = () => {

const stampEdge = computed(() => currentTheme.value.specific.stampEdgeEnable)
useHtmlDataset('stampEdge', stampEdge)

watch(
currentTheme,
theme => updateStampShadowTheme(theme.specific.stampEdgeEnable),
{ immediate: true, flush: 'post' }
)
}

const useEcoModeObserver = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<div :class="$style.state">
<AIcon v-if="hasAttachments" name="file" mdi :class="$style.icon" />
<bdi
v-stamp-shadow="renderedContent"
class="markdown-inline-body"
:class="$style.text"
v-html="renderedContent"
Expand All @@ -22,6 +23,7 @@ import { computed, ref, watchEffect } from 'vue'
import AIcon from '/@/components/UI/AIcon.vue'
import useChannelPath from '/@/composables/useChannelPath'
import { renderInline } from '/@/lib/markdown/markdown'
import { stampShadowDirective as vStampShadow } from '/@/lib/stampShadowDirective'
import type { MessageInputState } from '/@/store/ui/messageInputStateStore'
import type { ChannelId } from '/@/types/entity-ids'

Expand All @@ -38,7 +40,7 @@ const channelPath = computed(
const channelLink = computed(() => channelIdToLink(props.channelId) ?? '')
const hasAttachments = computed(() => props.state.attachments.length > 0)

const renderedContent = ref()
const renderedContent = ref<string>()
watchEffect(async () => {
const { renderedText } = await renderInline(props.state.text)
renderedContent.value = renderedText
Expand Down
17 changes: 7 additions & 10 deletions src/components/UI/AStamp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
<div :class="$style.container" :style="containerStyle">
<img
v-if="imageUrl.length > 0"
v-stamp-shadow="stamp?.isUnicode ? undefined : stamp?.fileId"
class="stamp-image"
:class="$style.img"
:data-unicode-stamp="stamp?.isUnicode || undefined"
:src="imageUrl"
:alt="name"
:title="!withoutTitle ? name : undefined"
Expand All @@ -16,6 +19,7 @@
import { computed } from 'vue'

import { buildFilePath } from '/@/lib/apis'
import { stampShadowDirective as vStampShadow } from '/@/lib/stampShadowDirective'
import { useStampsStore } from '/@/store/entities/stamps'
import type { StampId } from '/@/types/entity-ids'

Expand All @@ -34,11 +38,10 @@ const props = withDefaults(

const { stampsMap } = useStampsStore()

const name = computed(
() => stampsMap.value.get(props.stampId)?.name ?? 'unknown stamp'
)
const stamp = computed(() => stampsMap.value.get(props.stampId))
const name = computed(() => stamp.value?.name ?? 'unknown stamp')
const imageUrl = computed(() => {
const fileId = stampsMap.value.get(props.stampId)?.fileId
const fileId = stamp.value?.fileId
return fileId ? buildFilePath(fileId) : ''
})

Expand All @@ -58,11 +61,5 @@ const containerStyle = computed(() => ({
.img {
max-height: 100%;
margin: auto;
html[data-stamp-edge='true'] & {
filter: drop-shadow(0.1px 0.1px 0 rgb(255, 255, 255, 0.1))
drop-shadow(0.1px -0.1px 0 rgb(255, 255, 255, 0.1))
drop-shadow(-0.1px 0.1px 0 rgb(255, 255, 255, 0.1))
drop-shadow(-0.1px -0.1px 0 rgb(255, 255, 255, 0.1));
}
}
</style>
2 changes: 2 additions & 0 deletions src/components/UI/MarkdownContent.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<div
ref="contentRef"
v-stamp-shadow="content"
class="markdown-body"
:class="$style.content"
@click="onClick"
Expand All @@ -12,6 +13,7 @@
import { createVNode, onMounted, ref, render, watch } from 'vue'

import useMarkdownInternalHandler from '/@/composables/markdown/useMarkdownInternalHandler'
import { stampShadowDirective as vStampShadow } from '/@/lib/stampShadowDirective'

import FoldableCodeBlock from './FoldableCodeBlock.vue'

Expand Down
3 changes: 3 additions & 0 deletions src/components/UI/MarkdownPreview.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<template>
<bdi
v-if="props.inline"
v-stamp-shadow="renderedContent"
class="markdown-inline-body"
:class="$style.content"
:data-accept-action="acceptAction"
Expand All @@ -9,6 +10,7 @@
/>
<div
v-else
v-stamp-shadow="renderedContent"
class="markdown-body"
:class="$style.content"
:data-accept-action="acceptAction"
Expand All @@ -28,6 +30,7 @@ import {
renderInline,
renderInlineIfReady
} from '/@/lib/markdown/markdown'
import { stampShadowDirective as vStampShadow } from '/@/lib/stampShadowDirective'

const props = withDefaults(
defineProps<{
Expand Down
2 changes: 2 additions & 0 deletions src/components/UI/MessagePanel/RenderContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
:size="20"
/>
<bdi
v-stamp-shadow="renderedContent"
class="markdown-inline-body"
:class="$style.content"
v-html="renderedContent"
Expand Down Expand Up @@ -55,6 +56,7 @@ const getUniqueFileTypes = (fileTypes: Array<[AttachmentType, boolean]>) => {
<script lang="ts" setup>
import AIcon from '/@/components/UI/AIcon.vue'
import FileTypeIcon from '/@/components/UI/FileTypeIcon.vue'
import { stampShadowDirective as vStampShadow } from '/@/lib/stampShadowDirective'

const props = withDefaults(
defineProps<{
Expand Down
149 changes: 149 additions & 0 deletions src/lib/stampShadow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { buildFilePath } from '/@/lib/apis'
import type { FileId } from '/@/types/entity-ids'

const sampleSize = 32
const luminanceLevels = 64
const maxCachedOutlines = 1024

type StampOutline = {
luminanceCounts: Uint16Array
pixelCount: number
isGif: boolean
}

const outlines = new Map<FileId, Promise<StampOutline | undefined>>()
let context: CanvasRenderingContext2D | null = null

const linearize = (channel: number) => {
const srgb = channel / 255
return srgb <= 0.04045 ? srgb / 12.92 : ((srgb + 0.055) / 1.055) ** 2.4
}

export const getRelativeLuminance = (r: number, g: number, b: number) =>
0.2126 * linearize(r) + 0.7152 * linearize(g) + 0.0722 * linearize(b)

export const waitForStampAnalysisIdle = () =>
new Promise<void>(resolve => {
if (typeof requestIdleCallback === 'function') {
requestIdleCallback(() => resolve())
} else {
setTimeout(resolve, 16)
}
})

const sampleOutline = (bitmap: ImageBitmap, isGif: boolean) => {
if (!context) {
const canvas = document.createElement('canvas')
canvas.width = sampleSize
canvas.height = sampleSize
context = canvas.getContext('2d', { willReadFrequently: true })
}

if (!context) return

const scale = sampleSize / Math.max(bitmap.width, bitmap.height)
const width = bitmap.width * scale
const height = bitmap.height * scale
context.clearRect(0, 0, sampleSize, sampleSize)
context.drawImage(
bitmap,
(sampleSize - width) / 2,
(sampleSize - height) / 2,
width,
height
)

const { data } = context.getImageData(0, 0, sampleSize, sampleSize)
const luminanceCounts = new Uint16Array(luminanceLevels)
let pixelCount = 0
const alphaAt = (x: number, y: number) => {
if (x < 0 || y < 0 || x >= sampleSize || y >= sampleSize) return 0
return data[(y * sampleSize + x) * 4 + 3] ?? 0
}

for (let y = 0; y < sampleSize; y++) {
for (let x = 0; x < sampleSize; x++) {
if (alphaAt(x, y) < 192) continue
if (
alphaAt(x - 1, y) >= 128 &&
alphaAt(x + 1, y) >= 128 &&
alphaAt(x, y - 1) >= 128 &&
alphaAt(x, y + 1) >= 128
) {
continue
}

const pixel = (y * sampleSize + x) * 4
const luminance = getRelativeLuminance(
data[pixel] ?? 0,
data[pixel + 1] ?? 0,
data[pixel + 2] ?? 0
)
const level = Math.round(luminance * (luminanceLevels - 1))
luminanceCounts[level] = (luminanceCounts[level] ?? 0) + 1
pixelCount++
}
}

if (pixelCount > 0) return { luminanceCounts, pixelCount, isGif }
}

const loadOutline = async (fileId: FileId) => {
const response = await fetch(buildFilePath(fileId), { cache: 'force-cache' })
if (!response.ok) throw new Error('Failed to load stamp image')

const image = await response.blob()
const bitmap = await createImageBitmap(image)

try {
await waitForStampAnalysisIdle()
return sampleOutline(bitmap, image.type === 'image/gif')
} finally {
bitmap.close()
}
}

const getOutline = (fileId: FileId) => {
const cached = outlines.get(fileId)
if (cached) {
outlines.delete(fileId)
outlines.set(fileId, cached)
return cached
}

const outline = loadOutline(fileId).catch(() => {
if (outlines.get(fileId) === outline) outlines.delete(fileId)
return undefined
})
outlines.set(fileId, outline)

if (outlines.size > maxCachedOutlines) {
const oldest = outlines.keys().next().value
if (oldest !== undefined) outlines.delete(oldest)
}

return outline
}

export const getStampShadow = async (
fileId: FileId,
backgroundLuminance: number
) => {
const outline = await getOutline(fileId)
if (!outline) return

let lowContrastPixels = 0

for (const [index, count] of outline.luminanceCounts.entries()) {
const luminance = index / (luminanceLevels - 1)
const contrast =
(Math.max(luminance, backgroundLuminance) + 0.05) /
(Math.min(luminance, backgroundLuminance) + 0.05)
if (contrast < 2) lowContrastPixels += count
}

if (lowContrastPixels * 4 >= outline.pixelCount) return 'strong'

// 明るい最初のフレームだけで GIF の縁取りを外すと、後続のフレームが背景に溶けることがある。
if (!outline.isGif) return 'none'
}
Loading
Loading