From f5746da0aec39dedba62c7ca2e72ec645de489e9 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 25 Aug 2026 19:42:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/css/frontend/pages/submit.css | 4 ++ assets/js/front-submit.js | 54 +++++++++++++++++++ templates/parts/submit/editor-main.php | 10 +++- .../front-submit-editor-contract.test.cjs | 16 +++++- 4 files changed, 81 insertions(+), 3 deletions(-) diff --git a/assets/css/frontend/pages/submit.css b/assets/css/frontend/pages/submit.css index fd816f49..9a7d4df1 100644 --- a/assets/css/frontend/pages/submit.css +++ b/assets/css/frontend/pages/submit.css @@ -71,6 +71,10 @@ border: none; } +.aksrt-submit-editor-wrap textarea.wp-editor-area { + opacity: 1; +} + .aksrt-submit-editor-wrap .mce-toolbar-grp { background: var(--surface-1, #fff); border-bottom: 1px solid var(--color-line, #eceff4); diff --git a/assets/js/front-submit.js b/assets/js/front-submit.js index ae62eeed..44678fca 100644 --- a/assets/js/front-submit.js +++ b/assets/js/front-submit.js @@ -100,6 +100,7 @@ if (!this.form) { return; } + this.hydrateEditor(); this.bindDirty(); this.bindAutoHeight(); this.bindSubmitButtons(); @@ -113,6 +114,59 @@ this.loadFeaturedFromDom(); }, + hydrateEditor: function () { + var snapshot = document.getElementById('aksrt-initial-post-content'); + var textarea = qs('#post_content') || qs('[name="post_content"]'); + var initialContent = snapshot ? snapshot.value : textarea ? textarea.value : ''; + if (!initialContent) { + return; + } + + var fallbackToTextEditor = function () { + if (window.switchEditors && typeof window.switchEditors.go === 'function') { + window.switchEditors.go('post_content', 'html'); + } + if (textarea) { + textarea.value = initialContent; + textarea.style.opacity = '1'; + textarea.style.display = 'block'; + } + }; + + var attempts = 0; + var restore = function () { + var editor = window.tinymce && window.tinymce.get('post_content'); + if (!editor) { + attempts += 1; + if (attempts < 20) { + setTimeout(restore, 250); + } else { + fallbackToTextEditor(); + } + return; + } + + var hydrate = function () { + var current = editor.getContent({ format: 'raw' }); + if (!current || !current.trim()) { + editor.setContent(initialContent); + editor.save(); + } + if (!editor.getContent({ format: 'raw' }).trim()) { + fallbackToTextEditor(); + } + }; + + if (editor.initialized) { + hydrate(); + } else { + editor.on('init', hydrate); + } + }; + + restore(); + }, + bindDirty: function () { var self = this; this.form.addEventListener('input', function () { diff --git a/templates/parts/submit/editor-main.php b/templates/parts/submit/editor-main.php index 4fcd347d..a3a170df 100644 --- a/templates/parts/submit/editor-main.php +++ b/templates/parts/submit/editor-main.php @@ -42,6 +42,12 @@ $is_can_new = !empty($ctx['is_can_new']);
+ + wp_is_mobile() ? 460 : 470, 'media_buttons' => false, 'default_editor' => 'tinymce', - 'quicktags' => false, + 'quicktags' => true, 'teeny' => false, 'tinymce' => [ 'toolbar1' => $toolbar1, @@ -59,7 +65,7 @@ $is_can_new = !empty($ctx['is_can_new']); 'placeholder' => (string) ($settings['content_placeholder'] ?? ''), ], ]; - wp_editor((string) $ctx['current_content'], 'post_content', $editor_settings); + wp_editor($initial_content, 'post_content', $editor_settings); ?>
diff --git a/tests/widget-system/front-submit-editor-contract.test.cjs b/tests/widget-system/front-submit-editor-contract.test.cjs index bebe9a67..3da60f4c 100644 --- a/tests/widget-system/front-submit-editor-contract.test.cjs +++ b/tests/widget-system/front-submit-editor-contract.test.cjs @@ -21,5 +21,19 @@ test('published post content reaches the front submit editor unchanged', () => { const editor = read('templates/parts/submit/editor-main.php'); assert.match(context, /\$current_content\s*=\s*\$edit_post\s*\?\s*\$edit_post->post_content\s*:\s*'';/); - assert.match(editor, /wp_editor\(\(string\) \$ctx\['current_content'\], 'post_content', \$editor_settings\);/); + assert.match(editor, /\$initial_content\s*=\s*\(string\) \$ctx\['current_content'\]/); + assert.match(editor, /wp_editor\(\$initial_content, 'post_content', \$editor_settings\);/); +}); + +test('front submit restores published content when TinyMCE initializes empty', () => { + const editor = read('templates/parts/submit/editor-main.php'); + const runtime = read('assets/js/front-submit.js'); + const styles = read('assets/css/frontend/pages/submit.css'); + + assert.match(editor, /'quicktags'\s*=>\s*true/); + assert.match(editor, /id="aksrt-initial-post-content"/); + assert.match(runtime, /hydrateEditor:\s*function/); + assert.match(runtime, /editor\.setContent\(initialContent\)/); + assert.match(runtime, /switchEditors\.go\('post_content', 'html'\)/); + assert.match(styles, /textarea\.wp-editor-area\s*\{[^}]*opacity:\s*1/s); });