import fs from 'fs';
const filePath = 'C:/Users/12914/Desktop/sky/frontend/src/pages/editor/editor.vue';
let content = fs.readFileSync(filePath, 'utf8');
// 1. 模板片段
const previewTemplate = `
‹
返回编辑
美篇效果预览
存草稿
发布
🎵
{{ previewMusicPlaying ? '播放中' : '背景音乐' }}
{{ selectedMusic ? selectedMusic.name : '美篇背景音乐' }}
{{ previewMusicPlaying ? '❚❚' : '▶' }}
{{ doc.article.title || '无标题' }}
美
创作者
{{ previewDateText }}
美篇排版
暂无段落内容,返回编辑后添加图片或文字
❖ 美篇精选 · 匠心排版 ❖
✓
{{ item.name }}
✕
不使用背景音乐
当前应用
{{ playingMusicId === m.id ? '⏸' : '▶' }}
{{ m.name }}
{{ m.artist }} · {{ m.category || '精选' }}
当前应用
使用
正文字号
标准 (15px)
舒适 (17px)
大号 (19px)
段落行距
正常 (1.8倍)
宽松 (2.2倍)
对齐方式
左对齐
两端对齐
居中对齐
正文字体
{{ f.name }}
🎨
模板
🎵
音乐
🔤
排版
标题字号
标准 (22px)
大号 (26px)
超大 (32px)
对齐方式
居左
居中
字体加粗
常规
加粗
`;
// 插入模板
const tplEndPos = content.indexOf('');
if (tplEndPos !== -1) {
const lastViewPos = content.lastIndexOf('', tplEndPos);
if (lastViewPos !== -1) {
content = content.substring(0, lastViewPos) + previewTemplate + '\n \r\n \r\n' + content.substring(tplEndPos + ''.length);
} else {
console.error('Could not find last view before template end');
process.exit(1);
}
} else {
console.error('Could not find template end anchor');
process.exit(1);
}
// 2. 注入 Script
const scriptLogic = `
// ════════════════════════════════════════════════════════════════════════════════
// 美篇式沉浸全真预览与装配状态机
// ════════════════════════════════════════════════════════════════════════════════
const showMeipianPreview = ref(false);
const activePreviewDrawer = ref(''); // 'template' | 'music' | 'layout' | ''
const previewMusicPlaying = ref(false);
const previewFontSize = ref('md');
const previewLineHeight = ref('normal');
const previewTextAlign = ref('left');
let previewAudioCtx = null;
const previewDateText = computed(() => {
const d = new Date();
return \`\${d.getFullYear()}-\${String(d.getMonth() + 1).padStart(2, '0')}-\${String(d.getDate()).padStart(2, '0')} \${String(d.getHours()).padStart(2, '0')}:\${String(d.getMinutes()).padStart(2, '0')}\`;
});
const previewSectionStyle = computed(() => {
const fsMap = { sm: '30rpx', md: '34rpx', lg: '38rpx' };
const lhMap = { normal: '1.8', loose: '2.2' };
return {
fontSize: fsMap[previewFontSize.value] || '34rpx',
lineHeight: lhMap[previewLineHeight.value] || '1.8',
textAlign: previewTextAlign.value,
};
});
// 标题样式状态
const showTitleStyleModal = ref(false);
const titleStyleConfig = reactive({
size: 'lg',
align: 'left',
is_bold: true,
});
const titleStyleFontSize = computed(() => {
if (titleStyleConfig.size === 'sm') return '44rpx';
if (titleStyleConfig.size === 'md') return '52rpx';
return '60rpx';
});
function openTitleStyleModal() {
showTitleStyleModal.value = true;
}
function closeTitleStyleModal() {
showTitleStyleModal.value = false;
}
async function openMeipianPreview() {
showMeipianPreview.value = true;
activePreviewDrawer.value = '';
if (!backdropList.value.length || backdropList.value.length <= 1) {
try {
const res = await listBackdrops();
if (Array.isArray(res?.data) && res.data.length > 0) {
backdropList.value = res.data;
}
} catch (e) {}
}
if (!musicList.value.length) {
try {
const res = await listMusic();
if (Array.isArray(res?.data) && res.data.length > 0) {
musicList.value = res.data;
}
} catch (e) {}
}
if (!fontList.value.length) {
try {
const res = await listFonts();
if (Array.isArray(res?.data) && res.data.length > 0) {
fontList.value = res.data;
}
} catch (e) {}
}
}
function closeMeipianPreview() {
showMeipianPreview.value = false;
activePreviewDrawer.value = '';
if (previewAudioCtx) {
previewAudioCtx.stop();
previewAudioCtx.destroy();
previewAudioCtx = null;
}
previewMusicPlaying.value = false;
}
function togglePreviewDrawer(type) {
if (activePreviewDrawer.value === type) {
activePreviewDrawer.value = '';
} else {
activePreviewDrawer.value = type;
}
}
function applyTemplateInPreview(item) {
selectBackdrop(item);
}
function selectMusicInPreview(music) {
doc.article.music_id = music.id;
doc.dirty.value = true;
autosave.notifyChange();
if (previewMusicPlaying.value) {
playBgMusic(music.url);
}
uni.showToast({ title: \`已设置BGM: \${music.name}\`, icon: 'none' });
}
function clearMusicInPreview() {
doc.article.music_id = '';
doc.dirty.value = true;
autosave.notifyChange();
if (previewAudioCtx) {
previewAudioCtx.stop();
}
previewMusicPlaying.value = false;
uni.showToast({ title: '已取消背景音乐', icon: 'none' });
}
function selectFontInPreview(font) {
doc.article.font_id = font.id;
doc.dirty.value = true;
autosave.notifyChange();
uni.showToast({ title: \`已应用字体: \${font.name}\`, icon: 'none' });
}
function playBgMusic(url) {
if (!previewAudioCtx) {
previewAudioCtx = uni.createInnerAudioContext();
previewAudioCtx.loop = true;
previewAudioCtx.onEnded(() => {
previewMusicPlaying.value = false;
});
previewAudioCtx.onError(() => {
previewMusicPlaying.value = false;
});
}
previewAudioCtx.src = url;
previewAudioCtx.play();
previewMusicPlaying.value = true;
}
function togglePreviewBgMusic() {
if (previewMusicPlaying.value) {
if (previewAudioCtx) {
previewAudioCtx.pause();
}
previewMusicPlaying.value = false;
} else {
const m = selectedMusic.value || (musicList.value && musicList.value[0]);
if (m && m.url) {
playBgMusic(m.url);
} else {
uni.showToast({ title: '请先在底部选择一首背景音乐', icon: 'none' });
}
}
}
function saveDraftInPreview() {
doc.article.status = 'draft';
handleSave(false, true);
uni.showToast({ title: '草稿已保存', icon: 'success' });
}
function publishInPreview() {
closeMeipianPreview();
openPublishModal();
}
`;
const scriptEndIndex = content.lastIndexOf('');
if (scriptEndIndex !== -1) {
content = content.substring(0, scriptEndIndex) + scriptLogic + '\n' + content.substring(scriptEndIndex + ''.length);
} else {
console.error('Could not find script end anchor');
process.exit(1);
}
// 3. 注入 Style 样式
const styleSnippet = `
/* ═══════════ 美篇式全真沉浸预览 & 底部模板/音乐/排版装配样式 ═══════════ */
.mp-preview-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 9999;
background-color: #f8fafc;
display: flex;
flex-direction: column;
}
.mp-preview-nav {
height: 96rpx;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(16rpx);
border-bottom: 1rpx solid #e2e8f0;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 24rpx;
box-sizing: border-box;
}
.mp-nav-back {
display: flex;
align-items: center;
gap: 8rpx;
color: #334155;
font-size: 28rpx;
cursor: pointer;
}
.mp-back-arrow {
font-size: 40rpx;
line-height: 1;
}
.mp-nav-title {
font-size: 32rpx;
font-weight: 600;
color: #0f172a;
}
.mp-nav-actions {
display: flex;
align-items: center;
gap: 16rpx;
}
.mp-btn-draft {
padding: 10rpx 22rpx;
background: #f1f5f9;
border-radius: 30rpx;
color: #475569;
font-size: 26rpx;
font-weight: 500;
}
.mp-btn-publish {
padding: 10rpx 28rpx;
background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
border-radius: 30rpx;
color: #ffffff;
font-size: 26rpx;
font-weight: 600;
box-shadow: 0 4rpx 12rpx rgba(37, 99, 235, 0.25);
}
.mp-preview-scroll {
flex: 1;
width: 100%;
overflow-y: auto;
box-sizing: border-box;
padding-bottom: 240rpx;
}
.mp-header-decor {
width: 100%;
line-height: 0;
}
.mp-decor-img {
width: 100%;
display: block;
}
.mp-floating-music-capsule {
position: sticky;
top: 24rpx;
float: right;
margin-right: 24rpx;
margin-top: 20rpx;
z-index: 100;
background: rgba(255, 255, 255, 0.92);
backdrop-filter: blur(12rpx);
border: 1rpx solid rgba(226, 232, 240, 0.8);
border-radius: 40rpx;
padding: 8rpx 20rpx 8rpx 12rpx;
display: flex;
align-items: center;
gap: 12rpx;
box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.08);
}
.mp-vinyl-disc {
width: 52rpx;
height: 52rpx;
border-radius: 50%;
background: #1e293b;
display: flex;
align-items: center;
justify-content: center;
box-shadow: inset 0 0 0 4rpx #334155;
}
.mp-vinyl-icon {
font-size: 24rpx;
}
.mp-vinyl-disc.spin {
animation: mp-spin 4s linear infinite;
}
@keyframes mp-spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.mp-music-track {
display: flex;
flex-direction: column;
}
.mp-music-label {
font-size: 18rpx;
color: #64748b;
}
.mp-music-title {
font-size: 22rpx;
font-weight: 500;
color: #0f172a;
max-width: 180rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.mp-music-state-icon {
font-size: 22rpx;
color: #2563eb;
}
.mp-preview-card {
max-width: 750rpx;
margin: 20rpx auto 0;
background: #ffffff;
border-radius: 20rpx;
border: 1rpx solid #e2e8f0;
padding: 32rpx 28rpx;
box-sizing: border-box;
box-shadow: 0 8rpx 30rpx rgba(0, 0, 0, 0.04);
}
.mp-preview-cover-wrap {
width: 100%;
border-radius: 16rpx;
overflow: hidden;
margin-bottom: 28rpx;
line-height: 0;
}
.mp-preview-cover {
width: 100%;
height: 380rpx;
display: block;
}
.mp-preview-title-wrap {
margin-bottom: 24rpx;
}
.mp-preview-title-wrap.title-center {
text-align: center;
}
.mp-preview-title-wrap.title-left {
text-align: left;
}
.mp-preview-title {
color: #0f172a;
line-height: 1.4;
word-break: break-all;
}
.mp-preview-title-wrap.title-bold .mp-preview-title {
font-weight: 700;
}
.mp-preview-author-row {
display: flex;
align-items: center;
gap: 16rpx;
padding-bottom: 28rpx;
margin-bottom: 28rpx;
border-bottom: 1rpx solid #f1f5f9;
}
.mp-author-avatar-box {
width: 68rpx;
height: 68rpx;
border-radius: 50%;
background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
display: flex;
align-items: center;
justify-content: center;
}
.mp-author-avatar-text {
color: #ffffff;
font-size: 30rpx;
font-weight: bold;
}
.mp-author-info {
flex: 1;
display: flex;
flex-direction: column;
}
.mp-author-name {
font-size: 28rpx;
font-weight: 500;
color: #1e293b;
}
.mp-article-time {
font-size: 22rpx;
color: #94a3b8;
}
.mp-author-badge {
font-size: 22rpx;
padding: 6rpx 16rpx;
background: #eff6ff;
color: #2563eb;
border-radius: 20rpx;
}
.mp-preview-sections {
display: flex;
flex-direction: column;
gap: 24rpx;
}
.mp-preview-empty {
padding: 60rpx 0;
text-align: center;
}
.mp-empty-hint {
font-size: 28rpx;
color: #94a3b8;
}
.mp-preview-seal-wrap {
margin-top: 60rpx;
padding-top: 40rpx;
display: flex;
align-items: center;
justify-content: center;
gap: 20rpx;
}
.mp-seal-line {
flex: 1;
height: 1rpx;
background: #e2e8f0;
}
.mp-seal-badge {
padding: 6rpx 20rpx;
background: #f8fafc;
border: 1rpx dashed #cbd5e1;
border-radius: 30rpx;
}
.mp-seal-text {
font-size: 22rpx;
color: #64748b;
}
.mp-preview-safe-space {
height: 200rpx;
}
/* 底部美篇三联固定操作托盘 */
.mp-bottom-dock {
position: fixed;
bottom: 0;
left: 0;
right: 0;
z-index: 10000;
background: rgba(255, 255, 255, 0.98);
backdrop-filter: blur(20rpx);
border-top: 1rpx solid #e2e8f0;
box-shadow: 0 -8rpx 30rpx rgba(0, 0, 0, 0.08);
}
.mp-dock-tabs {
height: 110rpx;
display: flex;
align-items: center;
justify-content: space-around;
padding: 0 40rpx;
}
.mp-dock-tab {
display: flex;
flex-direction: column;
align-items: center;
gap: 6rpx;
position: relative;
padding: 8rpx 24rpx;
cursor: pointer;
}
.mp-dock-tab.active {
background: #eff6ff;
border-radius: 16rpx;
}
.mp-tab-icon {
font-size: 38rpx;
}
.mp-tab-label {
font-size: 22rpx;
color: #475569;
font-weight: 500;
}
.mp-dock-tab.active .mp-tab-label {
color: #2563eb;
font-weight: 600;
}
.mp-tab-dot {
position: absolute;
top: 6rpx;
right: 18rpx;
width: 12rpx;
height: 12rpx;
border-radius: 50%;
background: #2563eb;
}
.mp-drawer-content {
background: #ffffff;
border-bottom: 1rpx solid #f1f5f9;
padding: 24rpx;
max-height: 480rpx;
}
.mp-drawer-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20rpx;
}
.mp-drawer-title {
font-size: 28rpx;
font-weight: 600;
color: #0f172a;
}
.mp-drawer-close {
font-size: 28rpx;
color: #94a3b8;
padding: 8rpx 12rpx;
}
.mp-template-scroll {
width: 100%;
white-space: nowrap;
}
.mp-template-cards {
display: inline-flex;
gap: 20rpx;
padding: 10rpx 4rpx;
}
.mp-template-card {
width: 160rpx;
display: flex;
flex-direction: column;
align-items: center;
gap: 12rpx;
cursor: pointer;
}
.mp-tpl-swatch {
width: 150rpx;
height: 100rpx;
border-radius: 12rpx;
border: 2rpx solid #e2e8f0;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 4rpx 10rpx rgba(0, 0, 0, 0.05);
}
.mp-template-card.selected .mp-tpl-swatch {
border-color: #2563eb;
box-shadow: 0 0 0 4rpx rgba(37, 99, 235, 0.2);
}
.mp-tpl-check {
font-size: 32rpx;
color: #2563eb;
font-weight: bold;
}
.mp-tpl-name {
font-size: 24rpx;
color: #334155;
white-space: nowrap;
}
.mp-template-card.selected .mp-tpl-name {
color: #2563eb;
font-weight: 600;
}
.mp-music-scroll {
max-height: 380rpx;
}
.mp-music-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 18rpx 20rpx;
border-radius: 14rpx;
margin-bottom: 10rpx;
background: #f8fafc;
}
.mp-music-item.selected {
background: #eff6ff;
border: 1rpx solid #bfdbfe;
}
.mp-music-left {
display: flex;
align-items: center;
gap: 16rpx;
}
.mp-music-play-btn {
width: 50rpx;
height: 50rpx;
border-radius: 50%;
background: #e2e8f0;
display: flex;
align-items: center;
justify-content: center;
font-size: 20rpx;
color: #1e293b;
}
.mp-music-texts {
display: flex;
flex-direction: column;
}
.mp-music-name {
font-size: 26rpx;
font-weight: 500;
color: #0f172a;
}
.mp-music-artist {
font-size: 22rpx;
color: #64748b;
}
.mp-item-selected-tag {
font-size: 22rpx;
color: #2563eb;
font-weight: 600;
padding: 6rpx 16rpx;
background: #dbeafe;
border-radius: 20rpx;
}
.mp-item-pick-btn {
font-size: 24rpx;
color: #475569;
padding: 6rpx 20rpx;
border: 1rpx solid #cbd5e1;
border-radius: 20rpx;
}
.mp-layout-controls {
display: flex;
flex-direction: column;
gap: 20rpx;
}
.mp-control-group {
display: flex;
align-items: center;
justify-content: space-between;
}
.mp-group-label {
font-size: 26rpx;
color: #475569;
font-weight: 500;
}
.mp-pill-selector {
display: flex;
gap: 12rpx;
}
.mp-pill-opt {
padding: 10rpx 24rpx;
background: #f1f5f9;
border-radius: 24rpx;
font-size: 24rpx;
color: #475569;
cursor: pointer;
}
.mp-pill-opt.active {
background: #2563eb;
color: #ffffff;
font-weight: 600;
}
.mp-font-pill-scroll {
max-width: 460rpx;
white-space: nowrap;
}
.mp-font-pills {
display: inline-flex;
gap: 12rpx;
}
.mp-font-pill {
padding: 8rpx 20rpx;
background: #f1f5f9;
border-radius: 24rpx;
font-size: 22rpx;
color: #475569;
white-space: nowrap;
}
.mp-font-pill.active {
background: #2563eb;
color: #ffffff;
}
/* 标题样式设置 */
.title-input-row {
display: flex;
align-items: flex-start;
gap: 16rpx;
}
.title-style-btn {
display: flex;
align-items: center;
gap: 6rpx;
padding: 10rpx 18rpx;
background: #f1f5f9;
border-radius: 24rpx;
color: #475569;
font-size: 24rpx;
font-weight: 500;
white-space: nowrap;
margin-top: 10rpx;
}
.title-style-icon {
font-size: 26rpx;
}
.title-align-center {
text-align: center;
}
.title-align-left {
text-align: left;
}
.title-weight-bold {
font-weight: 700;
}
.nav-btn-preview {
display: flex;
align-items: center;
gap: 6rpx;
padding: 10rpx 20rpx;
background: #eff6ff;
border-radius: 24rpx;
margin-right: 12rpx;
}
.preview-btn-icon {
font-size: 26rpx;
}
.preview-btn-text {
font-size: 24rpx;
color: #2563eb;
font-weight: 600;
}
`;
const styleEndIndex = content.lastIndexOf('');
if (styleEndIndex !== -1) {
content = content.substring(0, styleEndIndex) + styleSnippet + '\n' + content.substring(styleEndIndex + ''.length);
} else {
console.error('Could not find style end anchor');
process.exit(1);
}
fs.writeFileSync(filePath, content, 'utf8');
console.log('Patch applied successfully!');