-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8594 from nextcloud/enh/CKEditor-slash-commands
Add slash command and emoji picker to mail composer
- Loading branch information
Showing
6 changed files
with
305 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @author Daniel Kesselberg <[email protected]> | ||
* | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
import Command from '@ckeditor/ckeditor5-core/src/command' | ||
export default class InsertItemCommand extends Command { | ||
|
||
/** | ||
* @param {module:core/editor/editor~Editor} editor instance | ||
* @param {module:engine/model/writer~Writer} writer instance | ||
* @param {string} item smart picker or emoji picker | ||
* @param {string} trigger the character to replace | ||
*/ | ||
insertItem(editor, writer, item, trigger) { | ||
const currentPosition = editor.model.document.selection.getLastPosition() | ||
if (currentPosition === null) { | ||
// null as current position is probably not possible | ||
// @TODO Add error to handle such a situation in the callback | ||
return | ||
} | ||
|
||
const range = editor.model.createRange( | ||
currentPosition.getShiftedBy(-5), | ||
currentPosition | ||
) | ||
|
||
// Iterate over all items in this range: | ||
const walker = range.getWalker({ shallow: false, direction: 'backward' }) | ||
|
||
for (const value of walker) { | ||
if (value.type === 'text' && value.item.data.includes(trigger)) { | ||
writer.remove(value.item) | ||
|
||
const text = value.item.data | ||
const lastSlash = text.lastIndexOf(trigger) | ||
|
||
const textElement = writer.createElement('paragraph') | ||
writer.insertText(text.substring(0, lastSlash), textElement) | ||
editor.model.insertContent(textElement) | ||
|
||
const itemElement = writer.createElement('paragraph') | ||
writer.insertText(item, itemElement) | ||
editor.model.insertContent(itemElement) | ||
|
||
return | ||
} | ||
} | ||
|
||
// @TODO If we end up here, we did not find the slash. We should throw an error maybe. | ||
} | ||
|
||
/** | ||
* @param {string} item link from smart picker or emoji from emoji picker | ||
* @param {string} trigger the character to replace | ||
*/ | ||
execute(item, trigger) { | ||
this.editor.model.change(writer => { | ||
this.insertItem(this.editor, writer, item, trigger) | ||
}) | ||
} | ||
|
||
refresh() { | ||
this.isEnabled = true | ||
} | ||
|
||
} |
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,31 @@ | ||
/** | ||
* @author Daniel Kesselberg <[email protected]> | ||
* | ||
* | ||
* This code is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License, version 3, | ||
* as published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License, version 3, | ||
* along with this program. If not, see <http://www.gnu.org/licenses/> | ||
* | ||
*/ | ||
|
||
import Plugin from '@ckeditor/ckeditor5-core/src/plugin' | ||
import InsertItemCommand from './InsertItemCommand' | ||
export default class PickerPlugin extends Plugin { | ||
|
||
init() { | ||
this.editor.commands.add( | ||
'insertItem', | ||
new InsertItemCommand(this.editor) | ||
) | ||
} | ||
|
||
} |
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
Oops, something went wrong.