Spaces:
Running
Running
Surf commited on
Commit ·
259ee51
1
Parent(s): 9eab607
优化番茄写作项目工作台与章节版本管理
Browse files- fanqie/dist/index.html +5 -3
- fanqie/dist/project-integration.js +302 -76
- fanqie/projects.js +9 -0
fanqie/dist/index.html
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html lang="
|
| 3 |
<head>
|
| 4 |
<script type="module">
|
| 5 |
(function() {
|
|
@@ -52,9 +52,11 @@
|
|
| 52 |
</script>
|
| 53 |
|
| 54 |
<meta charset="UTF-8" />
|
| 55 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
|
|
|
| 56 |
<link rel="icon" href="/favicon.ico" />
|
| 57 |
-
<title>番茄写作工作台</title>
|
| 58 |
<script>
|
| 59 |
document.documentElement.classList.add('dark');
|
| 60 |
</script>
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="zh-CN">
|
| 3 |
<head>
|
| 4 |
<script type="module">
|
| 5 |
(function() {
|
|
|
|
| 52 |
</script>
|
| 53 |
|
| 54 |
<meta charset="UTF-8" />
|
| 55 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
| 56 |
+
<meta name="theme-color" content="#0f1117" />
|
| 57 |
+
<meta name="description" content="番茄小说写作工作台:素材、母题、作品圣经、章节和版本管理。" />
|
| 58 |
<link rel="icon" href="/favicon.ico" />
|
| 59 |
+
<title>番茄写作工作台 · 素材与项目</title>
|
| 60 |
<script>
|
| 61 |
document.documentElement.classList.add('dark');
|
| 62 |
</script>
|
fanqie/dist/project-integration.js
CHANGED
|
@@ -1,65 +1,99 @@
|
|
| 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({
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 33 |
}
|
| 34 |
-
|
|
|
|
| 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
|
| 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 |
-
|
| 45 |
-
return aside.querySelector('nav')
|
| 46 |
}
|
|
|
|
| 47 |
function setHash(value) {
|
| 48 |
-
|
|
|
|
| 49 |
else 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('
|
| 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()
|
|
@@ -76,6 +110,7 @@
|
|
| 76 |
renderProjectList()
|
| 77 |
return true
|
| 78 |
}
|
|
|
|
| 79 |
function renderProjectList() {
|
| 80 |
var list = document.getElementById('surf-project-list')
|
| 81 |
if (!list) return
|
|
@@ -89,26 +124,36 @@
|
|
| 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 ' + (
|
| 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 |
-
|
|
|
|
| 103 |
try {
|
| 104 |
var data = await api('/projects')
|
| 105 |
projectCache = data.projects || []
|
| 106 |
ensureSidebar()
|
| 107 |
renderProjectList()
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
ensureSidebar()
|
|
|
|
| 110 |
}
|
| 111 |
}
|
|
|
|
| 112 |
function panel() {
|
| 113 |
var element = document.getElementById('surf-project-panel')
|
| 114 |
if (element) return element
|
|
@@ -118,171 +163,352 @@
|
|
| 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 |
-
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
|
|
|
| 128 |
function projectShell(content) {
|
| 129 |
-
|
|
|
|
| 130 |
}
|
|
|
|
| 131 |
function renderProjectHome() {
|
| 132 |
selectedProject = null
|
| 133 |
selectedChapter = null
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
panel().innerHTML = projectShell(html)
|
| 136 |
document.getElementById('surf-project-back').onclick = function () { setHash('home') }
|
| 137 |
document.getElementById('surf-create-project').onclick = createProject
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 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) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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('
|
| 181 |
-
|
| 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/' +
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
} catch (e) { message(e.message, true) }
|
| 195 |
}
|
|
|
|
| 196 |
async function saveBible() {
|
| 197 |
try {
|
| 198 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 207 |
message('总纲已保存')
|
| 208 |
} catch (e) { message(e.message, true) }
|
| 209 |
}
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
| 211 |
try {
|
| 212 |
-
var data = await api('/projects/' +
|
| 213 |
-
|
| 214 |
-
selectedProject = refreshed
|
| 215 |
renderProjectEditor()
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
} catch (e) { message(e.message, true) }
|
| 218 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
async function openChapter(id) {
|
| 220 |
try {
|
| 221 |
-
var data = await api('/projects/' +
|
| 222 |
-
selectedChapter = data
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
document.getElementById('sc-save').onclick = saveChapter
|
| 234 |
document.getElementById('sc-save-version').onclick = saveVersion
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
|
|
|
|
|
|
| 238 |
}
|
|
|
|
| 239 |
async function saveChapter() {
|
| 240 |
try {
|
| 241 |
-
await api('/projects/' +
|
| 242 |
-
|
|
|
|
| 243 |
} catch (e) { message(e.message, true) }
|
| 244 |
}
|
|
|
|
| 245 |
async function saveVersion() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
try {
|
| 247 |
-
await api('/projects/' +
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
| 250 |
} catch (e) { message(e.message, true) }
|
| 251 |
}
|
| 252 |
-
|
|
|
|
| 253 |
try {
|
| 254 |
-
var
|
| 255 |
-
document.getElementById('
|
| 256 |
-
|
| 257 |
-
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
}
|
|
|
|
| 272 |
function boot() {
|
| 273 |
-
var observer = new MutationObserver(function () {
|
|
|
|
|
|
|
| 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 (!
|
| 281 |
renderForHash()
|
| 282 |
}
|
|
|
|
| 283 |
var css = document.createElement('style')
|
| 284 |
-
css.textContent =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
document.head.appendChild(css)
|
|
|
|
| 286 |
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot)
|
| 287 |
else boot()
|
| 288 |
})()
|
|
|
|
| 1 |
(function () {
|
| 2 |
'use strict'
|
| 3 |
+
|
| 4 |
+
// “我的作品”是独立于旧素材平台的轻量工作台;数据仍由 /fanqie/api 写入 R2。
|
| 5 |
var API = '/fanqie/api'
|
| 6 |
var PASSWORD_KEY = 'xiaoshuo_app_password'
|
| 7 |
+
var BASKET_KEY = 'fq-basket'
|
| 8 |
var projectCache = []
|
|
|
|
| 9 |
var selectedProject = null
|
| 10 |
var selectedChapter = null
|
| 11 |
+
var selectedVersionId = null
|
| 12 |
+
var refreshTimer = null
|
| 13 |
+
var loading = false
|
| 14 |
|
| 15 |
function password() {
|
| 16 |
try { return localStorage.getItem(PASSWORD_KEY) || '' } catch (_) { return '' }
|
| 17 |
}
|
| 18 |
+
|
| 19 |
function esc(value) {
|
| 20 |
return String(value == null ? '' : value).replace(/[&<>"']/g, function (c) {
|
| 21 |
return ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]
|
| 22 |
})
|
| 23 |
}
|
| 24 |
+
|
| 25 |
function escAttr(value) { return esc(value).replace(/`/g, '`') }
|
| 26 |
+
|
| 27 |
function json(value, fallback) {
|
| 28 |
try { return JSON.stringify(value == null ? fallback : value, null, 2) } catch (_) { return JSON.stringify(fallback, null, 2) }
|
| 29 |
}
|
| 30 |
+
|
| 31 |
+
function projectId() {
|
| 32 |
+
return selectedProject && selectedProject.project ? selectedProject.project.project_id : ''
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
function readBasket() {
|
| 36 |
+
try {
|
| 37 |
+
var value = JSON.parse(localStorage.getItem(BASKET_KEY) || '[]')
|
| 38 |
+
return Array.isArray(value) ? value : []
|
| 39 |
+
} catch (_) { return [] }
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
async function api(path, options) {
|
| 43 |
options = options || {}
|
| 44 |
+
var headers = Object.assign({}, options.headers || {})
|
| 45 |
+
if (options.body != null && !headers['Content-Type']) headers['Content-Type'] = 'application/json'
|
| 46 |
+
headers['x-app-password'] = encodeURIComponent(password())
|
| 47 |
+
var response
|
| 48 |
+
try {
|
| 49 |
+
response = await fetch(API + path, Object.assign({}, options, { headers: headers, cache: 'no-store' }))
|
| 50 |
+
} catch (e) {
|
| 51 |
+
throw new Error('网络连接失败,请检查 Space 是否正在启动')
|
| 52 |
+
}
|
| 53 |
if (!response.ok) {
|
| 54 |
+
var message = response.statusText || ('请求失败(' + response.status + ')')
|
| 55 |
try { message = (await response.json()).error || message } catch (_) {}
|
| 56 |
if (response.status === 401) window.dispatchEvent(new Event('fq-need-password'))
|
| 57 |
throw new Error(message)
|
| 58 |
}
|
| 59 |
+
if (response.status === 204) return null
|
| 60 |
+
return response.json()
|
| 61 |
}
|
| 62 |
+
|
| 63 |
+
function navButton(label, icon) {
|
| 64 |
var button = document.createElement('button')
|
| 65 |
button.type = 'button'
|
| 66 |
+
button.className = 'flex w-full items-center gap-2.5 rounded-lg px-3 py-2 text-sm font-bold transition text-muted-foreground hover:bg-accent hover:text-foreground'
|
| 67 |
button.innerHTML = '<span>' + icon + '</span>' + esc(label)
|
| 68 |
return button
|
| 69 |
}
|
| 70 |
+
|
| 71 |
function findSidebar() { return document.querySelector('aside') }
|
| 72 |
function findSidebarNav() {
|
| 73 |
var aside = findSidebar()
|
| 74 |
+
return aside ? aside.querySelector('nav') : null
|
|
|
|
| 75 |
}
|
| 76 |
+
|
| 77 |
function setHash(value) {
|
| 78 |
+
var next = '#' + value
|
| 79 |
+
if (location.hash !== next) location.hash = next
|
| 80 |
else renderForHash()
|
| 81 |
}
|
| 82 |
+
|
| 83 |
function ensureSidebar() {
|
| 84 |
var aside = findSidebar()
|
| 85 |
var nav = findSidebarNav()
|
| 86 |
if (!aside || !nav) return false
|
| 87 |
+
|
| 88 |
var start = document.getElementById('surf-start-writing')
|
| 89 |
if (!start || !nav.contains(start)) {
|
| 90 |
if (start) start.remove()
|
| 91 |
+
start = navButton('我的作品', '✍️')
|
| 92 |
start.id = 'surf-start-writing'
|
| 93 |
start.addEventListener('click', function () { setHash('projects') })
|
| 94 |
nav.appendChild(start)
|
| 95 |
}
|
| 96 |
+
|
| 97 |
var list = document.getElementById('surf-project-list')
|
| 98 |
if (!list || !aside.contains(list)) {
|
| 99 |
if (list) list.remove()
|
|
|
|
| 110 |
renderProjectList()
|
| 111 |
return true
|
| 112 |
}
|
| 113 |
+
|
| 114 |
function renderProjectList() {
|
| 115 |
var list = document.getElementById('surf-project-list')
|
| 116 |
if (!list) return
|
|
|
|
| 124 |
list.appendChild(empty)
|
| 125 |
return
|
| 126 |
}
|
| 127 |
+
var activeId = projectId()
|
| 128 |
projectCache.forEach(function (project) {
|
| 129 |
var button = document.createElement('button')
|
| 130 |
button.type = 'button'
|
| 131 |
+
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 ' + (activeId === project.project_id ? 'bg-accent text-foreground' : '')
|
| 132 |
button.title = project.title
|
| 133 |
button.textContent = '📖 ' + project.title
|
| 134 |
button.addEventListener('click', function () { openProject(project.project_id) })
|
| 135 |
list.appendChild(button)
|
| 136 |
})
|
| 137 |
}
|
| 138 |
+
|
| 139 |
+
async function loadProjects(showError) {
|
| 140 |
try {
|
| 141 |
var data = await api('/projects')
|
| 142 |
projectCache = data.projects || []
|
| 143 |
ensureSidebar()
|
| 144 |
renderProjectList()
|
| 145 |
+
if (selectedProject) {
|
| 146 |
+
var current = projectCache.find(function (p) { return p.project_id === projectId() })
|
| 147 |
+
if (current && selectedProject.project) selectedProject.project = Object.assign({}, selectedProject.project, current)
|
| 148 |
+
} else if (location.hash === '#projects') {
|
| 149 |
+
renderProjectHome()
|
| 150 |
+
}
|
| 151 |
+
} catch (e) {
|
| 152 |
ensureSidebar()
|
| 153 |
+
if (showError) message(e.message, true)
|
| 154 |
}
|
| 155 |
}
|
| 156 |
+
|
| 157 |
function panel() {
|
| 158 |
var element = document.getElementById('surf-project-panel')
|
| 159 |
if (element) return element
|
|
|
|
| 163 |
document.body.appendChild(element)
|
| 164 |
return element
|
| 165 |
}
|
| 166 |
+
|
| 167 |
function message(text, error) {
|
| 168 |
var node = document.getElementById('surf-project-message')
|
| 169 |
+
if (!node) return
|
| 170 |
+
node.textContent = text || ''
|
| 171 |
+
node.className = 'sp-message ' + (error ? 'error' : 'ok')
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
function setBusy(button, busyText) {
|
| 175 |
+
if (!button) return function () {}
|
| 176 |
+
var original = button.textContent
|
| 177 |
+
button.disabled = true
|
| 178 |
+
button.textContent = busyText || '处理中…'
|
| 179 |
+
return function () { button.disabled = false; button.textContent = original }
|
| 180 |
}
|
| 181 |
+
|
| 182 |
function projectShell(content) {
|
| 183 |
+
var title = selectedProject ? selectedProject.project.title : '我的作品'
|
| 184 |
+
return '<div class="sp-shell"><header class="sp-header"><div><div class="sp-kicker">番茄写作工作台 / 我的作品</div><h1>' + esc(title) + '</h1><p>作品圣经、章节正文、版本和写作请求统一保存到 R2。</p></div><div class="sp-header-actions"><button id="surf-project-back" class="sp-btn ghost">← 返回平台</button>' + (selectedProject ? '<button id="surf-refresh-project" class="sp-btn ghost">↻ 刷新</button><button id="surf-new-chapter-top" class="sp-btn primary">+ 新章节</button>' : '') + '</div></header>' + content + '<div id="surf-project-message" class="sp-message"></div></div>'
|
| 185 |
}
|
| 186 |
+
|
| 187 |
function renderProjectHome() {
|
| 188 |
selectedProject = null
|
| 189 |
selectedChapter = null
|
| 190 |
+
selectedVersionId = null
|
| 191 |
+
var cards = projectCache.length ? projectCache.map(function (p) {
|
| 192 |
+
return '<button class="sp-project-card" data-open-project="' + escAttr(p.project_id) + '"><div class="sp-card-title">📖 ' + esc(p.title) + '</div><div class="sp-card-meta">' + esc(p.genre || '未设置题材') + ' · ' + esc(p.target_platform || '未设置平台') + '</div><div class="sp-card-desc">' + esc(p.premise || '还没有一句话创意') + '</div><div class="sp-card-foot">最后更新:' + esc(formatDate(p.updated_at)) + '</div></button>'
|
| 193 |
+
}).join('') : '<div class="sp-empty">还没有作品,先创建一个项目吧。</div>'
|
| 194 |
+
var html = '<div class="sp-home-grid"><section class="sp-card sp-hero"><div class="sp-eyebrow">WRITE WITH CONTEXT</div><h2>把灵感变成可持续写作项目</h2><p>先建立作品设定,再拆总纲、章节和版本。每一步都可保存、检查、回滚,不再靠聊天记录找上下文。</p><div class="sp-actions"><button id="surf-create-project" class="sp-btn primary">+ 新建作品</button><button id="surf-refresh-home" class="sp-btn ghost">↻ 刷新项目</button></div></section><section class="sp-card"><h2>推荐工作流</h2><ol class="sp-steps"><li><b>定义作品</b><span>题材、平台、目标字数和一句话创意</span></li><li><b>建立作品圣经</b><span>核心冲突、人物、关系、时间线和禁区</span></li><li><b>拆分章节</b><span>先写章纲,再保存正文版本</span></li><li><b>检查与回滚</b><span>保留每次修改,随时恢复可用版本</span></li></ol></section></div><section class="sp-card sp-list-card"><div class="sp-section-head"><div><h2>我的作品</h2><p>共 ' + projectCache.length + ' 个项目</p></div><button id="surf-create-project-2" class="sp-btn small">+ 新建</button></div><div class="sp-project-grid">' + cards + '</div></section>'
|
| 195 |
panel().innerHTML = projectShell(html)
|
| 196 |
document.getElementById('surf-project-back').onclick = function () { setHash('home') }
|
| 197 |
document.getElementById('surf-create-project').onclick = createProject
|
| 198 |
+
document.getElementById('surf-create-project-2').onclick = createProject
|
| 199 |
+
document.getElementById('surf-refresh-home').onclick = async function () { var done = setBusy(this, '刷新中…'); await loadProjects(true); done(); renderProjectHome() }
|
| 200 |
+
document.querySelectorAll('[data-open-project]').forEach(function (button) { button.onclick = function () { openProject(button.dataset.openProject) } })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
}
|
| 202 |
+
|
| 203 |
+
function formatDate(value) {
|
| 204 |
+
if (!value) return '暂无'
|
| 205 |
+
try { return new Date(value).toLocaleString('zh-CN', { month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit' }) } catch (_) { return String(value) }
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
async function createProject() {
|
| 209 |
+
var title = window.prompt('作品名称(例如:雨夜里的旧账本)')
|
| 210 |
if (!title || !title.trim()) return
|
| 211 |
try {
|
| 212 |
var data = await api('/projects', { method: 'POST', body: JSON.stringify({ title: title.trim(), genre: '', target_platform: '番茄小说', target_words: 150000, pov: '第三人称有限视角', style: '', premise: '' }) })
|
| 213 |
+
await loadProjects(true)
|
| 214 |
openProject(data.project.project_id)
|
| 215 |
} catch (e) { message(e.message, true) }
|
| 216 |
}
|
| 217 |
+
|
| 218 |
async function openProject(id) {
|
| 219 |
+
if (!id || loading) return
|
| 220 |
+
loading = true
|
| 221 |
try {
|
| 222 |
var data = await api('/projects/' + encodeURIComponent(id))
|
| 223 |
selectedProject = data
|
| 224 |
selectedChapter = null
|
| 225 |
+
selectedVersionId = null
|
| 226 |
setHash('projects/' + encodeURIComponent(id))
|
| 227 |
renderProjectEditor()
|
| 228 |
renderProjectList()
|
| 229 |
+
} catch (e) {
|
| 230 |
+
renderProjectHome()
|
| 231 |
+
message(e.message, true)
|
| 232 |
+
} finally { loading = false }
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
async function refreshProject(silent) {
|
| 236 |
+
if (!projectId()) return
|
| 237 |
+
try {
|
| 238 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()))
|
| 239 |
+
selectedProject = data
|
| 240 |
+
if (selectedChapter) {
|
| 241 |
+
var chapterId = selectedChapter.chapter_id
|
| 242 |
+
try { var detail = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(chapterId)); selectedChapter = detail } catch (_) { selectedChapter = null }
|
| 243 |
+
}
|
| 244 |
+
renderProjectEditor()
|
| 245 |
+
if (!silent) message('项目已刷新')
|
| 246 |
+
} catch (e) { message(e.message, true) }
|
| 247 |
}
|
| 248 |
+
|
| 249 |
function renderProjectEditor() {
|
| 250 |
var p = selectedProject.project || {}
|
| 251 |
var b = selectedProject.bible || {}
|
| 252 |
var o = selectedProject.outline || {}
|
| 253 |
var chapters = selectedProject.chapters || []
|
| 254 |
+
var selectedItems = (selectedProject.materials && selectedProject.materials.items) || []
|
| 255 |
+
var html = '<div class="sp-stats"><div><b>' + chapters.length + '</b><span>章节</span></div><div><b>' + selectedItems.length + '</b><span>参考素材</span></div><div><b>' + esc(p.status || 'planning') + '</b><span>项目状态</span></div><div><b>' + esc(formatDate(p.updated_at)) + '</b><span>最近更新</span></div></div>'
|
| 256 |
+
html += '<div class="sp-editor-grid"><section class="sp-card"><div class="sp-section-head"><div><h2>作品信息</h2><p>先把写作边界定下来,后续生成和检查都会读取这些字段。</p></div></div><div class="sp-form-grid"><label>作品名<input id="sp-title" class="sp-input" value="' + escAttr(p.title) + '"></label><label>题材<input id="sp-genre" class="sp-input" value="' + escAttr(p.genre) + '" placeholder="都市 / 悬疑 / 古言…"></label><label>目标平台<input id="sp-platform" class="sp-input" value="' + escAttr(p.target_platform) + '"></label><label>目标字数<input id="sp-words" type="number" min="0" class="sp-input" value="' + escAttr(p.target_words || '') + '"></label><label>叙事视角<input id="sp-pov" class="sp-input" value="' + escAttr(p.pov) + '" placeholder="第一人称 / 第三人称有限…"></label><label>文风<input id="sp-style" class="sp-input" value="' + escAttr(p.style) + '" placeholder="克制、快节奏、生活流…"></label></div><label>一句话创意<textarea id="sp-premise" class="sp-textarea" placeholder="主角在什么处境下,为了什么目标,必须面对什么冲突?">' + esc(p.premise) + '</textarea><div class="sp-actions"><button id="sp-save-project" class="sp-btn primary">���存作品信息</button></div></section>'
|
| 257 |
+
html += '<section class="sp-card"><div class="sp-section-head"><div><h2>作品圣经</h2><p>这是跨章节的长期设定;保存后可作为 Surf 写作上下文。</p></div></div><label>核心冲突<textarea id="sp-core" class="sp-textarea" placeholder="主角想要什么,谁或什么在阻止他?">' + esc(b.story_core) + '</textarea></label><label>主线<textarea id="sp-plot" class="sp-textarea" placeholder="故事从开端到结局的主线推进">' + esc(b.main_plot) + '</textarea></label><div class="sp-form-grid"><label>结局方向<textarea id="sp-ending" class="sp-textarea short" placeholder="圆满 / 反转 / 留白…">' + esc(b.ending_direction) + '</textarea></label><label>读者期待<textarea id="sp-reader" class="sp-textarea short" placeholder="读者想持续追什么?">' + esc(b.reader_expectation) + '</textarea></label></div><div class="sp-actions"><button id="sp-save-bible" class="sp-btn primary">保存作品圣经</button></div></section></div>'
|
| 258 |
+
html += '<section class="sp-card"><div class="sp-section-head"><div><h2>总纲与写作请求</h2><p>三幕结构使用 JSON 保存;不会覆盖已有章节或正文。</p></div><button id="sp-save-outline" class="sp-btn small">保存总纲</button></div><label>三幕结构<textarea id="sp-acts" class="sp-textarea sp-json" placeholder="[{"act":1,"goal":"…"}]">' + esc(json(o.three_act, [])) + '</textarea></label><div class="sp-request"><div><b>让 Surf 处理当前项目</b><span>保存一条请求后,在对话中让我读取并执行。</span></div><textarea id="sp-request-text" class="sp-textarea" placeholder="例如:根据作品圣经,设计前 10 章的章节目标和每章反转,先不要写正文。"></textarea><div class="sp-actions"><button id="sp-save-request" class="sp-btn ghost">保存写作请求</button></div></div></section>'
|
| 259 |
+
html += '<section class="sp-card"><div class="sp-section-head"><div><h2>参考素材</h2><p>素材库的篮子是浏览器本地状态;点击同步后才会绑定到这个项目。</p></div><button id="sp-sync-basket" class="sp-btn small">同步素材篮</button></div><div id="sp-materials" class="sp-materials">' + renderMaterials(selectedItems) + '</div></section>'
|
| 260 |
+
html += '<section class="sp-card"><div class="sp-section-head"><div><h2>章节</h2><p>先写章纲,再保存正文版本。当前章节会自动记住。</p></div><button id="sp-new-chapter" class="sp-btn primary">+ 新建章节</button></div><div class="sp-chapter-layout"><div id="sp-chapter-list" class="sp-chapter-list"></div><div id="sp-chapter-editor" class="sp-chapter-editor"><div class="sp-placeholder">选择左侧章节,开始写章纲和正文。</div></div></div></section>'
|
| 261 |
+
html += '<div class="sp-danger"><button id="sp-delete-project" class="sp-btn danger">删除这个项目</button><span>会删除 R2 中的章节、版本和请求,无法恢复。</span></div>'
|
| 262 |
panel().innerHTML = projectShell(html)
|
| 263 |
+
bindProjectEditor()
|
| 264 |
+
renderChapterList(chapters)
|
| 265 |
+
if (selectedChapter) renderChapterEditor(selectedChapter)
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
function renderMaterials(items) {
|
| 269 |
+
if (!items.length) return '<div class="sp-placeholder">当前项目还没有绑定参考素材。去素材库加入素材后,再点击“同步素材篮”。</div>'
|
| 270 |
+
return items.map(function (item) { return '<article class="sp-material"><div class="sp-material-head"><b>' + esc(item.sum || item.uid) + '</b><span>' + esc(item.uid) + '</span></div><p>' + esc((item.text || '').slice(0, 600)) + (item.text && item.text.length > 600 ? '…' : '') + '</p></article>' }).join('')
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
function bindProjectEditor() {
|
| 274 |
document.getElementById('surf-project-back').onclick = function () { setHash('home') }
|
| 275 |
+
document.getElementById('surf-refresh-project').onclick = function () { refreshProject(false) }
|
| 276 |
+
document.getElementById('surf-new-chapter-top').onclick = createChapter
|
| 277 |
document.getElementById('sp-save-project').onclick = saveProject
|
| 278 |
document.getElementById('sp-save-bible').onclick = saveBible
|
| 279 |
document.getElementById('sp-save-outline').onclick = saveOutline
|
| 280 |
document.getElementById('sp-new-chapter').onclick = createChapter
|
| 281 |
+
document.getElementById('sp-sync-basket').onclick = syncBasket
|
| 282 |
+
document.getElementById('sp-save-request').onclick = saveRequest
|
| 283 |
+
document.getElementById('sp-delete-project').onclick = deleteProject
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
}
|
| 285 |
+
|
| 286 |
async function saveProject() {
|
| 287 |
try {
|
| 288 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()), { 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 }) })
|
| 289 |
+
selectedProject.project = data
|
| 290 |
+
await loadProjects(false)
|
| 291 |
+
renderProjectList()
|
| 292 |
+
message('作品信息已保存')
|
| 293 |
} catch (e) { message(e.message, true) }
|
| 294 |
}
|
| 295 |
+
|
| 296 |
async function saveBible() {
|
| 297 |
try {
|
| 298 |
+
var current = selectedProject.bible || {}
|
| 299 |
+
selectedProject.bible = await api('/projects/' + encodeURIComponent(projectId()) + '/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: current.characters || [], relationships: current.relationships || [], timeline: current.timeline || [], settings: current.settings || [], foreshadowing: current.foreshadowing || [], open_questions: current.open_questions || [], style_rules: current.style_rules || [], safety_rules: current.safety_rules || [], source_material_uids: current.source_material_uids || [] }) })
|
| 300 |
+
await loadProjects(false)
|
| 301 |
message('作品圣经已保存')
|
| 302 |
} catch (e) { message(e.message, true) }
|
| 303 |
}
|
| 304 |
+
|
| 305 |
async function saveOutline() {
|
| 306 |
try {
|
| 307 |
var acts
|
| 308 |
+
try { acts = JSON.parse(document.getElementById('sp-acts').value || '[]') } catch (_) { throw new Error('三幕结构不是合法 JSON') }
|
| 309 |
+
if (!Array.isArray(acts)) throw new Error('三幕结构必须是数组')
|
| 310 |
+
var current = selectedProject.outline || {}
|
| 311 |
+
selectedProject.outline = await api('/projects/' + encodeURIComponent(projectId()) + '/outline', { method: 'PUT', body: JSON.stringify(Object.assign({}, current, { three_act: acts })) })
|
| 312 |
+
await loadProjects(false)
|
| 313 |
message('总纲已保存')
|
| 314 |
} catch (e) { message(e.message, true) }
|
| 315 |
}
|
| 316 |
+
|
| 317 |
+
async function syncBasket() {
|
| 318 |
+
var basket = readBasket().slice(0, 3)
|
| 319 |
+
if (!basket.length) { message('素材篮是空的,请先在素材库选择素材', true); return }
|
| 320 |
try {
|
| 321 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()) + '/materials', { method: 'PUT', body: JSON.stringify({ items: basket }) })
|
| 322 |
+
selectedProject.materials = data
|
|
|
|
| 323 |
renderProjectEditor()
|
| 324 |
+
message('已同步 ' + basket.length + ' 条素材到当前项目')
|
| 325 |
+
} catch (e) { message(e.message, true) }
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
async function saveRequest() {
|
| 329 |
+
var input = document.getElementById('sp-request-text')
|
| 330 |
+
var instruction = input && input.value.trim()
|
| 331 |
+
if (!instruction) { message('请先填写写作请求', true); return }
|
| 332 |
+
try {
|
| 333 |
+
await api('/projects/' + encodeURIComponent(projectId()) + '/requests', { method: 'POST', body: JSON.stringify({ instruction: instruction, type: 'writing', chapter_id: selectedChapter ? selectedChapter.chapter_id : null }) })
|
| 334 |
+
input.value = ''
|
| 335 |
+
message('写作请求已保存;在对话中让我读取当前项目即可继续')
|
| 336 |
} catch (e) { message(e.message, true) }
|
| 337 |
}
|
| 338 |
+
|
| 339 |
+
async function deleteProject() {
|
| 340 |
+
if (!window.confirm('确定删除“' + (selectedProject.project.title || '') + '”?此操作不可恢复。')) return
|
| 341 |
+
try {
|
| 342 |
+
await api('/projects/' + encodeURIComponent(projectId()), { method: 'DELETE' })
|
| 343 |
+
await loadProjects(false)
|
| 344 |
+
setHash('projects')
|
| 345 |
+
renderProjectHome()
|
| 346 |
+
} catch (e) { message(e.message, true) }
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
function renderChapterList(chapters) {
|
| 350 |
+
var list = document.getElementById('sp-chapter-list')
|
| 351 |
+
if (!list) return
|
| 352 |
+
if (!chapters.length) { list.innerHTML = '<div class="sp-placeholder">还没有章节。</div>'; return }
|
| 353 |
+
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) + '"><b>第 ' + esc(c.number) + ' 章</b><span>' + esc(c.title || '未命名章节') + '</span><small>' + esc(c.status || 'planned') + ' · ' + esc(formatDate(c.updated_at)) + '</small></button>' }).join('')
|
| 354 |
+
list.querySelectorAll('[data-cid]').forEach(function (button) { button.onclick = function () { openChapter(button.dataset.cid) } })
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
async function createChapter() {
|
| 358 |
+
try {
|
| 359 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters', { method: 'POST', body: JSON.stringify({ title: '未命名章节', goal: '' }) })
|
| 360 |
+
await refreshProject(true)
|
| 361 |
+
await openChapter(data.chapter_id)
|
| 362 |
+
message('已创建第 ' + data.number + ' 章')
|
| 363 |
+
} catch (e) { message(e.message, true) }
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
async function openChapter(id) {
|
| 367 |
try {
|
| 368 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(id))
|
| 369 |
+
selectedChapter = data
|
| 370 |
+
selectedVersionId = data.chapter && data.chapter.current_version_id
|
| 371 |
renderChapterList(selectedProject.chapters || [])
|
| 372 |
renderChapterEditor(data)
|
| 373 |
} catch (e) { message(e.message, true) }
|
| 374 |
}
|
| 375 |
+
|
| 376 |
function renderChapterEditor(data) {
|
| 377 |
var c = data.chapter || {}
|
| 378 |
var versions = data.versions || []
|
| 379 |
var editor = document.getElementById('sp-chapter-editor')
|
| 380 |
if (!editor) return
|
| 381 |
+
var versionRows = versions.length ? versions.map(function (v) {
|
| 382 |
+
return '<div class="sp-version-row"><button class="sp-version ' + (selectedVersionId === v.version_id ? 'active' : '') + '" data-vid="' + escAttr(v.version_id) + '"><b>' + esc(formatDate(v.created_at)) + '</b><span>' + esc(v.word_count || 0) + ' 字 · ' + esc(v.kind || 'draft') + '</span><small>' + esc(v.summary || '无摘要') + '</small></button><button class="sp-btn tiny" data-rollback="' + escAttr(v.version_id) + '">回滚</button></div>'
|
| 383 |
+
}).join('') : '<div class="sp-placeholder">还没有正文版本。保存第一版正文后会显示在这里。</div>'
|
| 384 |
+
editor.innerHTML = '<div class="sp-chapter-head"><div><h3>第 ' + esc(c.number) + ' 章:' + esc(c.title) + '</h3><p>修改章纲不会生成正文版本;保存正文才会留下可回滚记录。</p></div><button id="sp-check-chapter" class="sp-btn ghost">✓ 基础检查</button></div><div class="sp-form-grid"><label>章节标题<input id="sc-title" class="sp-input" value="' + escAttr(c.title) + '"></label><label>所属卷<input id="sc-volume" class="sp-input" value="' + escAttr(c.volume) + '"></label></div><label>本章目标<textarea id="sc-goal" class="sp-textarea">' + esc(c.goal) + '</textarea></label><label>章纲<textarea id="sc-outline" class="sp-textarea" placeholder="本章开始状态 → 冲突升级 → 结尾钩子">' + esc(c.outline) + '</textarea></label><label>上一章摘要<textarea id="sc-prev" class="sp-textarea">' + esc(c.previous_summary) + '</textarea></label><div class="sp-actions"><button id="sc-save" class="sp-btn primary">保存章纲</button><span id="sp-chapter-status" class="sp-inline-status"></span></div><div class="sp-divider"></div><div class="sp-version-head"><div><h3>正文版本</h3><p>当前章节共有 ' + versions.length + ' 个版本;点击版本载入正文。</p></div></div><textarea id="sc-text" class="sp-textarea sp-body" placeholder="在这里写正文,保存时会生成新的版本。"></textarea><div class="sp-form-grid"><label>本版本摘要<textarea id="sc-summary" class="sp-textarea short"></textarea></label><label>修改说明<textarea id="sc-notes" class="sp-textarea short"></textarea></label></div><div class="sp-actions"><button id="sc-save-version" class="sp-btn primary">保存为新版本</button><span id="sp-version-status" class="sp-inline-status"></span></div><div class="sp-version-list">' + versionRows + '</div><div id="sp-check-result"></div>'
|
| 385 |
document.getElementById('sc-save').onclick = saveChapter
|
| 386 |
document.getElementById('sc-save-version').onclick = saveVersion
|
| 387 |
+
document.getElementById('sp-check-chapter').onclick = checkChapter
|
| 388 |
+
editor.querySelectorAll('[data-vid]').forEach(function (button) { button.onclick = function () { loadVersion(button.dataset.vid) } })
|
| 389 |
+
editor.querySelectorAll('[data-rollback]').forEach(function (button) { button.onclick = function () { rollbackVersion(button.dataset.rollback) } })
|
| 390 |
+
var currentId = c.current_version_id || (versions[0] && versions[0].version_id)
|
| 391 |
+
if (currentId) loadVersion(currentId, true)
|
| 392 |
}
|
| 393 |
+
|
| 394 |
async function saveChapter() {
|
| 395 |
try {
|
| 396 |
+
await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(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 }) })
|
| 397 |
+
await refreshProject(true)
|
| 398 |
+
message('第 ' + selectedChapter.number + ' 章章纲已保存')
|
| 399 |
} catch (e) { message(e.message, true) }
|
| 400 |
}
|
| 401 |
+
|
| 402 |
async function saveVersion() {
|
| 403 |
+
var text = document.getElementById('sc-text').value
|
| 404 |
+
if (!text.trim()) { message('正文不能为空', true); return }
|
| 405 |
+
try {
|
| 406 |
+
await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(selectedChapter.chapter_id) + '/versions', { method: 'POST', body: JSON.stringify({ text: text, summary: document.getElementById('sc-summary').value, notes: document.getElementById('sc-notes').value, created_by: 'user' }) })
|
| 407 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(selectedChapter.chapter_id))
|
| 408 |
+
selectedChapter = data
|
| 409 |
+
selectedVersionId = data.chapter.current_version_id
|
| 410 |
+
renderChapterEditor(data)
|
| 411 |
+
renderChapterList(selectedProject.chapters || [])
|
| 412 |
+
message('正文已保存为新版本(' + text.length + ' 字)')
|
| 413 |
+
} catch (e) { message(e.message, true) }
|
| 414 |
+
}
|
| 415 |
+
|
| 416 |
+
async function loadVersion(id, silent) {
|
| 417 |
+
try {
|
| 418 |
+
var version = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(selectedChapter.chapter_id) + '/versions/' + encodeURIComponent(id))
|
| 419 |
+
selectedVersionId = id
|
| 420 |
+
var text = document.getElementById('sc-text')
|
| 421 |
+
var summary = document.getElementById('sc-summary')
|
| 422 |
+
var notes = document.getElementById('sc-notes')
|
| 423 |
+
if (text) text.value = version.text || ''
|
| 424 |
+
if (summary) summary.value = version.summary || ''
|
| 425 |
+
if (notes) notes.value = version.notes || ''
|
| 426 |
+
var buttons = document.querySelectorAll('#sp-chapter-editor [data-vid]')
|
| 427 |
+
buttons.forEach(function (button) { button.classList.toggle('active', button.dataset.vid === id) })
|
| 428 |
+
if (!silent) message('已载入版本 ' + formatDate(version.created_at))
|
| 429 |
+
} catch (e) { message(e.message, true) }
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
async function rollbackVersion(id) {
|
| 433 |
+
if (!window.confirm('回滚会把当前章节切换到所选版本,原版本仍会保留。继续吗?')) return
|
| 434 |
try {
|
| 435 |
+
var data = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(selectedChapter.chapter_id) + '/versions/' + encodeURIComponent(id) + '/rollback', { method: 'POST', body: '{}' })
|
| 436 |
+
selectedChapter = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(selectedChapter.chapter_id))
|
| 437 |
+
selectedVersionId = data.version && data.version.version_id
|
| 438 |
+
renderChapterEditor(selectedChapter)
|
| 439 |
+
message('已回滚,当前版本已切换;原版本仍保留')
|
| 440 |
} catch (e) { message(e.message, true) }
|
| 441 |
}
|
| 442 |
+
|
| 443 |
+
async function checkChapter() {
|
| 444 |
try {
|
| 445 |
+
var result = await api('/projects/' + encodeURIComponent(projectId()) + '/chapters/' + encodeURIComponent(selectedChapter.chapter_id) + '/check', { method: 'POST', body: '{}' })
|
| 446 |
+
var box = document.getElementById('sp-check-result')
|
| 447 |
+
if (!box) return
|
| 448 |
+
var cls = result.status === 'pass' ? 'pass' : result.status === 'error' ? 'error' : 'warning'
|
| 449 |
+
box.innerHTML = '<div class="sp-check ' + cls + '"><b>' + (result.status === 'pass' ? '基础检查通过' : result.status === 'error' ? '发现必须处理的问题' : '发现待复核项') + '</b>' + (result.issues && result.issues.length ? '<ul>' + result.issues.map(function (x) { return '<li>' + esc(x.message) + '</li>' }).join('') + '</ul>' : '<p>当前规则没有发现明显缺项。</p>') + '</div>'
|
| 450 |
} catch (e) { message(e.message, true) }
|
| 451 |
}
|
| 452 |
+
|
| 453 |
function renderForHash() {
|
| 454 |
var hash = location.hash.slice(1)
|
| 455 |
var isProject = hash === 'projects' || hash.indexOf('projects/') === 0
|
| 456 |
var projectPanel = document.getElementById('surf-project-panel')
|
| 457 |
if (!isProject) {
|
| 458 |
+
if (projectPanel) { projectPanel.style.display = 'none'; document.body.classList.remove('surf-project-open') }
|
| 459 |
return
|
| 460 |
}
|
| 461 |
+
if (projectPanel) { projectPanel.style.display = 'block'; document.body.classList.add('surf-project-open') }
|
| 462 |
if (hash === 'projects') renderProjectHome()
|
| 463 |
+
else {
|
| 464 |
+
var id
|
| 465 |
+
try { id = decodeURIComponent(hash.slice(9)) } catch (_) { id = '' }
|
| 466 |
+
if (!id) return renderProjectHome()
|
| 467 |
+
if (!selectedProject || projectId() !== id) openProject(id)
|
| 468 |
+
}
|
| 469 |
}
|
| 470 |
+
|
| 471 |
function boot() {
|
| 472 |
+
var observer = new MutationObserver(function () {
|
| 473 |
+
if (!document.getElementById('surf-start-writing') || !document.getElementById('surf-project-list')) ensureSidebar()
|
| 474 |
+
})
|
| 475 |
observer.observe(document.body, { childList: true, subtree: true })
|
| 476 |
ensureSidebar()
|
| 477 |
+
loadProjects(false)
|
| 478 |
window.addEventListener('hashchange', renderForHash)
|
| 479 |
window.addEventListener('popstate', renderForHash)
|
| 480 |
window.addEventListener('fq-need-password', function () { ensureSidebar() })
|
| 481 |
+
refreshTimer = window.setInterval(function () { if (!document.hidden) loadProjects(false) }, 30000)
|
| 482 |
renderForHash()
|
| 483 |
}
|
| 484 |
+
|
| 485 |
var css = document.createElement('style')
|
| 486 |
+
css.textContent = [
|
| 487 |
+
'.surf-project-panel{display:none;position:fixed;z-index:40;inset:0;overflow:auto;background:hsl(var(--background));color:hsl(var(--foreground));}',
|
| 488 |
+
'body.surf-project-open{overflow:hidden}',
|
| 489 |
+
'.sp-shell{max-width:1360px;margin:0 auto;padding:28px clamp(16px,4vw,48px) 56px;min-height:100vh}',
|
| 490 |
+
'.sp-header{display:flex;align-items:flex-start;justify-content:space-between;gap:20px;margin-bottom:22px;padding-bottom:20px;border-bottom:1px solid hsl(var(--border))}',
|
| 491 |
+
'.sp-header h1{margin:4px 0 4px;font-size:clamp(24px,4vw,34px);font-weight:900;letter-spacing:-.03em}',
|
| 492 |
+
'.sp-header p,.sp-section-head p,.sp-card>p{margin:0;color:hsl(var(--muted-foreground));font-size:13px;line-height:1.7}',
|
| 493 |
+
'.sp-kicker,.sp-eyebrow{font-size:11px;font-weight:800;letter-spacing:.12em;text-transform:uppercase;color:#ff6b4a}',
|
| 494 |
+
'.sp-header-actions,.sp-actions{display:flex;align-items:center;gap:8px;flex-wrap:wrap}',
|
| 495 |
+
'.sp-btn{border:1px solid hsl(var(--border));border-radius:9px;background:hsl(var(--card));color:inherit;padding:9px 13px;font-size:13px;font-weight:750;cursor:pointer;transition:.18s}',
|
| 496 |
+
'.sp-btn:hover{border-color:#ff6b4a;transform:translateY(-1px)}.sp-btn:disabled{opacity:.55;cursor:wait;transform:none}',
|
| 497 |
+
'.sp-btn.primary{border-color:#ff6b4a;background:#ff6b4a;color:#25100a}.sp-btn.ghost{background:transparent;color:hsl(var(--muted-foreground))}.sp-btn.small{padding:7px 10px;font-size:12px}.sp-btn.tiny{padding:5px 7px;font-size:11px}.sp-btn.danger{color:#ff9d9d;border-color:#713b43;background:transparent}',
|
| 498 |
+
'.sp-card{border:1px solid hsl(var(--border));border-radius:16px;background:hsl(var(--card)/.55);padding:20px;box-shadow:0 8px 30px rgba(0,0,0,.08)}',
|
| 499 |
+
'.sp-home-grid,.sp-editor-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:16px;margin-bottom:16px}.sp-hero{background:linear-gradient(135deg,rgba(255,107,74,.16),hsl(var(--card)/.55) 55%)}.sp-hero h2{max-width:560px;margin:7px 0 8px;font-size:clamp(24px,4vw,38px);line-height:1.12;letter-spacing:-.04em}.sp-hero p{max-width:680px;margin:0;color:hsl(var(--muted-foreground));line-height:1.8}',
|
| 500 |
+
'.sp-section-head{display:flex;align-items:flex-start;justify-content:space-between;gap:12px;margin-bottom:15px}.sp-section-head h2,.sp-section-head h3{margin:0;font-size:18px;font-weight:850}.sp-card label{display:block;margin-top:12px;color:hsl(var(--muted-foreground));font-size:12px;font-weight:700}.sp-card label:first-of-type{margin-top:0}',
|
| 501 |
+
'.sp-form-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:11px}.sp-input,.sp-textarea{display:block;width:100%;margin-top:6px;border:1px solid hsl(var(--border));border-radius:9px;background:hsl(var(--background));color:inherit;padding:10px 11px;font:inherit;font-size:13px;outline:none}.sp-input:focus,.sp-textarea:focus{border-color:#ff6b4a;box-shadow:0 0 0 3px rgba(255,107,74,.12)}.sp-textarea{min-height:102px;resize:vertical;line-height:1.65}.sp-textarea.short{min-height:78px}.sp-json{min-height:170px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px}.sp-body{min-height:360px;font-size:15px;line-height:1.9}',
|
| 502 |
+
'.sp-list-card{margin-bottom:16px}.sp-project-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:11px}.sp-project-card{display:block;text-align:left;border:1px solid hsl(var(--border));border-radius:12px;background:hsl(var(--background)/.5);color:inherit;padding:14px;cursor:pointer;min-height:140px;transition:.18s}.sp-project-card:hover{border-color:#ff6b4a;transform:translateY(-2px)}.sp-card-title{font-weight:800}.sp-card-meta,.sp-card-foot{margin-top:6px;color:hsl(var(--muted-foreground));font-size:11px}.sp-card-desc{margin-top:13px;color:hsl(var(--muted-foreground));font-size:12px;line-height:1.6;display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}.sp-card-foot{margin-top:14px}',
|
| 503 |
+
'.sp-steps{display:grid;gap:10px;margin:14px 0 0;padding:0;list-style:none}.sp-steps li{display:grid;gap:2px;padding:10px 12px;border-left:3px solid #ff6b4a;background:hsl(var(--background)/.45);border-radius:0 8px 8px 0}.sp-steps span{font-size:12px;color:hsl(var(--muted-foreground))}.sp-empty,.sp-placeholder{padding:18px;border:1px dashed hsl(var(--border));border-radius:10px;color:hsl(var(--muted-foreground));font-size:13px}',
|
| 504 |
+
'.sp-stats{display:grid;grid-template-columns:repeat(4,minmax(0,1fr));gap:10px;margin-bottom:16px}.sp-stats>div{border:1px solid hsl(var(--border));border-radius:11px;padding:12px 14px;background:hsl(var(--card)/.4)}.sp-stats b{display:block;font-size:18px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.sp-stats span{display:block;margin-top:3px;color:hsl(var(--muted-foreground));font-size:11px}',
|
| 505 |
+
'.sp-request{margin-top:18px;padding-top:16px;border-top:1px solid hsl(var(--border))}.sp-request>div{display:flex;justify-content:space-between;gap:10px;align-items:baseline}.sp-request span,.sp-version-head p,.sp-chapter-head p{color:hsl(var(--muted-foreground));font-size:12px}.sp-chapter-layout{display:grid;grid-template-columns:260px minmax(0,1fr);gap:16px}.sp-chapter-list{display:grid;align-content:start;gap:7px}.sp-chapter{display:block;width:100%;border:1px solid hsl(var(--border));border-radius:10px;background:transparent;color:inherit;padding:10px 11px;text-align:left;cursor:pointer}.sp-chapter:hover,.sp-chapter.active{border-color:#ff6b4a;background:rgba(255,107,74,.1)}.sp-chapter b,.sp-chapter span,.sp-chapter small{display:block}.sp-chapter span{margin-top:3px;font-size:12px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}.sp-chapter small{margin-top:5px;color:hsl(var(--muted-foreground));font-size:10px}',
|
| 506 |
+
'.sp-chapter-head,.sp-version-head{display:flex;align-items:flex-start;justify-content:space-between;gap:12px}.sp-chapter-head h3,.sp-version-head h3{margin:0;font-size:16px}.sp-chapter-head p{margin:4px 0 0}.sp-chapter-editor>label{margin-top:12px}.sp-divider{height:1px;margin:22px 0 17px;background:hsl(var(--border))}.sp-version-list{display:grid;gap:6px;margin-top:17px}.sp-version-row{display:flex;align-items:stretch;gap:6px}.sp-version{display:block;flex:1;text-align:left;border:1px solid hsl(var(--border));border-radius:8px;background:transparent;color:hsl(var(--muted-foreground));padding:8px 10px;cursor:pointer}.sp-version:hover,.sp-version.active{border-color:#ff6b4a;color:inherit}.sp-version b,.sp-version span,.sp-version small{display:block}.sp-version span{margin-top:3px;font-size:11px}.sp-version small{margin-top:3px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;font-size:10px;color:hsl(var(--muted-foreground))}.sp-materials{display:grid;gap:8px}.sp-material{padding:11px;border:1px solid hsl(var(--border));border-radius:10px;background:hsl(var(--background)/.35)}.sp-material-head{display:flex;justify-content:space-between;gap:8px}.sp-material-head b{font-size:13px}.sp-material-head span{font:10px ui-monospace,monospace;color:hsl(var(--muted-foreground))}.sp-material p{margin:7px 0 0;color:hsl(var(--muted-foreground));font-size:12px;line-height:1.7;white-space:pre-wrap}.sp-check{margin-top:14px;padding:12px;border:1px solid hsl(var(--border));border-radius:9px;font-size:12px}.sp-check.pass{border-color:#347c61;background:rgba(52,124,97,.12)}.sp-check.warning{border-color:#9b7b36;background:rgba(155,123,54,.12)}.sp-check.error{border-color:#8b4545;background:rgba(139,69,69,.12)}.sp-check ul{margin:7px 0 0;padding-left:18px}.sp-check p{margin:6px 0 0;color:hsl(var(--muted-foreground))}.sp-message{min-height:22px;margin-top:12px;font-size:12px}.sp-message.ok,.sp-inline-status{color:#5ed39a}.sp-message.error{color:#ff8585}.sp-danger{display:flex;align-items:center;gap:10px;margin-top:17px;color:#ff8585;font-size:11px}',
|
| 507 |
+
'@media(max-width:900px){.sp-home-grid,.sp-editor-grid{grid-template-columns:1fr}.sp-project-grid{grid-template-columns:repeat(2,minmax(0,1fr))}.sp-chapter-layout{grid-template-columns:1fr}.sp-chapter-list{display:flex;overflow:auto}.sp-chapter{min-width:190px}.sp-header{flex-direction:column}.sp-stats{grid-template-columns:repeat(2,minmax(0,1fr))}}',
|
| 508 |
+
'@media(max-width:560px){.sp-shell{padding:18px 12px 40px}.sp-card{padding:15px;border-radius:13px}.sp-project-grid,.sp-form-grid{grid-template-columns:1fr}.sp-header-actions{width:100%}.sp-header-actions .sp-btn{flex:1}.sp-stats b{font-size:15px}.sp-danger{align-items:flex-start;flex-direction:column}}'
|
| 509 |
+
].join('')
|
| 510 |
document.head.appendChild(css)
|
| 511 |
+
|
| 512 |
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot)
|
| 513 |
else boot()
|
| 514 |
})()
|
fanqie/projects.js
CHANGED
|
@@ -315,6 +315,15 @@ async function updateChapter(projectId, chapterId, patch) {
|
|
| 315 |
const allowed = ['number', 'volume', 'title', 'goal', 'outline', 'scenes', 'previous_summary', 'status']
|
| 316 |
const changes = {}
|
| 317 |
for (const key of allowed) if (patch && patch[key] !== undefined) changes[key] = patch[key]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 318 |
const next = { ...chapter, ...changes, updated_at: now(), revision: Number(chapter.revision || 0) + 1 }
|
| 319 |
await writeJson(projectKey(projectId, `chapters/${chapterId}/chapter.json`), next)
|
| 320 |
const list = await readJson(projectKey(projectId, 'chapters/index.json'), { chapters: [] })
|
|
|
|
| 315 |
const allowed = ['number', 'volume', 'title', 'goal', 'outline', 'scenes', 'previous_summary', 'status']
|
| 316 |
const changes = {}
|
| 317 |
for (const key of allowed) if (patch && patch[key] !== undefined) changes[key] = patch[key]
|
| 318 |
+
if (changes.number !== undefined) {
|
| 319 |
+
const number = Number(changes.number)
|
| 320 |
+
if (!Number.isFinite(number) || number < 1) throw new HttpError(400, '章节号必须是正整数')
|
| 321 |
+
changes.number = Math.floor(number)
|
| 322 |
+
const index = await readJson(projectKey(projectId, 'chapters/index.json'), { chapters: [] })
|
| 323 |
+
if ((index.chapters || []).some(item => item.chapter_id !== chapterId && Number(item.number) === changes.number)) {
|
| 324 |
+
throw new HttpError(409, `第 ${changes.number} 章已经存在`)
|
| 325 |
+
}
|
| 326 |
+
}
|
| 327 |
const next = { ...chapter, ...changes, updated_at: now(), revision: Number(chapter.revision || 0) + 1 }
|
| 328 |
await writeJson(projectKey(projectId, `chapters/${chapterId}/chapter.json`), next)
|
| 329 |
const list = await readJson(projectKey(projectId, 'chapters/index.json'), { chapters: [] })
|