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

Listening for focus and blur event #23

Open
kristofferdamborg opened this issue Aug 24, 2023 · 5 comments
Open

Listening for focus and blur event #23

kristofferdamborg opened this issue Aug 24, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@kristofferdamborg
Copy link

I have previously used the vue wrapper for CodeMirror 5 (https://github.com/RennZhang/codemirror-editor-vue3) which supported binding the focus and blur event directly on the component. Is it possible to do this as well with vue-codemirror6?

Example:

<template>
  <CodeMirror
     @focus="onFocus"
     @blur="onBlur"
  />
</template>
@kristofferdamborg kristofferdamborg changed the title Focus and blur event Listening for focus and blur event Aug 24, 2023
@logue
Copy link
Owner

logue commented Aug 25, 2023

There is no blur, but I think focus can be used as is.

/** Model Update */
'update:modelValue': (value: string | Text) => true,
/** CodeMirror ViewUpdate */
update: (value: ViewUpdate) => true,
/** CodeMirror onReady */
ready: (value: {
view: EditorView;
state: EditorState;
container: HTMLElement;
}) => true,
/** CodeMirror onFocus */
focus: (value: boolean) => true,
/** State Changed */
change: (value: EditorState) => true,
/** CodeMirror onDestroy */
destroy: () => true,

@kristofferdamborg
Copy link
Author

kristofferdamborg commented Aug 25, 2023

There is no blur, but I think focus can be used as is.

/** Model Update */
'update:modelValue': (value: string | Text) => true,
/** CodeMirror ViewUpdate */
update: (value: ViewUpdate) => true,
/** CodeMirror onReady */
ready: (value: {
view: EditorView;
state: EditorState;
container: HTMLElement;
}) => true,
/** CodeMirror onFocus */
focus: (value: boolean) => true,
/** State Changed */
change: (value: EditorState) => true,
/** CodeMirror onDestroy */
destroy: () => true,

@logue It doesn't seem to work unfortunately. Here's a Stackblitz replication: https://stackblitz.com/edit/vue-ump37q?file=src%2Fcomponents%2FEditor.vue

@logue logue added the bug Something isn't working label Aug 25, 2023
logue added a commit that referenced this issue Aug 30, 2023
Add Focus demo.
Update dependencies.
@kristofferdamborg
Copy link
Author

@logue It unfortunately doesn't seem like f5d4fd9 fixed it. There's still no focus event triggered :(

Here's an updated Stackblitz (with version 1.1.26): https://vue-ump37q.stackblitz.io

@kristofferdamborg
Copy link
Author

kristofferdamborg commented Sep 5, 2023

@logue

A possible solution is to add a focusChangeEffect as an extension. Tested to be working locally, see example below.

function focusEffectHandler(state: EditorState, focusing: boolean): StateEffect<any> | null {
    if (focusing) {
        emit('focus');
    } else {
        emit('blur');
    }

    return null;
}

const extensions = [
     EditorView.focusChangeEffect.of(focusEffectHandler),
     //  ...other extensions
];

@logue
Copy link
Owner

logue commented Sep 6, 2023

In 1.1.27, focus was solved by hooking ViewUpdate. As a side effect, character count processing and error counting processing are faster.

However, regarding blur, the official answer is as follows, but it didn't work when I implemented it, so I decided to postpone it.
https://discuss.codemirror.net/t/code-mirror-6-has-focus-what-about-blur/4071

I skipped the implementation of blur because it seems to be hitting the DOM directly instead of CodeMirror's function.
So, for blur, I'm thinking of releasing an API that can hit the dom (contentDOM).

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

No branches or pull requests

2 participants