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

feat: add dark mode toggle and enhance interaction in CodemirrorEditor #511

Merged
merged 2 commits into from
Jan 10, 2025
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
26 changes: 24 additions & 2 deletions src/components/CodemirrorEditor/EditorHeader/PostInfo.vue
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script setup lang="ts">
import { useStore } from '@/stores'
import { Info } from 'lucide-vue-next'
import { Primitive } from 'radix-vue'

const store = useStore()
const { output } = storeToRefs(store)

const dialogVisible = ref(false)

const extensionInstalled = ref(false)
const form = ref<any>({
title: ``,
desc: ``,
Expand Down Expand Up @@ -40,12 +41,12 @@ function prePost() {
declare global {
interface Window {
syncPost: (data: { thumb: string, title: string, desc: string, content: string }) => void
$syncer: any
}
}

function post() {
dialogVisible.value = false;
// 使用 window.$syncer 可以检测是否安装插件
(window.syncPost)({
thumb: form.value.thumb || form.value.auto.thumb,
title: form.value.title || form.value.auto.title,
Expand All @@ -59,6 +60,10 @@ function onUpdate(val: boolean) {
dialogVisible.value = false
}
}

onMounted(() => {
extensionInstalled.value = window.$syncer !== `undefined`
})
</script>

<template>
Expand All @@ -80,6 +85,23 @@ function onUpdate(val: boolean) {
</AlertDescription>
</Alert>

<Alert v-if="!extensionInstalled">
<Info class="h-4 w-4" />
<AlertTitle>未检测到插件</AlertTitle>
<AlertDescription>
请安装
<Primitive
as="a"
class="text-blue-500"
href="https://www.wechatsync.com/?utm_source=syncicon#install"
target="_blank"
>
文章同步助手
</Primitive>
插件
</AlertDescription>
</Alert>

<div class="w-full flex items-center gap-4">
<Label for="thumb" class="w-10 text-end">
封面
Expand Down
7 changes: 6 additions & 1 deletion src/components/CodemirrorEditor/EditorHeader/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@/config'
import { useStore } from '@/stores'
import { addPrefix, processClipboardContent } from '@/utils'
import { ChevronDownIcon, PanelLeftClose, PanelLeftOpen, Settings } from 'lucide-vue-next'
import { ChevronDownIcon, Moon, PanelLeftClose, PanelLeftOpen, Settings, Sun } from 'lucide-vue-next'

const emit = defineEmits([`addFormat`, `formatContent`, `startCopy`, `endCopy`])

Expand Down Expand Up @@ -155,6 +155,11 @@ function copy() {
</Tooltip>
</TooltipProvider>

<Button variant="outline" @click="toggleDark()">
<Moon v-show="isDark" class="size-4" />
<Sun v-show="!isDark" class="size-4" />
</Button>

<div class="space-x-1 bg-background text-background-foreground mx-2 flex items-center border rounded-md">
<Button variant="ghost" class="shadow-none" @click="copy">
复制
Expand Down
61 changes: 61 additions & 0 deletions src/components/ui/back-top/BackTop.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<script setup lang="ts">
import { throttle } from 'es-toolkit'
import { ArrowUpFromLine } from 'lucide-vue-next'

type Target = HTMLElement | Window | null

const props = defineProps<{
left?: number
top?: number
right?: number
bottom?: number
visibilityHeight?: number
target?: string
onClick?: (e: MouseEvent) => void
}>()

const visibilityHeight = ref(props.visibilityHeight ?? 400)
const visible = ref(false)

const target = ref<Target>(null)

function scrollToTop(e: MouseEvent) {
console.log(`scrollToTop`)
target.value?.scrollTo({ top: 0, left: 0, behavior: `smooth` })
props.onClick?.(e)
}

const throttledScroll = throttle((el: Target) => {
if (el instanceof HTMLElement) {
visible.value = el.scrollTop > visibilityHeight.value
}
else {
visible.value = window.scrollY > visibilityHeight.value
}
}, 200, { edges: [`leading`, `trailing`] })

onMounted(() => {
if (props.target) {
target.value = document.getElementById(props.target)
}
else {
target.value = window
}

target.value!.addEventListener(`scroll`, () => {
throttledScroll(target.value)
})
})

onUnmounted(() => {
target.value!.removeEventListener(`scroll`, () => {
throttledScroll(target.value)
})
})
</script>

<template>
<Button v-if="visible" variant="outline" size="icon" class="fixed z-50 rounded-full" :style="{ left: `${left}px`, top: `${top}px`, right: `${right}px`, bottom: `${bottom}px` }" @click="scrollToTop">
<ArrowUpFromLine />
</Button>
</template>
1 change: 1 addition & 0 deletions src/components/ui/back-top/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as BackTop } from './BackTop.vue'
2 changes: 2 additions & 0 deletions src/views/CodemirrorEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ onMounted(() => {
</div>
</div>
</div>

<BackTop target="preview" :right="40" :bottom="40" />
</div>
<CssEditor class="order-2 flex-1" />
<RightSlider class="order-2" />
Expand Down
Loading