/* Copyright 2022 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { useContext, useMemo, useRef, useState } from "react"; import { IContent, MatrixEvent } from "matrix-js-sdk/src/models/event"; import { _t, _td } from "../../../../languageHandler"; import Field from "../../elements/Field"; import BaseTool, { DevtoolsContext, IDevtoolsProps } from "./BaseTool"; import MatrixClientContext from "../../../../contexts/MatrixClientContext"; import withValidation from "../../elements/Validation"; import SyntaxHighlight from "../../elements/SyntaxHighlight"; export const stringify = (object: object): string => { return JSON.stringify(object, null, 2); }; interface IEventEditorProps extends Pick { fieldDefs: IFieldDef[]; // immutable defaultContent?: string; onSend(fields: string[], content?: IContent): Promise; } interface IFieldDef { id: string; label: string; // _td default?: string; } export const eventTypeField = (defaultValue?: string): IFieldDef => ({ id: "eventType", label: _td("Event Type"), default: defaultValue, }); export const stateKeyField = (defaultValue?: string): IFieldDef => ({ id: "stateKey", label: _td("State Key"), default: defaultValue, }); const validateEventContent = withValidation({ deriveData({ value }) { try { JSON.parse(value); } catch (e) { return e; } }, rules: [{ key: "validJson", test: ({ value }, error) => { if (!value) return true; return !error; }, invalid: (error) => _t("Doesn't look like valid JSON.") + " " + error, }], }); export const EventEditor = ({ fieldDefs, defaultContent = "{\n\n}", onSend, onBack }: IEventEditorProps) => { const [fieldData, setFieldData] = useState(fieldDefs.map(def => def.default ?? "")); const [content, setContent] = useState(defaultContent); const contentField = useRef(); const fields = fieldDefs.map((def, i) => ( setFieldData(data => { data[i] = ev.target.value; return [...data]; })} /> )); const onAction = async () => { const valid = await contentField.current.validate({}); if (!valid) { contentField.current.focus(); contentField.current.validate({ focused: true }); return; } try { const json = JSON.parse(content); await onSend(fieldData, json); } catch (e) { return _t("Failed to send event!") + ` (${e.toString()})`; } return _t("Event sent!"); }; return
{ fields }
setContent(ev.target.value)} element="textarea" onValidate={validateEventContent} ref={contentField} autoFocus={!!defaultContent} />
; }; export interface IEditorProps extends Pick { mxEvent?: MatrixEvent; } interface IViewerProps extends Required { Editor: React.FC>; } export const EventViewer = ({ mxEvent, onBack, Editor }: IViewerProps) => { const [editing, setEditing] = useState(false); if (editing) { const onBack = () => { setEditing(false); }; return ; } const onAction = async () => { setEditing(true); }; return { stringify(mxEvent.event) } ; }; // returns the id of the initial message, not the id of the previous edit const getBaseEventId = (baseEvent: MatrixEvent): string => { // show the replacing event, not the original, if it is an edit const mxEvent = baseEvent.replacingEvent() ?? baseEvent; return mxEvent.getWireContent()["m.relates_to"]?.event_id ?? baseEvent.getId(); }; export const TimelineEventEditor = ({ mxEvent, onBack }: IEditorProps) => { const context = useContext(DevtoolsContext); const cli = useContext(MatrixClientContext); const fields = useMemo(() => [ eventTypeField(mxEvent?.getType()), ], [mxEvent]); const onSend = ([eventType]: string[], content?: IContent) => { return cli.sendEvent(context.room.roomId, eventType, content); }; let defaultContent: string; if (mxEvent) { const originalContent = mxEvent.getContent(); // prefill an edit-message event, keep only the `body` and `msgtype` fields of originalContent const bodyToStartFrom = originalContent["m.new_content"]?.body ?? originalContent.body; // prefill the last edit body, to start editing from there const newContent = { "body": ` * ${bodyToStartFrom}`, "msgtype": originalContent.msgtype, "m.new_content": { body: bodyToStartFrom, msgtype: originalContent.msgtype, }, "m.relates_to": { rel_type: "m.replace", event_id: getBaseEventId(mxEvent), }, }; defaultContent = stringify(newContent); } return ; };