helper functions to find next & previous editable events in timeline

This commit is contained in:
Bruno Windels 2019-05-24 14:41:24 +02:00
parent 3c778e80b0
commit 8926fcb3a6

View file

@ -16,7 +16,8 @@ limitations under the License.
import { EventStatus } from 'matrix-js-sdk'; import { EventStatus } from 'matrix-js-sdk';
import MatrixClientPeg from '../MatrixClientPeg'; import MatrixClientPeg from '../MatrixClientPeg';
import { findLastIndex, findIndex } from "lodash";
import shouldHideEvent from "../shouldHideEvent";
/** /**
* Returns whether an event should allow actions like reply, reactions, edit, etc. * Returns whether an event should allow actions like reply, reactions, edit, etc.
* which effectively checks whether it's a regular message that has been sent and that we * which effectively checks whether it's a regular message that has been sent and that we
@ -50,3 +51,35 @@ export function canEditContent(mxEvent) {
mxEvent.getOriginalContent().msgtype === "m.text" && mxEvent.getOriginalContent().msgtype === "m.text" &&
mxEvent.getSender() === MatrixClientPeg.get().getUserId(); mxEvent.getSender() === MatrixClientPeg.get().getUserId();
} }
export function findPreviousEditableEvent(room, fromEventId = undefined) {
const liveTimeline = room.getLiveTimeline();
const events = liveTimeline.getEvents();
let startFromIdx = events.length - 1;
if (fromEventId) {
const fromEventIdx = findLastIndex(events, e => e.getId() === fromEventId);
if (fromEventIdx !== -1) {
startFromIdx = fromEventIdx - 1;
}
}
const nextEventIdx = findLastIndex(events, e => !shouldHideEvent(e) && canEditContent(e), startFromIdx);
if (nextEventIdx !== -1) {
return events[nextEventIdx];
}
}
export function findNextEditableEvent(room, fromEventId = undefined) {
const liveTimeline = room.getLiveTimeline();
const events = liveTimeline.getEvents();
let startFromIdx = 0;
if (fromEventId) {
const fromEventIdx = findIndex(events, e => e.getId() === fromEventId);
if (fromEventIdx !== -1) {
startFromIdx = fromEventIdx + 1;
}
}
const nextEventIdx = findIndex(events, e => !shouldHideEvent(e) && canEditContent(e), startFromIdx);
if (nextEventIdx !== -1) {
return events[nextEventIdx];
}
}