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

text selection mixin now works in grid #247

Merged
merged 2 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ The author of the majority of the code is Miki, but this project would not be po
* Gerald Koch
* Sebastian Kühnau
* Jean-François Lamy
* Erik Lumme
* Simon Martinelli
* Dmitry Nazukin
* Stefan Penndorf
* Stuart Robinson
* Kaspar Scherrer
Expand All @@ -68,3 +70,5 @@ The author of the majority of the code is Miki, but this project would not be po
## Small print

All components are provided "as is", with no warranty or liability. See license for details.

This library is not officially supported or endorsed by Vaadin and is not part of the Vaadin Platform.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
* The source component must implement {@link CanSelectText} and {@link CanReceiveSelectionEventsFromClient} and delegate them
* to this object.
* The client-side component must mix in {@code text-selection-mixin.js} or otherwise react to needed JS method calls.
* Finally, the delegating class must implement a method {@code @ClientCallable void selectionChanged(int, int, String)}.
* Finally, the delegating class must implement methods: {@code @ClientCallable void selectionChanged(int, int, String)} and
* {@code @ClientCallable void reinitialiseEventListening()}. The first method should call this object's
* {@link #fireTextSelectionEvent(boolean, int, int, String)} and the second method should just be delegated to this object.
*
* @author miki
* @since 2020-06-01
Expand Down Expand Up @@ -115,7 +117,6 @@ public void selectNone() {
}
}


@Override
public void select(int from, int to) {
if(from <= to)
Expand Down Expand Up @@ -182,4 +183,12 @@ public void setReceivingSelectionEventsFromClient(boolean receivingSelectionEven
this.receivingSelectionEventsFromClient = receivingSelectionEventsFromClient;
this.informClientAboutSendingEvents(receivingSelectionEventsFromClient);
}

/**
* This method should be called in response to {@code @ClientCallable void reinitialiseListeners()} on the owning object.
*/
// fix for https://github.com/vaadin-miki/super-fields/issues/243 and the way components are initialised inside Grid
public void reinitialiseListeners() {
this.informClientAboutSendingEvents(this.isReceivingSelectionEventsFromClient());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ private void selectionChanged(int start, int end, String selection) {
this.delegate.fireTextSelectionEvent(true, start, end, selection);
}

@ClientCallable
private void reinitialiseListening() {
this.delegate.reinitialiseListeners();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ private void selectionChanged(int start, int end, String selection) {
this.delegate.fireTextSelectionEvent(true, start, end, selection);
}

@ClientCallable
private void reinitialiseListening() {
this.delegate.reinitialiseListeners();
}

@Override
public boolean isReceivingSelectionEventsFromClient() {
return this.delegate.isReceivingSelectionEventsFromClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SuperTextField extends TextSelectionMixin.to(TextFieldElement) {

setCallingServer(callingServer) {
console.log('STF: configuring event listeners; callingServer flag is '+callingServer);
console.log('STF: this now refers to '+this);
this.listenToEvents(this.inputElement, this, callingServer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,33 @@ export class TextSelectionMixin {

listenToEvents(inputComponent, webComponent, notifyServer) {
console.log('TSM: setting up text selection for component <'+webComponent.tagName+'>');
if (webComponent.selectionMixin === undefined) {
webComponent.selectionMixin = {
input: inputComponent,
callServer: notifyServer,
startsAt: -1,
endsAt: -1,
selection: ''
if (inputComponent === undefined) {
console.log('TSM: input component is undefined, attempting to find it from shadow root/input element');
inputComponent = webComponent.inputElement;
if (inputComponent === undefined) {
console.warn('TSM: no input component, server will reinitialise this component shortly');
// this trick has been suggested by the magnificent Erik Lumme, thank you!
webComponent.$server.reinitialiseListening();
}

const listener = () => webComponent.updateData(webComponent.selectionMixin, webComponent);
inputComponent.addEventListener('mouseup', listener);
inputComponent.addEventListener('keyup', listener);
inputComponent.addEventListener('mouseleave', listener);
}
else {
webComponent.selectionMixin.callServer = notifyServer;
if (inputComponent !== undefined) {
console.log('TSM: input component is ' + inputComponent);
if (webComponent.selectionMixin === undefined) {
webComponent.selectionMixin = {
input: inputComponent,
callServer: notifyServer,
startsAt: -1,
endsAt: -1,
selection: ''
}

const listener = () => webComponent.updateData(webComponent.selectionMixin, webComponent);
inputComponent.addEventListener('mouseup', listener);
inputComponent.addEventListener('keyup', listener);
inputComponent.addEventListener('mouseleave', listener);
} else {
webComponent.selectionMixin.callServer = notifyServer;
}
}
}
}
Expand Down