Spaces:
Running
Running
Surf commited on
Commit ·
32a7b67
1
Parent(s): 5c8bf61
在原平台侧栏直接增加开始创作和作品列表
Browse files- fanqie/dist/index.html +4 -6
- fanqie/dist/project-integration.js +288 -0
fanqie/dist/index.html
CHANGED
|
@@ -79,13 +79,10 @@
|
|
| 79 |
</script>
|
| 80 |
<!-- 素材批次下载入口 -->
|
| 81 |
<style>
|
| 82 |
-
#batch-download-link
|
| 83 |
-
#batch-download-link{
|
| 84 |
-
#
|
| 85 |
-
#batch-download-link:hover,#writer-workspace-link:hover{opacity:1;transform:translateY(-1px)}
|
| 86 |
-
@media print{#batch-download-link,#writer-workspace-link{display:none}}
|
| 87 |
</style>
|
| 88 |
-
<a id="writer-workspace-link" href="/fanqie/workspace" aria-label="我的作品">📖 我的作品</a>
|
| 89 |
<a id="batch-download-link" href="/fanqie/batches" aria-label="素材批次下载">⬇ 素材批次下载</a>
|
| 90 |
<!-- Made by Surf badge -->
|
| 91 |
<style>
|
|
@@ -134,5 +131,6 @@
|
|
| 134 |
}catch(e){}
|
| 135 |
})();
|
| 136 |
</script>
|
|
|
|
| 137 |
</body>
|
| 138 |
</html>
|
|
|
|
| 79 |
</script>
|
| 80 |
<!-- 素材批次下载入口 -->
|
| 81 |
<style>
|
| 82 |
+
#batch-download-link{position:fixed;top:16px;right:16px;z-index:2147483647;display:inline-flex;align-items:center;gap:6px;padding:8px 13px;border-radius:10px;font-family:system-ui,-apple-system,"PingFang SC","Microsoft YaHei",sans-serif;font-size:13px;font-weight:650;line-height:18px;text-decoration:none;background:#24283a;color:#f4f5fb;border:1px solid #454c69;box-shadow:0 5px 18px rgba(0,0,0,.18);opacity:.94;transition:opacity .2s,transform .2s}
|
| 83 |
+
#batch-download-link:hover{opacity:1;transform:translateY(-1px)}
|
| 84 |
+
@media print{#batch-download-link{display:none}}
|
|
|
|
|
|
|
| 85 |
</style>
|
|
|
|
| 86 |
<a id="batch-download-link" href="/fanqie/batches" aria-label="素材批次下载">⬇ 素材批次下载</a>
|
| 87 |
<!-- Made by Surf badge -->
|
| 88 |
<style>
|
|
|
|
| 131 |
}catch(e){}
|
| 132 |
})();
|
| 133 |
</script>
|
| 134 |
+
<script src="/fanqie/project-integration.js"></script>
|
| 135 |
</body>
|
| 136 |
</html>
|
fanqie/dist/project-integration.js
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
(function () {
|
| 2 |
+
'use strict'
|
| 3 |
+
var API = '/fanqie/api'
|
| 4 |
+
var PASSWORD_KEY = 'xiaoshuo_app_password'
|
| 5 |
+
var projectCache = []
|
| 6 |
+
var projectTimer = null
|
| 7 |
+
var selectedProject = null
|
| 8 |
+
var selectedChapter = null
|
| 9 |
+
|
| 10 |
+
function password() {
|
| 11 |
+
try { return localStorage.getItem(PASSWORD_KEY) || '' } catch (_) { return '' }
|
| 12 |
+
}
|
| 13 |
+
function esc(value) {
|
| 14 |
+
return String(value == null ? '' : value).replace(/[&<>"']/g, function (c) {
|
| 15 |
+
return ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]
|
| 16 |
+
})
|
| 17 |
+
}
|
| 18 |
+
function escAttr(value) { return esc(value).replace(/`/g, '`') }
|
| 19 |
+
function json(value, fallback) {
|
| 20 |
+
try { return JSON.stringify(value == null ? fallback : value, null, 2) } catch (_) { return JSON.stringify(fallback, null, 2) }
|
| 21 |
+
}
|
| 22 |
+
async function api(path, options) {
|
| 23 |
+
options = options || {}
|
| 24 |
+
var headers = Object.assign({ 'Content-Type': 'application/json', 'x-app-password': encodeURIComponent(password()) }, options.headers || {})
|
| 25 |
+
var response = await fetch(API + path, Object.assign({}, options, { headers: headers }))
|
| 26 |
+
if (!response.ok) {
|
| 27 |
+
var message = response.statusText
|
| 28 |
+
try { message = (await response.json()).error || message } catch (_) {}
|
| 29 |
+
if (response.status === 401) window.dispatchEvent(new Event('fq-need-password'))
|
| 30 |
+
throw new Error(message)
|
| 31 |
+
}
|
| 32 |
+
return response.status === 204 ? null : response.json()
|
| 33 |
+
}
|
| 34 |
+
function navButton(label, icon, active) {
|
| 35 |
+
var button = document.createElement('button')
|
| 36 |
+
button.type = 'button'
|
| 37 |
+
button.className = 'flex w-full items-center gap-2.5 rounded-lg px-3 py-2 text-sm font-bold transition ' + (active ? 'bg-[#ff6b4a] text-white' : 'text-muted-foreground hover:bg-accent hover:text-foreground')
|
| 38 |
+
button.innerHTML = '<span>' + icon + '</span>' + esc(label)
|
| 39 |
+
return button
|
| 40 |
+
}
|
| 41 |
+
function findSidebar() { return document.querySelector('aside') }
|
| 42 |
+
function findSidebarNav() {
|
| 43 |
+
var aside = findSidebar()
|
| 44 |
+
if (!aside) return null
|
| 45 |
+
return aside.querySelector('nav')
|
| 46 |
+
}
|
| 47 |
+
function setHash(value) {
|
| 48 |
+
if (location.hash !== '#' + value) history.pushState({}, '', '#' + value)
|
| 49 |
+
renderForHash()
|
| 50 |
+
}
|
| 51 |
+
function ensureSidebar() {
|
| 52 |
+
var aside = findSidebar()
|
| 53 |
+
var nav = findSidebarNav()
|
| 54 |
+
if (!aside || !nav) return false
|
| 55 |
+
var start = document.getElementById('surf-start-writing')
|
| 56 |
+
if (!start || !nav.contains(start)) {
|
| 57 |
+
if (start) start.remove()
|
| 58 |
+
start = navButton('开始创作', '✍️', false)
|
| 59 |
+
start.id = 'surf-start-writing'
|
| 60 |
+
start.addEventListener('click', function () { setHash('projects') })
|
| 61 |
+
nav.appendChild(start)
|
| 62 |
+
}
|
| 63 |
+
var list = document.getElementById('surf-project-list')
|
| 64 |
+
if (!list || !aside.contains(list)) {
|
| 65 |
+
if (list) list.remove()
|
| 66 |
+
list = document.createElement('div')
|
| 67 |
+
list.id = 'surf-project-list'
|
| 68 |
+
list.className = 'mt-3 border-t border-border pt-3'
|
| 69 |
+
var title = document.createElement('div')
|
| 70 |
+
title.className = 'mb-2 px-3 text-[11px] font-bold text-muted-foreground'
|
| 71 |
+
title.textContent = '我的作品'
|
| 72 |
+
list.appendChild(title)
|
| 73 |
+
var anchor = nav.nextElementSibling
|
| 74 |
+
aside.insertBefore(list, anchor || null)
|
| 75 |
+
}
|
| 76 |
+
renderProjectList()
|
| 77 |
+
return true
|
| 78 |
+
}
|
| 79 |
+
function renderProjectList() {
|
| 80 |
+
var list = document.getElementById('surf-project-list')
|
| 81 |
+
if (!list) return
|
| 82 |
+
var title = list.firstElementChild
|
| 83 |
+
list.innerHTML = ''
|
| 84 |
+
if (title) list.appendChild(title)
|
| 85 |
+
if (!projectCache.length) {
|
| 86 |
+
var empty = document.createElement('div')
|
| 87 |
+
empty.className = 'px-3 py-1 text-xs text-muted-foreground'
|
| 88 |
+
empty.textContent = '还没有作品'
|
| 89 |
+
list.appendChild(empty)
|
| 90 |
+
return
|
| 91 |
+
}
|
| 92 |
+
projectCache.forEach(function (project) {
|
| 93 |
+
var button = document.createElement('button')
|
| 94 |
+
button.type = 'button'
|
| 95 |
+
button.className = 'block w-full truncate rounded-lg px-3 py-1.5 text-left text-xs text-muted-foreground hover:bg-accent hover:text-foreground ' + (selectedProject && selectedProject.project_id === project.project_id ? 'bg-accent text-foreground' : '')
|
| 96 |
+
button.title = project.title
|
| 97 |
+
button.textContent = '📖 ' + project.title
|
| 98 |
+
button.addEventListener('click', function () { openProject(project.project_id) })
|
| 99 |
+
list.appendChild(button)
|
| 100 |
+
})
|
| 101 |
+
}
|
| 102 |
+
async function loadProjects() {
|
| 103 |
+
try {
|
| 104 |
+
var data = await api('/projects')
|
| 105 |
+
projectCache = data.projects || []
|
| 106 |
+
ensureSidebar()
|
| 107 |
+
renderProjectList()
|
| 108 |
+
} catch (_) {
|
| 109 |
+
ensureSidebar()
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
function panel() {
|
| 113 |
+
var element = document.getElementById('surf-project-panel')
|
| 114 |
+
if (element) return element
|
| 115 |
+
element = document.createElement('section')
|
| 116 |
+
element.id = 'surf-project-panel'
|
| 117 |
+
element.className = 'surf-project-panel'
|
| 118 |
+
document.body.appendChild(element)
|
| 119 |
+
return element
|
| 120 |
+
}
|
| 121 |
+
function message(text, error) {
|
| 122 |
+
var node = document.getElementById('surf-project-message')
|
| 123 |
+
if (node) {
|
| 124 |
+
node.textContent = text || ''
|
| 125 |
+
node.className = 'text-xs ' + (error ? 'text-red-300' : 'text-emerald-300')
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
function projectShell(content) {
|
| 129 |
+
return '<div class="mx-auto max-w-6xl px-4 py-6 md:px-8 md:py-10"><div class="mb-6 flex items-start justify-between gap-4"><div><div class="mb-1 text-xs font-bold uppercase tracking-wider text-[#ff6b4a]">番茄写作工作台 / 我的作品</div><h1 class="text-2xl font-black">' + (selectedProject ? esc(selectedProject.title) : '开始创作') + '</h1><p class="mt-1 text-sm text-muted-foreground">项目数据、作品圣经、章节正文和版本都保存到 R2。</p></div><div class="flex gap-2"><button id="surf-project-back" class="rounded-lg border border-border px-3 py-2 text-sm font-bold text-muted-foreground hover:text-foreground">← 返回平台</button>' + (selectedProject ? '<button id="surf-new-chapter-top" class="rounded-lg bg-[#ff6b4a] px-3 py-2 text-sm font-bold text-white">+ 新章节</button>' : '') + '</div></div>' + content + '<div id="surf-project-message" class="mt-3 min-h-5"></div></div>'
|
| 130 |
+
}
|
| 131 |
+
function renderProjectHome() {
|
| 132 |
+
selectedProject = null
|
| 133 |
+
selectedChapter = null
|
| 134 |
+
var html = '<div class="grid gap-4 md:grid-cols-2"><div class="rounded-xl border border-border bg-card/50 p-5"><h2 class="text-lg font-black">开始一部新作品</h2><p class="mt-2 text-sm text-muted-foreground">先建立作品,再逐步完善人物、冲突、总纲、章节和正文版本。</p><button id="surf-create-project" class="mt-5 rounded-lg bg-[#ff6b4a] px-4 py-2 text-sm font-bold text-white">+ 新建作品</button></div><div class="rounded-xl border border-border bg-card/50 p-5"><h2 class="text-lg font-black">写作流程</h2><ol class="mt-2 space-y-1 text-sm text-muted-foreground"><li>1. 填写作品信息和一句话创意</li><li>2. 建立作品圣经与总纲</li><li>3. 创建章节和场景卡</li><li>4. 保存正文版本并检查、回滚</li></ol></div></div><div class="mt-5 rounded-xl border border-border bg-card/50 p-5"><h2 class="text-lg font-black">我的作品</h2><div id="surf-project-cards" class="mt-3 grid gap-3 md:grid-cols-2"></div></div>'
|
| 135 |
+
panel().innerHTML = projectShell(html)
|
| 136 |
+
document.getElementById('surf-project-back').onclick = function () { setHash('home') }
|
| 137 |
+
document.getElementById('surf-create-project').onclick = createProject
|
| 138 |
+
var cards = document.getElementById('surf-project-cards')
|
| 139 |
+
if (!projectCache.length) cards.innerHTML = '<div class="text-sm text-muted-foreground">还没有作品,点击上面的按钮开始创作。</div>'
|
| 140 |
+
projectCache.forEach(function (p) {
|
| 141 |
+
var card = document.createElement('button')
|
| 142 |
+
card.type = 'button'
|
| 143 |
+
card.className = 'rounded-xl border border-border bg-background/50 p-4 text-left hover:border-[#ff6b4a]'
|
| 144 |
+
card.innerHTML = '<div class="font-bold">📖 ' + esc(p.title) + '</div><div class="mt-1 text-xs text-muted-foreground">' + esc(p.genre || '未设置题材') + ' · ' + esc(p.target_platform || '未设置平台') + '</div><div class="mt-2 line-clamp-2 text-xs text-muted-foreground">' + esc(p.premise || '还没有一句话创意') + '</div>'
|
| 145 |
+
card.onclick = function () { openProject(p.project_id) }
|
| 146 |
+
cards.appendChild(card)
|
| 147 |
+
})
|
| 148 |
+
}
|
| 149 |
+
async function createProject() {
|
| 150 |
+
var title = window.prompt('作品名称')
|
| 151 |
+
if (!title || !title.trim()) return
|
| 152 |
+
try {
|
| 153 |
+
var data = await api('/projects', { method: 'POST', body: JSON.stringify({ title: title.trim(), genre: '', target_platform: '番茄小说', target_words: 150000, pov: '第三人称有限视角', style: '', premise: '' }) })
|
| 154 |
+
await loadProjects()
|
| 155 |
+
openProject(data.project.project_id)
|
| 156 |
+
} catch (e) { message(e.message, true) }
|
| 157 |
+
}
|
| 158 |
+
async function openProject(id) {
|
| 159 |
+
try {
|
| 160 |
+
var data = await api('/projects/' + encodeURIComponent(id))
|
| 161 |
+
selectedProject = data
|
| 162 |
+
selectedChapter = null
|
| 163 |
+
setHash('projects/' + encodeURIComponent(id))
|
| 164 |
+
renderProjectEditor()
|
| 165 |
+
renderProjectList()
|
| 166 |
+
} catch (e) { renderProjectHome(); message(e.message, true) }
|
| 167 |
+
}
|
| 168 |
+
function renderProjectEditor() {
|
| 169 |
+
var p = selectedProject.project || {}
|
| 170 |
+
var b = selectedProject.bible || {}
|
| 171 |
+
var o = selectedProject.outline || {}
|
| 172 |
+
var chapters = selectedProject.chapters || []
|
| 173 |
+
var html = '<div class="grid gap-4 lg:grid-cols-2"><div class="rounded-xl border border-border bg-card/50 p-5"><h2 class="mb-3 text-lg font-black">作品信息</h2><div class="grid gap-3 md:grid-cols-2"><label class="text-xs text-muted-foreground">作品名<input id="sp-title" class="sp-input" value="' + escAttr(p.title) + '"></label><label class="text-xs text-muted-foreground">题材<input id="sp-genre" class="sp-input" value="' + escAttr(p.genre) + '"></label><label class="text-xs text-muted-foreground">目标平台<input id="sp-platform" class="sp-input" value="' + escAttr(p.target_platform) + '"></label><label class="text-xs text-muted-foreground">目标字数<input id="sp-words" type="number" class="sp-input" value="' + escAttr(p.target_words || '') + '"></label><label class="text-xs text-muted-foreground">叙事视角<input id="sp-pov" class="sp-input" value="' + escAttr(p.pov) + '"></label><label class="text-xs text-muted-foreground">文风<input id="sp-style" class="sp-input" value="' + escAttr(p.style) + '"></label></div><label class="mt-3 block text-xs text-muted-foreground">一句话创意<textarea id="sp-premise" class="sp-textarea">' + esc(p.premise) + '</textarea></label><button id="sp-save-project" class="mt-3 rounded-lg bg-[#ff6b4a] px-4 py-2 text-sm font-bold text-white">保存作品信息</button></div><div class="rounded-xl border border-border bg-card/50 p-5"><h2 class="mb-3 text-lg font-black">作品圣经</h2><label class="text-xs text-muted-foreground">核心冲突<textarea id="sp-core" class="sp-textarea">' + esc(b.story_core) + '</textarea></label><label class="mt-3 block text-xs text-muted-foreground">主线<textarea id="sp-plot" class="sp-textarea">' + esc(b.main_plot) + '</textarea></label><label class="mt-3 block text-xs text-muted-foreground">结局方向<textarea id="sp-ending" class="sp-textarea">' + esc(b.ending_direction) + '</textarea></label><label class="mt-3 block text-xs text-muted-foreground">读者期待<textarea id="sp-reader" class="sp-textarea">' + esc(b.reader_expectation) + '</textarea></label><button id="sp-save-bible" class="mt-3 rounded-lg border border-border px-4 py-2 text-sm font-bold hover:border-[#ff6b4a]">保存作品圣经</button></div></div><div class="mt-4 rounded-xl border border-border bg-card/50 p-5"><div class="flex items-center justify-between"><h2 class="text-lg font-black">总纲与章节</h2><button id="sp-save-outline" class="rounded-lg border border-border px-3 py-2 text-xs font-bold hover:border-[#ff6b4a]">保存总纲</button></div><label class="mt-3 block text-xs text-muted-foreground">三幕结构 JSON<textarea id="sp-acts" class="sp-textarea sp-json">' + esc(json(o.three_act, [])) + '</textarea></label><div class="mt-4 grid gap-4 lg:grid-cols-[250px_1fr]"><div><div class="mb-2 flex items-center justify-between"><b class="text-sm">章节</b><button id="sp-new-chapter" class="rounded border border-border px-2 py-1 text-xs">+ 新建</button></div><div id="sp-chapter-list" class="space-y-2"></div></div><div id="sp-chapter-editor"><div class="rounded-lg border border-dashed border-border p-6 text-sm text-muted-foreground">选择章节,开始写章纲和正文。</div></div></div></div>'
|
| 174 |
+
panel().innerHTML = projectShell(html)
|
| 175 |
+
document.getElementById('surf-project-back').onclick = function () { setHash('home') }
|
| 176 |
+
document.getElementById('sp-save-project').onclick = saveProject
|
| 177 |
+
document.getElementById('sp-save-bible').onclick = saveBible
|
| 178 |
+
document.getElementById('sp-save-outline').onclick = saveOutline
|
| 179 |
+
document.getElementById('sp-new-chapter').onclick = createChapter
|
| 180 |
+
document.getElementById('surf-new-chapter-top').onclick = createChapter
|
| 181 |
+
renderChapterList(chapters)
|
| 182 |
+
}
|
| 183 |
+
function renderChapterList(chapters) {
|
| 184 |
+
var list = document.getElementById('sp-chapter-list')
|
| 185 |
+
if (!list) return
|
| 186 |
+
if (!chapters.length) { list.innerHTML = '<div class="text-xs text-muted-foreground">还没有章节。</div>'; return }
|
| 187 |
+
list.innerHTML = chapters.map(function (c) { return '<button class="sp-chapter ' + (selectedChapter && selectedChapter.chapter_id === c.chapter_id ? 'active' : '') + '" data-cid="' + escAttr(c.chapter_id) + '">第 ' + esc(c.number) + ' 章:' + esc(c.title) + '<small>' + esc(c.status || 'planned') + '</small></button>' }).join('')
|
| 188 |
+
list.querySelectorAll('[data-cid]').forEach(function (button) { button.onclick = function () { openChapter(button.dataset.cid) } })
|
| 189 |
+
}
|
| 190 |
+
async function saveProject() {
|
| 191 |
+
try {
|
| 192 |
+
await api('/projects/' + selectedProject.project.project_id, { method: 'PATCH', body: JSON.stringify({ title: document.getElementById('sp-title').value, genre: document.getElementById('sp-genre').value, target_platform: document.getElementById('sp-platform').value, target_words: Number(document.getElementById('sp-words').value || 0), pov: document.getElementById('sp-pov').value, style: document.getElementById('sp-style').value, premise: document.getElementById('sp-premise').value }) })
|
| 193 |
+
await loadProjects(); message('作品信息已保存')
|
| 194 |
+
} catch (e) { message(e.message, true) }
|
| 195 |
+
}
|
| 196 |
+
async function saveBible() {
|
| 197 |
+
try {
|
| 198 |
+
await api('/projects/' + selectedProject.project.project_id + '/bible', { method: 'PUT', body: JSON.stringify({ story_core: document.getElementById('sp-core').value, main_plot: document.getElementById('sp-plot').value, ending_direction: document.getElementById('sp-ending').value, reader_expectation: document.getElementById('sp-reader').value, characters: selectedProject.bible.characters || [], relationships: selectedProject.bible.relationships || [], timeline: selectedProject.bible.timeline || [], settings: selectedProject.bible.settings || [], foreshadowing: selectedProject.bible.foreshadowing || [] }) })
|
| 199 |
+
message('作品圣经已保存')
|
| 200 |
+
} catch (e) { message(e.message, true) }
|
| 201 |
+
}
|
| 202 |
+
async function saveOutline() {
|
| 203 |
+
try {
|
| 204 |
+
var acts
|
| 205 |
+
try { acts = JSON.parse(document.getElementById('sp-acts').value || '[]') } catch (_) { throw new Error('三幕结构 JSON 格式错误') }
|
| 206 |
+
await api('/projects/' + selectedProject.project.project_id + '/outline', { method: 'PUT', body: JSON.stringify(Object.assign({}, selectedProject.outline, { three_act: acts })) })
|
| 207 |
+
message('总纲已保存')
|
| 208 |
+
} catch (e) { message(e.message, true) }
|
| 209 |
+
}
|
| 210 |
+
async function createChapter() {
|
| 211 |
+
try {
|
| 212 |
+
var data = await api('/projects/' + selectedProject.project.project_id + '/chapters', { method: 'POST', body: JSON.stringify({ title: '未命名章节', goal: '' }) })
|
| 213 |
+
var refreshed = await api('/projects/' + selectedProject.project.project_id)
|
| 214 |
+
selectedProject = refreshed
|
| 215 |
+
renderProjectEditor()
|
| 216 |
+
openChapter(data.chapter_id)
|
| 217 |
+
} catch (e) { message(e.message, true) }
|
| 218 |
+
}
|
| 219 |
+
async function openChapter(id) {
|
| 220 |
+
try {
|
| 221 |
+
var data = await api('/projects/' + selectedProject.project.project_id + '/chapters/' + encodeURIComponent(id))
|
| 222 |
+
selectedChapter = data.chapter
|
| 223 |
+
renderChapterList(selectedProject.chapters || [])
|
| 224 |
+
renderChapterEditor(data)
|
| 225 |
+
} catch (e) { message(e.message, true) }
|
| 226 |
+
}
|
| 227 |
+
function renderChapterEditor(data) {
|
| 228 |
+
var c = data.chapter || {}
|
| 229 |
+
var versions = data.versions || []
|
| 230 |
+
var editor = document.getElementById('sp-chapter-editor')
|
| 231 |
+
if (!editor) return
|
| 232 |
+
editor.innerHTML = '<div class="rounded-lg border border-border p-4"><div class="grid gap-3 md:grid-cols-2"><label class="text-xs text-muted-foreground">章节标题<input id="sc-title" class="sp-input" value="' + escAttr(c.title) + '"></label><label class="text-xs text-muted-foreground">所属卷<input id="sc-volume" class="sp-input" value="' + escAttr(c.volume) + '"></label></div><label class="mt-3 block text-xs text-muted-foreground">本章目标<textarea id="sc-goal" class="sp-textarea">' + esc(c.goal) + '</textarea></label><label class="mt-3 block text-xs text-muted-foreground">章纲<textarea id="sc-outline" class="sp-textarea">' + esc(c.outline) + '</textarea></label><label class="mt-3 block text-xs text-muted-foreground">上一章摘要<textarea id="sc-prev" class="sp-textarea">' + esc(c.previous_summary) + '</textarea></label><button id="sc-save" class="mt-3 rounded-lg border border-border px-3 py-2 text-xs font-bold hover:border-[#ff6b4a]">保存章纲</button></div><div class="mt-4 rounded-lg border border-border p-4"><div class="mb-2 flex items-center justify-between"><b class="text-sm">正文版本</b><span class="text-xs text-muted-foreground">' + versions.length + ' 个版本</span></div><textarea id="sc-text" class="sp-textarea min-h-[260px]" placeholder="在这里写正文,保存时会生成新的版本。"></textarea><div class="mt-3 grid gap-3 md:grid-cols-2"><label class="text-xs text-muted-foreground">本版本摘要<textarea id="sc-summary" class="sp-textarea"></textarea></label><label class="text-xs text-muted-foreground">修改说明<textarea id="sc-notes" class="sp-textarea"></textarea></label></div><button id="sc-save-version" class="mt-3 rounded-lg bg-[#ff6b4a] px-3 py-2 text-xs font-bold text-white">保存为新版本</button><div class="mt-3 space-y-1" id="sc-versions"></div></div>'
|
| 233 |
+
document.getElementById('sc-save').onclick = saveChapter
|
| 234 |
+
document.getElementById('sc-save-version').onclick = saveVersion
|
| 235 |
+
var versionList = document.getElementById('sc-versions')
|
| 236 |
+
versionList.innerHTML = versions.length ? versions.map(function (v) { return '<button class="sp-version" data-vid="' + escAttr(v.version_id) + '">' + esc(new Date(v.created_at).toLocaleString()) + ' · ' + esc(v.word_count) + '字</button>' }).join('') : '<div class="text-xs text-muted-foreground">还没有正文版本。</div>'
|
| 237 |
+
versionList.querySelectorAll('[data-vid]').forEach(function (button) { button.onclick = function () { loadVersion(button.dataset.vid) } })
|
| 238 |
+
}
|
| 239 |
+
async function saveChapter() {
|
| 240 |
+
try {
|
| 241 |
+
await api('/projects/' + selectedProject.project.project_id + '/chapters/' + selectedChapter.chapter_id, { method: 'PATCH', body: JSON.stringify({ title: document.getElementById('sc-title').value, volume: document.getElementById('sc-volume').value, goal: document.getElementById('sc-goal').value, outline: document.getElementById('sc-outline').value, previous_summary: document.getElementById('sc-prev').value }) })
|
| 242 |
+
message('章纲已保存')
|
| 243 |
+
} catch (e) { message(e.message, true) }
|
| 244 |
+
}
|
| 245 |
+
async function saveVersion() {
|
| 246 |
+
try {
|
| 247 |
+
await api('/projects/' + selectedProject.project.project_id + '/chapters/' + selectedChapter.chapter_id + '/versions', { method: 'POST', body: JSON.stringify({ text: document.getElementById('sc-text').value, summary: document.getElementById('sc-summary').value, notes: document.getElementById('sc-notes').value, created_by: 'user' }) })
|
| 248 |
+
var data = await api('/projects/' + selectedProject.project.project_id + '/chapters/' + selectedChapter.chapter_id)
|
| 249 |
+
renderChapterEditor(data); message('正文已保存为新版本')
|
| 250 |
+
} catch (e) { message(e.message, true) }
|
| 251 |
+
}
|
| 252 |
+
async function loadVersion(id) {
|
| 253 |
+
try {
|
| 254 |
+
var version = await api('/projects/' + selectedProject.project.project_id + '/chapters/' + selectedChapter.chapter_id + '/versions/' + encodeURIComponent(id))
|
| 255 |
+
document.getElementById('sc-text').value = version.text || ''
|
| 256 |
+
document.getElementById('sc-summary').value = version.summary || ''
|
| 257 |
+
document.getElementById('sc-notes').value = version.notes || ''
|
| 258 |
+
} catch (e) { message(e.message, true) }
|
| 259 |
+
}
|
| 260 |
+
function renderForHash() {
|
| 261 |
+
var hash = location.hash.slice(1)
|
| 262 |
+
var isProject = hash === 'projects' || hash.indexOf('projects/') === 0
|
| 263 |
+
var projectPanel = document.getElementById('surf-project-panel')
|
| 264 |
+
if (!isProject) {
|
| 265 |
+
if (projectPanel) projectPanel.style.display = 'none'
|
| 266 |
+
return
|
| 267 |
+
}
|
| 268 |
+
if (projectPanel) projectPanel.style.display = 'block'
|
| 269 |
+
if (hash === 'projects') renderProjectHome()
|
| 270 |
+
else if (!selectedProject || selectedProject.project.project_id !== decodeURIComponent(hash.slice(9))) openProject(decodeURIComponent(hash.slice(9)))
|
| 271 |
+
}
|
| 272 |
+
function boot() {
|
| 273 |
+
var observer = new MutationObserver(function () { if (!document.getElementById('surf-start-writing') || !document.getElementById('surf-project-list')) ensureSidebar() })
|
| 274 |
+
observer.observe(document.body, { childList: true, subtree: true })
|
| 275 |
+
ensureSidebar()
|
| 276 |
+
loadProjects()
|
| 277 |
+
window.addEventListener('hashchange', renderForHash)
|
| 278 |
+
window.addEventListener('popstate', renderForHash)
|
| 279 |
+
window.addEventListener('fq-need-password', function () { ensureSidebar() })
|
| 280 |
+
setInterval(function () { if (!projectTimer) { projectTimer = true; loadProjects().finally(function () { projectTimer = false }) } }, 15000)
|
| 281 |
+
renderForHash()
|
| 282 |
+
}
|
| 283 |
+
var css = document.createElement('style')
|
| 284 |
+
css.textContent = '.surf-project-panel{display:none;position:fixed;z-index:40;inset:0 0 0 14rem;overflow:auto;background:hsl(var(--background));color:hsl(var(--foreground))}.sp-input,.sp-textarea{display:block;width:100%;margin-top:5px;border:1px solid hsl(var(--border));border-radius:.5rem;background:hsl(var(--background));color:inherit;padding:.55rem .65rem;font:inherit;font-size:.875rem}.sp-textarea{min-height:78px;resize:vertical}.sp-json{min-height:150px;font-family:ui-monospace,monospace;font-size:.75rem}.sp-chapter{display:block;width:100%;border:1px solid hsl(var(--border));border-radius:.5rem;background:transparent;color:inherit;padding:.55rem .65rem;text-align:left;font-size:.75rem}.sp-chapter:hover,.sp-chapter.active{border-color:#ff6b4a;background:rgba(255,107,74,.1)}.sp-chapter small{display:block;color:hsl(var(--muted-foreground));margin-top:2px}.sp-version{display:block;width:100%;border:1px solid hsl(var(--border));border-radius:.4rem;background:transparent;color:hsl(var(--muted-foreground));padding:.35rem .5rem;text-align:left;font-size:.7rem}.sp-version:hover{border-color:#ff6b4a;color:inherit}@media(max-width:767px){.surf-project-panel{inset:0}}'
|
| 285 |
+
document.head.appendChild(css)
|
| 286 |
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot)
|
| 287 |
+
else boot()
|
| 288 |
+
})()
|