RuleEditor

A CodeMirror 6 based rule editor with Regex validation, search, and theme switching.

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.greasyfork.org/scripts/566628/1896077/RuleEditor.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

작성자
ryxel
버전
2.0.0
생성일
2026-02-18
갱신일
2026-08-07
크기
973KB
라이선스
MIT

CodeMirror 6 Rule Editor Documentation

This editor is built on CodeMirror 6, integrating custom syntax highlighting, regular expression validation, real-time search navigation, and multi-theme switching.


Quick Start

Initialize the editor by instantiating the RuleEditor class.

const container = document.getElementById('editor-container');
const editor = new RuleEditor(container, {
  content: "# Initial rules\n/example/g",
  darkMode: false
});


Core API

Instance Properties & Options

  • darkMode (boolean)
    • Description: Property getter/setter to retrieve or dynamically toggle the visual theme. true for dark mode, false for light mode.
    • Example: editor.darkMode = true;

Content Operations

  • setContent(content, keepHistory)
    • Description: Sets the editor text content.
    • Parameters:
      • content (string): The new text content.
      • keepHistory (boolean, default false): When false, resets undo history and syncs baseline snapshot.
  • getContent()
    • Description: Returns the complete text content of the editor.
  • updateSnapshot()
    • Description: Manually sets the current document content as a new baseline snapshot.
  • restoreToSnapshot()
    • Description: Restores the editor text to the last saved baseline snapshot.

Search & Navigation

  • setSearch(term)
    • Description: Updates the search keyword, triggers highlights, and automatically navigates to the first match.
  • findNext()
    • Description: Navigates forward to the next match.
  • findPrev()
    • Description: Navigates backward to the previous match.

Event Callbacks

Assign callback functions directly to instance properties:

  • onDirtyChange
    • Description: Triggered when the document's modified (dirty) state changes relative to the baseline snapshot.
    • Callback Argument: isDirty (boolean) — Indicates whether the document has been modified.
    • Example: editor.onDirtyChange = (isDirty) => { console.log(isDirty); };
  • onSearchUpdate
    • Description: Triggered when search results are updated.
    • Callback Argument: Returns object { total, current, isExact }
      • total: Total number of matched lines.
      • current: Line index closest to the current cursor position (1-based).
      • isExact: Whether the cursor is exactly on a matched line.
    • Example: editor.onSearchUpdate = (res) => { console.log(res); };

Rule Parsing & Cleanup

  • getRules()
    • Description: Parses the syntax tree and returns structured rule data.
    • Return Structure: Returns object { regexes, keywords, errors }
      • regexes: Array of valid RegExp objects [{ pattern, flags }].
      • keywords: Array of recognized keyword strings [string].
      • errors: Array of invalid RegExp objects and position info [{ text, from, to, line }].
  • destroy()
    • Description: Destroys the editor instance and releases resources.


Syntax Reference

The editor supports the following three basic syntax elements:

  • Keywords: Plain text used for substring matching.
  • Regular Expressions: Supports /pattern/flags format with real-time syntax validation and error reporting.
  • Comments: Entire lines or inline text starting with #.


Credits & References

This project is built on top of the CodeMirror 6 core library, with interaction design and UI inspiration drawn from uBlacklist and uBlock Origin.