Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get current applied formats? #29

Open
rushangfynd opened this issue Aug 28, 2023 · 2 comments
Open

How to get current applied formats? #29

rushangfynd opened this issue Aug 28, 2023 · 2 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@rushangfynd
Copy link

rushangfynd commented Aug 28, 2023

When a user taps "Return" without entering a value, the unordered list format should automatically deselect. However, there isn't a decisive variable or method to determine the current selected format, posing a challenge in altering the UI accordingly.

PFA Video.

Simulator.Screen.Recording.-.iPhone.14.Pro.-.2023-08-28.at.14.49.47.mp4
 public func unorderedList() {
      runJS("RE.setUnorderedList();")
 }

@Andrew-Chen-Wang
Copy link
Owner

Andrew-Chen-Wang commented Aug 29, 2023

Nice catch. Off the top of my head, using the "callback" function in the rich_editor.js file is the way to go:

  1. From rich_editor.js, use RE.callback("input"); where you change input to something we can identify (e.g. disableUnorderedListButton) in our conditional tree seen below
  2. Here's the conditional tree where you'll receive the command:
    private func performCommand(_ method: String) {
    if method.hasPrefix("ready") {
    // If loading for the first time, we have to set the content HTML to be displayed
    if !isEditorLoaded {
    isEditorLoaded = true
    setHTML(html)
    contentHTML = html
    contentEditable = editingEnabledVar
    placeholder = placeholderText
    lineHeight = DefaultInnerLineHeight
    delegate?.richEditorDidLoad?(self)
    }
    updateHeight()
    }
    else if method.hasPrefix("input") {
  3. Just check for when the user presses "enter" or "return" and fire the callback when that happens. Then, you can access the toolbar in the RichEditorView swift class and deselect. The logic to detect whether the cursor is in an unordered list is a little weird because the nearest outer HTML tag within a block of text in a list may be strong instead of the the li element itself; I think you would essentially find the nearest list in the DOM hierarchy. Follow something similar to this:
    function getNearestTableAncestor(htmlElementNode) {
    while (htmlElementNode) {
    htmlElementNode = htmlElementNode.parentNode;
    if (htmlElementNode.tagName.toLowerCase() === 'table') {
    return htmlElementNode;
    }
    }
    return undefined;
    }
    or
    RE.getSelectedHref = function() {
    var href, sel;
    href = '';
    sel = window.getSelection();
    if (!RE.rangeOrCaretSelectionExists()) {
    return null;
    }
    var tags = RE.getAnchorTagsInNode(sel.anchorNode);
    //if more than one link is there, return null
    if (tags.length > 1) {
    return null;
    } else if (tags.length == 1) {
    href = tags[0];
    } else {
    var node = _findNodeByNameInContainer(sel.anchorNode.parentElement, 'A', 'editor');
    href = node.href;
    }
    return href ? href : null;
    };

If you've solved it, please open a PR or leave some code here for others. Thanks!

@Andrew-Chen-Wang Andrew-Chen-Wang added bug Something isn't working enhancement New feature or request labels Aug 29, 2023
@rushangfynd
Copy link
Author

Merge request created, please review it.

#30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants