Now.js Framework Documentation
RichTextElementFactory
RichTextElementFactory
โหลดเมื่อใช้ — อยู่ใน feature bundle
now.richtext.min.jsไม่ได้อยู่ในnow.core.min.js: markupdata-element="richtext"โหลดให้เอง ส่วนโค้ดที่เรียกRichTextElementFactoryตรง ๆ ให้await Now.require('RichTextElementFactory')ก่อน — ดู Feature bundle
เชื่อม RichTextEditor เข้ากับ ElementManager: <textarea name="detail" data-element="richtext" data-attr="value:detail"></textarea>
| attribute | ความหมาย |
|---|---|
data-rte-profile="basic\|full\|minimal\|comment" |
ชุดเครื่องมือของ editor (ค่าเริ่มต้น basic) |
data-rte-height="400" / data-rte-min-height="200" / data-rte-max-height |
ความสูง (px) |
data-rte-placeholder="..." |
ข้อความ placeholder |
data-rte-sticky="true" |
แถบเครื่องมือติดด้านบน |
data-rte-allow-style="true" |
HTML ที่วางจากข้างนอกคง <style> ไว้ |
data-rte-allow-script="true" |
HTML ที่วางจากข้างนอกคง <script> ไว้ |
data-rte-allow-iframe="false" |
HTML ที่วางจากข้างนอกไม่รับ <iframe> |
data-rte-allow-interactive-tags="button\|select" |
HTML ที่วางจากข้างนอกคง control ของฟอร์มเหล่านี้ (true = ทุกตัว) |
editor ส่งเนื้อหาตามที่ผู้เขียนเขียน
editor ไม่ตัดเนื้อหาเอง server เป็นผู้ตัดสิน ว่าจะเก็บอะไร เนื้อหาที่โหลดเข้ามา (จากฐานข้อมูล หรือพิมพ์ในโหมด source) กลับออกไปจาก
getContent() และตอน submit ฟอร์มครบทุกส่วน: <style>, <script>, <link>, <noscript>, ฟอร์มและปุ่ม, onclick, ลิงก์ javascript:,
comment และ iframe พร้อม attribute ทุกตัว
ระหว่างแก้ไข ไม่มีอะไรในเนื้อหารันหรือโหลดในหน้าแอดมิน:
- แท็กที่จะรัน โหลด หรือ submit ถูกแสดงเป็น placeholder ตรงตำแหน่งเดิม (ป้ายเล็ก ๆ เช่น
script · pagead2.googlesyndication.com,
style,form,button · เล่นส่วน iframe เป็นกรอบขนาดเท่าของจริง) คลิกเลือก ย้าย ตัด-วาง และลบได้เหมือนรูป แก้โค้ดข้างในได้ในโหมด source - attribute ที่จะรัน (
on*, URL แบบjavascript:,autofocus) ถูกพักไว้ ตัว element ยังแก้ไขได้ตามปกติ - CSS จาก
<style>ของเนื้อหาแสดงผลเฉพาะในพื้นที่แก้ไข (รวมกฎใน@media) ไม่ไปแต่งหน้าแอดมิน - แถบล่างของ editor บอกว่ามีอะไรถูกเก็บไว้ เช่น
Embedded: 1 style • 2 scripts
HTML ที่ วางจากข้างนอก (clipboard จากเว็บอื่นหรือ Word, คำตอบของ AI, โค้ดฝังในกล่อง iframe) ยังถูกทำความสะอาด: สคริปต์ handler และ URL แบบสคริปต์
ถูกตัด — ต้องการวางโค้ด ให้วางในโหมด source · การตัด/คัดลอกแล้ววางภายใน editor ของหน้าเดียวกันได้ครบทุกส่วน
โหมด source จัดย่อหน้าให้อ่านง่ายโดยไม่เปลี่ยนเนื้อหา (ไม่เพิ่มหรือลดช่องว่างระหว่างคำ, <pre> <script> <style> และ comment คงตามเดิม) ข้อจำกัดเดียว:
<!DOCTYPE>, <html>, <head>, <body> อยู่ได้เฉพาะตอนบันทึกจากโหมด source โดยตรง เมื่อสลับกลับโหมดปกติแท็กครอบเหล่านี้หายไป (ลูกข้างในยังอยู่)
ฝั่ง server (Kotchasan) เลือกระดับตามคนเขียน:
| ใช้ | เก็บอะไร |
|---|---|
$request->post('detail')->detail() |
ทุกอย่างตามที่เขียน รวม CSS และ JS — ผู้เขียนที่ได้รับอนุญาต (บทความ หน้าเว็บ แม่แบบอีเมล) |
$request->post('detail')->detail(false) |
ตัดเฉพาะสิ่งที่รัน JavaScript ได้ CSS ฟอร์ม และ iframe อยู่ครบ |
$request->post('comment')->cleanHtml() |
การจัดรูปแบบเท่านั้น — เนื้อหาจากคนทั่วไป |
ก่อนแก้ไข (2026-11-11) editor ตัด
<style>,<script>, ฟอร์ม และ handler ทิ้งเองเมื่อโหลดเนื้อหาในโหมดปกติ การเปิดบทความแล้วบันทึกจึงทำให้
โค้ดโฆษณาหรือ CSS หาย และการ parse เนื้อหาในเอกสารจริงทำให้<img onerror>ในเนื้อหารันได้ตอนโหลดและตอนวางหน้านี้สร้างจากซอร์สโดยตรงเมื่อ 2026-08-21 คำอธิบายมาจาก JSDoc ในไฟล์
รันซ้ำได้ด้วยdocs_scaffoldเมื่อโค้ดเปลี่ยน อย่าแก้ด้วยมือถ้าตั้งใจให้ generate ต่อ
ที่มา
- ไฟล์:
Now/js/RichTextElementFactory.js - รูปแบบ: ES class
เมธอดสาธารณะ (3)
static cleanup(instance)
Destroy the editor attached to this element and release the reference. Safe to call when no editor was created, and errors thrown by the editor's own destroy are swallowed so teardown of the surrounding form still finishes.
static extractCustomConfig(element, def, dataset)
Read the editor options a single element declares through its data-rte-* attributes. Anything not declared falls back to the value in def, and placeholder also falls back to the element's own placeholder attribute.
static setupElement(instance)
Attach a RichTextEditor to the element and keep it in sync with the form. The editor bundle is loaded separately, so this retries every 100 ms until window.RichTextEditor exists rather than failing when it is not ready yet.
Data attribute ที่ใช้
data-read-only · data-rte-allow-iframe · data-rte-allow-interactive-tags · data-rte-allow-script · data-rte-allow-style · data-rte-fallback-bound · data-rte-fallback-loaded · data-rte-height · data-rte-max-height · data-rte-media-fallback · data-rte-min-height · data-rte-placeholder · data-rte-profile · data-rte-sticky
พึ่งพาคลาสอื่น
ElementFactory · ElementManager