mirror of
https://github.com/element-hq/element-web
synced 2024-11-29 04:48:50 +03:00
helper functions to find next & previous editable events in timeline
This commit is contained in:
parent
3c778e80b0
commit
8926fcb3a6
1 changed files with 34 additions and 1 deletions
|
@ -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];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue