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

[Feature]: Add the _enterEditing method to prevent it from triggering the editing:entered event. #10187

Closed
4 tasks done
zhe-he opened this issue Sep 29, 2024 · 2 comments
Closed
4 tasks done

Comments

@zhe-he
Copy link
Contributor

zhe-he commented Sep 29, 2024

CheckList

  • I agree to follow this project's Code of Conduct
  • I have read and followed the Contributing Guide
  • I have searched and referenced existing issues, feature requests and discussions
  • I am filing a FEATURE request.

Description

I noticed that there are _exitEditing and exitEditing methods for exiting text editing, but there is no _enterEditing method for entering text editing; there is only enterEditing. I hope there will be a _enterEditing method for entering text editing.
I have a requirement where entering and exiting text editing triggers undoRedo, so I listen for text:editing:entered and text:editing:exited. After performing the undo operation, I will execute _exitEditing and _enterEditing to blur and focus without triggering the aforementioned listener events. However, currently, there is no _enterEditing method.

Current State

Here is a minimal implementation example to represent the idea I mentioned.

const mapId = {};
const undo = [];
const redo = [];
canvas.on("text:editing:entered", e => {
   if (!e.target.id) e.target.id = Date.now();
   mapId[e.target.id] = e.target;
   undo.push({ type: "text:entered", data: { id: e.target.id } });
   redo = [];
})
canvas.on("text:editing:exited", e => {
   undo.push({ type: "text:exited", data: { id: e.target.id } });
   redo = [];
})

// key press:   command + z
function keyPressUndo() {
    const data = undo[undo.length - 1];
    if (data.type == "text:exited") {
        // Use method _enterEditing instead of method enterEditing to prevent it from entering the aforementioned event listener.
        mapId[data.data.id]._enterEditing();
    } else if (data.type == "text:entered") {
        // Use method _exitEditing instead of method exitEditing to prevent it from entering the aforementioned event listener.
        mapId[data.data.id]._exitEditing();
    }
    redo.push(data);
}

// key press:   shift + command + z
function keyPressRedo() {
    const data = redo[redo.length - 1];
    if (data.type == "text:exited") {
        mapId[data.data.id]._exitEditing();
    } else if () {
        mapId[data.data.id]._enterEditing();
    }
    undo.push(data);
}

Additional Context

No response

@asturur
Copy link
Member

asturur commented Sep 30, 2024

What is the reason to trigger undo/redo from an operation that does not actually do any change to the text?
I m not opposed to consolidate the pattern with and without events to be clear.

@zhe-he
Copy link
Contributor Author

zhe-he commented Oct 8, 2024

What is the reason to trigger undo/redo from an operation that does not actually do any change to the text? I m not opposed to consolidate the pattern with and without events to be clear.

The undo and redo functionality includes the user's entry into and exit from text editing, so I listen for text:editing:entered and text:editing:exited to log them to meet my needs. When the user normally enters and exits text editing, all operations are recorded in an array. When the user actively triggers an undo, the latest entry is taken from the recorded array. If this data is 'exit editing', I need to actively trigger 'enter editing'. However, currently, the internal 'enter editing' in Fabric triggers the text:editing:entered event, which gets recorded in the undo array (considered a normal entry into editing). This is not what I want; I need a clean 'enter editing' event that does not trigger the text:editing:entered event, so I submitted this PR.

@asturur asturur closed this as completed Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants