How yuruiyin/richeditor works
Project Type and Purpose
• This is an Android component library (“richeditor” module) plus a sample application (“app” module).
• Purpose: provide a fully functional rich-text editor based on a custom RichEditText view, leveraging Android’s native EditText + Span architecture.
File Structure Analysis
Top-level:
• settings.gradle – includes two modules: “app” (sample) and “richeditor” (library)
• build.gradle / gradle scripts – build configuration for multi-module project
• image/ – demo assets for README
“app” module:
• build.gradle, proguard-rules.pro – app-specific configuration
• src/main – sample application entry point (MainActivity), sample models (ImageVm, VideoVm, GameVm), utilities, layouts, resources
• src/androidTest, src/test – instrumentation and unit tests
“richeditor” module (core library):
• build.gradle, proguard-rules.pro – library configuration
• src/main/java/com/yuruiyin/richeditor – core code:
– RichEditText.java (custom view)
– RichInputConnectionWrapper.java, RichTextWatcher.java (input handling)
– span package: implementations of IInlineSpan/IBlockSpan for block and inline styling (BoldStyleSpan, InlineImageSpan, CustomQuoteSpan, etc.)
– model package: data models (RichEditorBlock, BlockImageSpanVm, InlineImageSpanVm, StyleBtnVm)
– callback, config, enumtype, utils, undoredo packages
• src/main/res – custom attributes (attrs.xml), layout for image spans, drawables, styles
• src/androidTest, src/test – tests
Key Insights from README
• The library interfaces through a single custom view (RichEditText) and exposes APIs to initialize style buttons, insert images/videos/custom Views, and retrieve a list of RichEditorBlock objects.
• Internally, it manages spans for styling, block insertion, undo/redo, keyboard handling.
• Sample app demonstrates builder pattern (StyleBtnVm), file-picker integration, custom layout insertion (GameVm), and content serialization.
Main Components
A. Sample Application (“app” module)
• UI Layer: MainActivity + XML layouts + style buttons
• Interaction Layer: file picker Intent, onActivityResult, FileUtil
• RichEditor API Usage: initStyleButton(), insertBlockImage(), content retrieval
B. RichEditor Library (“richeditor” module)
1. RichEditText (Custom View)
2. Input Handling
• RichInputConnectionWrapper – intercepts IME commands
• RichTextWatcher – monitors text changes to apply spans
3. Span Management
• Inline spans (BoldStyleSpan, ItalicStyleSpan, InlineImageSpan, etc.)
• Block spans (BlockImageSpan for images/videos/custom Views)
4. Model Layer
• RichEditorBlock – represents each paragraph block
• BlockImageSpanVm, InlineImageSpanVm, StyleBtnVm
5. Undo/Redo Engine (UndoRedoHelper)
6. Utilities & Config
• FileUtil, ClipboardUtil, ViewUtil, WindowUtil, BitmapUtil
• AppConfig, custom attributes in attrs.xml
7. Callbacks & Extensibility
• OnImageClickListener
• Public APIs to insert spans, register style buttons, retrieve content
Relationships and Data Flows
• User taps style button in Sample App → StyleBtnVm event → RichEditText.initStyleButton() registers watcher → onClick triggers RichEditText.applyInlineStyle() → RichTextWatcher updates Spannable
• User inserts image/video/custom View → Sample Activity picks file or inflates layout → passes path/bitmap + BlockImageSpanVm to richEditText.insertBlockImage() → RichEditText creates BlockImageSpan → SpanManagement attaches it to text, updates RichEditorBlock list
• Undo/redo operations triggered by keyboard or programmatic calls → UndoRedoHelper maintains action stack
• At any point, app calls richEditText.getContent() → returns List<RichEditorBlock>
Architectural Patterns & Principles
• Span-based styling (leveraging Android’s native text engine)
• Builder pattern for StyleBtnVm
• Custom View encapsulation (RichEditText) exposing a clear public API
• Separation of concerns: input handling, span management, model representation, utilities, and UI usage are neatly divided into packages
• Extensibility via callbacks and custom attributes
Essential Elements
• Component names and boundaries
• Technology annotations (Android EditText, Spannable, IME, Kotlin/Java)