Skip to content

Commit 020eba7

Browse files
committed
feat(#324): AI 写作功能,将问答与润色功能转移至选中文本后的悬浮工具栏,并增加了精简和扩写功能
1 parent 5c00085 commit 020eba7

File tree

14 files changed

+247
-227
lines changed

14 files changed

+247
-227
lines changed

messages/en.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,27 +778,32 @@
778778
"question": {
779779
"tooltip": "Q&A",
780780
"selectContent": "Please select content first",
781-
"promptTemplate": "Reference text: {content}\nBased on the question: {question}, directly provide the answer content."
781+
"promptTemplate": "Reference text: \n{content}\nBased on the question: \n{question}\n, directly provide the answer content."
782782
},
783783
"continue": {
784784
"tooltip": "Continue",
785-
"promptTemplate": "Based on the preceding text: \"{content}\" continue writing and return content not exceeding 100 words.\nYou can reference the following text: \"{endContent}\", but avoid duplicating its content."
785+
"promptTemplate": "Based on the preceding text: \n{content}\n continue writing and return content not exceeding 100 words.\nYou can reference the following text: \n{endContent}\n, but avoid duplicating its content."
786786
},
787787
"polish": {
788788
"tooltip": "Polish",
789789
"selectContent": "Please select content first",
790-
"promptTemplate": "Polish this text: {content}, keep the language unchanged, fix typos and grammatical errors, directly return the polished result."
790+
"promptTemplate": "Polish this text: \n{content}\n, keep the language unchanged, fix typos and grammatical errors, directly return the polished result."
791791
},
792792
"eraser": {
793793
"tooltip": "Simplify",
794794
"selectContent": "Please select content first",
795-
"promptTemplate": "Simplify this text: {content}, this text is too verbose, reduce the word count by at least half, keep the language unchanged, directly return the optimized result."
795+
"promptTemplate": "Simplify this text: \n{content}\n, this text is too verbose, reduce the word count by at least half, keep the language unchanged, directly return the optimized result."
796+
},
797+
"expansion": {
798+
"tooltip": "Expand",
799+
"selectContent": "Please select content first",
800+
"promptTemplate": "Expand this text: \n{content}\n, this text is too short, increase the word count by at least half, keep the language unchanged, directly return the expanded result."
796801
},
797802
"translation": {
798803
"tooltip": "Translate",
799804
"description": "Translate the selected text",
800805
"selectContent": "Please select content first",
801-
"promptTemplate": "Translate this text: {content}, into {language}, directly return the translated result."
806+
"promptTemplate": "Translate this text: \n{content}\n, into {language}, directly return the translated result."
802807
}
803808
}
804809
},

messages/ja.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,27 +767,32 @@
767767
"question": {
768768
"tooltip": "質問応答",
769769
"selectContent": "先に内容を選択してください",
770-
"promptTemplate": "参考文:{content}\n質問内容:{question}に基づいて、直接回答を返してください。"
770+
"promptTemplate": "参考文:\n{content}\n質問内容:\n{question}\nに基づいて、直接回答を返してください。"
771771
},
772772
"continue": {
773773
"tooltip": "文章続き",
774-
"promptTemplate": "前文:\"{content}\"に基づいて、100文字以内で続きを書いてください。\n後文:\"{endContent}\"を参考にしても良いですが、重複は避けてください。"
774+
"promptTemplate": "前文:\n{content}\n内容に基づいて、100文字以内で続きを書いてください。\n後文:\n{endContent}\nを参考にしても良いですが、重複は避けてください。"
775775
},
776776
"polish": {
777777
"tooltip": "最適化",
778778
"selectContent": "先に内容を選択してください",
779-
"promptTemplate": "この文章を校正してください:{content}。言語はそのままに、誤字脱字や不自然な表現を修正し、校正後の結果を直接返してください。"
779+
"promptTemplate": "この文章を校正してください:\n{content}\n。言語はそのままに、誤字脱字や不自然な表現を修正し、校正後の結果を直接返してください。"
780780
},
781781
"eraser": {
782782
"tooltip": "簡潔化",
783783
"selectContent": "先に内容を選択してください",
784-
"promptTemplate": "この文章を要約してください:{content}。文章があまりに冗長なので、文字数を半分以上に減らしてください。言語は変更しないで、要約した結果を直接返してください。"
784+
"promptTemplate": "この文章を要約してください:\n{content}\n。文章があまりに冗長なので、文字数を半分以上に減らしてください。言語は変更しないで、要約した結果を直接返してください。"
785+
},
786+
"expansion": {
787+
"tooltip": "拡張",
788+
"selectContent": "先に内容を選択してください",
789+
"promptTemplate": "この文章を拡張してください:\n{content}\n。文章があまりに短いので、文字数を半分以上に増やしてください。言語は変更しないで、拡張した結果を直接返してください。"
785790
},
786791
"translation": {
787792
"tooltip": "翻訳",
788793
"description": "選択したテキストを翻訳します",
789794
"selectContent": "まず内容を選択してください",
790-
"promptTemplate": "このテキスト:{content}{language}に翻訳してください。翻訳結果のみを返してください。"
795+
"promptTemplate": "このテキスト:\n{content}\n{language}に翻訳してください。翻訳結果のみを返してください。"
791796
}
792797
}
793798
},

messages/zh.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -777,27 +777,32 @@
777777
"question": {
778778
"tooltip": "问答",
779779
"selectContent": "请先选择一段内容",
780-
"promptTemplate": "参考原文:{content}\n根据提问:{question},直接返回回答内容。"
780+
"promptTemplate": "参考原文:\n{content}\n根据提问:\n{question}\n,直接返回回答内容。"
781781
},
782782
"continue": {
783783
"tooltip": "续写",
784-
"promptTemplate": "根据前文:\"{content}\" 内容,直接返回续写内容,不要超过100字。\n内容可以参考后文:\"{endContent}\",不要与后文内容重复。"
784+
"promptTemplate": "根据前文:\n{content}\n内容,直接返回续写内容,不要超过100字。\n内容可以参考后文:\n{endContent}\n,不要与后文内容重复。"
785785
},
786786
"polish": {
787-
"tooltip": "优化",
787+
"tooltip": "润色",
788788
"selectContent": "请先选择一段内容",
789-
"promptTemplate": "润色这段文字:{content},要求语言不变,修复错别字和病句,直接返回润色后的结果。"
789+
"promptTemplate": "润色这段文字:\n{content}\n要求语言不变,修复错别字和病句,直接返回润色后的结果。"
790790
},
791791
"eraser": {
792792
"tooltip": "精简",
793793
"selectContent": "请先选择一段内容",
794-
"promptTemplate": "精简这段文字:{content},这段文字过于臃肿,字数要求缩减一半以上,要求语言不变,直接返回优化后的结果。"
794+
"promptTemplate": "精简这段文字:\n{content}\n这段文字过于臃肿,字数要求缩减一半以上,要求语言不变,直接返回优化后的结果。"
795+
},
796+
"expansion": {
797+
"tooltip": "扩写",
798+
"selectContent": "请先选择一段内容",
799+
"promptTemplate": "扩写这段文字:\n{content}\n这段文字过于简短,字数要求增加一倍以上,要求语言不变,直接返回扩写后的结果。"
795800
},
796801
"translation": {
797802
"tooltip": "翻译",
798803
"description": "将选中的文本进行翻译",
799804
"selectContent": "请先选择一段内容",
800-
"promptTemplate": "将这段文字:{content},翻译为{language}语言,直接返回翻译后的结果。"
805+
"promptTemplate": "将这段文字:\n{content}\n翻译为{language}语言,直接返回翻译后的结果。"
801806
}
802807
}
803808
},

src/app/core/article/custom-toolbar/eraser.tsx

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import Vditor from 'vditor'
22
import Mark from "./mark";
3-
import Question from "./question";
43
import Continue from "./continue";
5-
import Polish from "./polish";
6-
import Eraser from "./eraser";
74
import Translation from "./translation";
85

96
export default function CustomToolbar({editor}: {editor?: Vditor}) {
107
return <div className="h-12 w-full border-b items-center px-2 gap-1 justify-between hidden">
118
<Mark editor={editor} />
12-
<Question editor={editor} />
139
<Continue editor={editor} />
14-
<Polish editor={editor} />
15-
<Eraser editor={editor} />
1610
<Translation editor={editor} />
1711
</div>
1812
}

src/app/core/article/custom-toolbar/polish.tsx

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/app/core/article/custom-toolbar/question.tsx

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { TooltipButton } from "@/components/tooltip-button";
2+
import { fetchAiStreamToken } from "@/lib/ai";
3+
import { EraserIcon } from "lucide-react";
4+
import Vditor from "vditor";
5+
import { useTranslations } from "next-intl";
6+
7+
export default function Eraser({editor, value}: {editor?: Vditor, value?: string}) {
8+
const t = useTranslations('article.editor.toolbar.eraser')
9+
async function handleBlock() {
10+
if (value) {
11+
editor?.deleteValue()
12+
editor?.disabled()
13+
const req = t('promptTemplate', {content: value})
14+
await fetchAiStreamToken(req, (text) => {
15+
editor?.insertValue(text, true)
16+
})
17+
editor?.enable()
18+
}
19+
}
20+
return (
21+
<TooltipButton icon={<EraserIcon />} tooltipText={t('tooltip')} onClick={handleBlock}>
22+
</TooltipButton>
23+
)
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { TooltipButton } from "@/components/tooltip-button";
2+
import { fetchAiStreamToken } from "@/lib/ai";
3+
import { SquareCodeIcon } from "lucide-react";
4+
import Vditor from "vditor";
5+
import { useTranslations } from "next-intl";
6+
7+
export default function Expansion({editor, value}: {editor?: Vditor, value?: string}) {
8+
const t = useTranslations('article.editor.toolbar.expansion')
9+
async function handleBlock() {
10+
if (value) {
11+
editor?.deleteValue()
12+
editor?.disabled()
13+
const req = t('promptTemplate', {content: value})
14+
await fetchAiStreamToken(req, (text) => {
15+
editor?.insertValue(text, true)
16+
})
17+
editor?.enable()
18+
}
19+
}
20+
return (
21+
<TooltipButton icon={<SquareCodeIcon />} tooltipText={t('tooltip')} onClick={handleBlock}>
22+
</TooltipButton>
23+
)
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use client'
2+
import Question from "./question";
3+
import Polish from "./polish";
4+
import Eraser from "./eraser";
5+
import Expansion from "./expansion";
6+
import Vditor from "vditor";
7+
8+
export default function FloatBar({left, top, value, editor}: {left?: number, top?: number, value?: string, editor?: Vditor}) {
9+
return (
10+
left && top ? (
11+
<div
12+
className="absolute shadow rounded-lg bg-primary text-primary-foreground p-1"
13+
style={{left: left + 'px', top: top < 64 ? top + 82 + 'px' : top + 'px'}}>
14+
<div className="flex items-center justify-between">
15+
<div className="flex items-center">
16+
<Question editor={editor} value={value} />
17+
<Polish editor={editor} value={value} />
18+
<Eraser editor={editor} value={value} />
19+
<Expansion editor={editor} value={value} />
20+
</div>
21+
</div>
22+
</div>
23+
) : null
24+
)
25+
}

0 commit comments

Comments
 (0)