This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 833
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add link creation to rich text editor (#9775)
Add link creation to RTE
- Loading branch information
1 parent
88c3864
commit fe0273b
Showing
23 changed files
with
418 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
res/css/views/rooms/wysiwyg_composer/components/_LinkModal.pcss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
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. | ||
*/ | ||
|
||
.mx_LinkModal { | ||
padding: $spacing-32; | ||
|
||
.mx_Dialog_content { | ||
margin-top: 30px; | ||
margin-bottom: 42px; | ||
} | ||
|
||
.mx_LinkModal_content { | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions
36
src/components/views/rooms/wysiwyg_composer/ComposerContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
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 { createContext, useContext } from "react"; | ||
|
||
import { SubSelection } from "./types"; | ||
|
||
export function getDefaultContextValue(): { selection: SubSelection } { | ||
return { | ||
selection: { anchorNode: null, anchorOffset: 0, focusNode: null, focusOffset: 0 }, | ||
}; | ||
} | ||
|
||
export interface ComposerContextState { | ||
selection: SubSelection; | ||
} | ||
|
||
export const ComposerContext = createContext<ComposerContextState>(getDefaultContextValue()); | ||
ComposerContext.displayName = "ComposerContext"; | ||
|
||
export function useComposerContext() { | ||
return useContext(ComposerContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/components/views/rooms/wysiwyg_composer/components/LinkModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2022 The Matrix.org Foundation C.I.C. | ||
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 { FormattingFunctions } from "@matrix-org/matrix-wysiwyg"; | ||
import React, { ChangeEvent, useState } from "react"; | ||
|
||
import { _td } from "../../../../../languageHandler"; | ||
import Modal from "../../../../../Modal"; | ||
import QuestionDialog from "../../../dialogs/QuestionDialog"; | ||
import Field from "../../../elements/Field"; | ||
import { ComposerContextState } from "../ComposerContext"; | ||
import { isSelectionEmpty, setSelection } from "../utils/selection"; | ||
|
||
export function openLinkModal(composer: FormattingFunctions, composerContext: ComposerContextState) { | ||
const modal = Modal.createDialog( | ||
LinkModal, | ||
{ composerContext, composer, onClose: () => modal.close(), isTextEnabled: isSelectionEmpty() }, | ||
"mx_CompoundDialog", | ||
false, | ||
true, | ||
); | ||
} | ||
|
||
function isEmpty(text: string) { | ||
return text.length < 1; | ||
} | ||
|
||
interface LinkModalProps { | ||
composer: FormattingFunctions; | ||
isTextEnabled: boolean; | ||
onClose: () => void; | ||
composerContext: ComposerContextState; | ||
} | ||
|
||
export function LinkModal({ composer, isTextEnabled, onClose, composerContext }: LinkModalProps) { | ||
const [fields, setFields] = useState({ text: "", link: "" }); | ||
const isSaveDisabled = (isTextEnabled && isEmpty(fields.text)) || isEmpty(fields.link); | ||
|
||
return ( | ||
<QuestionDialog | ||
className="mx_LinkModal" | ||
title={_td("Create a link")} | ||
button={_td("Save")} | ||
buttonDisabled={isSaveDisabled} | ||
hasCancelButton={true} | ||
onFinished={async (isClickOnSave: boolean) => { | ||
if (isClickOnSave) { | ||
await setSelection(composerContext.selection); | ||
composer.link(fields.link, isTextEnabled ? fields.text : undefined); | ||
} | ||
onClose(); | ||
}} | ||
description={ | ||
<div className="mx_LinkModal_content"> | ||
{isTextEnabled && ( | ||
<Field | ||
autoFocus={true} | ||
label={_td("Text")} | ||
value={fields.text} | ||
onChange={(e: ChangeEvent<HTMLInputElement>) => | ||
setFields((fields) => ({ ...fields, text: e.target.value })) | ||
} | ||
/> | ||
)} | ||
<Field | ||
autoFocus={!isTextEnabled} | ||
label={_td("Link")} | ||
value={fields.link} | ||
onChange={(e: ChangeEvent<HTMLInputElement>) => | ||
setFields((fields) => ({ ...fields, link: e.target.value })) | ||
} | ||
/> | ||
</div> | ||
} | ||
/> | ||
); | ||
} |
Oops, something went wrong.