zhuyuan-ice-gl-zy001/~/Projects/guanghulab-miniapp/pages/editor/editor.js

60 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 编辑页:新建/编辑条目(原型:写回本地假数据)
Page({
data: {
id: null,
title: '',
content: '',
tagStr: '',
visibility: 'private',
aiReadable: true
},
onLoad(query) {
if (query.id) {
const n = getApp().globalData.notes.find(x => x.id === query.id)
if (n) {
this.setData({
id: n.id, title: n.title, content: n.content,
tagStr: n.tags.join(','),
visibility: n.visibility, aiReadable: n.aiReadable
})
wx.setNavigationBarTitle({ title: '编辑条目' })
}
}
},
onTitle(e) { this.setData({ title: e.detail.value }) },
onContent(e) { this.setData({ content: e.detail.value }) },
onTags(e) { this.setData({ tagStr: e.detail.value }) },
onVis(e) { this.setData({ visibility: e.detail.value ? 'public' : 'private' }) },
onAI(e) { this.setData({ aiReadable: e.detail.value }) },
onSave() {
if (!this.data.title.trim()) {
wx.showToast({ title: '标题不能为空', icon: 'none' })
return
}
const app = getApp()
const tags = this.data.tagStr.split(/[,,]/).map(t => t.trim()).filter(Boolean)
const now = new Date()
const stamp = `${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`
if (this.data.id) {
const n = app.globalData.notes.find(x => x.id === this.data.id)
Object.assign(n, {
title: this.data.title, content: this.data.content, tags,
visibility: this.data.visibility, aiReadable: this.data.aiReadable, updatedAt: stamp
})
} else {
app.globalData.notes.unshift({
id: 'n' + Date.now(),
title: this.data.title, content: this.data.content, tags,
summary: this.data.content.replace(/[#*\-\n]/g, ' ').slice(0, 40),
visibility: this.data.visibility, aiReadable: this.data.aiReadable,
updatedAt: stamp
})
}
wx.showToast({ title: '已保存(原型)' })
setTimeout(() => wx.navigateBack(), 600)
}
})