2018-05-12 22:04:58 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Based originally on slate-plain-serializer
|
|
|
|
|
|
|
|
import { Block } from 'slate';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plain text serializer, which converts a Slate `value` to a plain text string,
|
|
|
|
* serializing pills into various different formats as required.
|
|
|
|
*
|
|
|
|
* @type {PlainWithPillsSerializer}
|
|
|
|
*/
|
|
|
|
|
|
|
|
class PlainWithPillsSerializer {
|
|
|
|
/*
|
|
|
|
* @param {String} options.pillFormat - either 'md', 'plain', 'id'
|
|
|
|
*/
|
|
|
|
constructor(options = {}) {
|
2018-05-21 01:43:42 +03:00
|
|
|
const {
|
2018-05-12 22:04:58 +03:00
|
|
|
pillFormat = 'plain',
|
|
|
|
} = options;
|
|
|
|
this.pillFormat = pillFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a Slate `value` to a plain text string,
|
|
|
|
* serializing pills as either MD links, plain text representations or
|
|
|
|
* ID representations as required.
|
|
|
|
*
|
|
|
|
* @param {Value} value
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
serialize = value => {
|
2018-05-21 01:43:42 +03:00
|
|
|
return this._serializeNode(value.document);
|
2018-05-12 22:04:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a `node` to plain text.
|
|
|
|
*
|
|
|
|
* @param {Node} node
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
_serializeNode = node => {
|
|
|
|
if (
|
|
|
|
node.object == 'document' ||
|
|
|
|
(node.object == 'block' && Block.isBlockList(node.nodes))
|
|
|
|
) {
|
|
|
|
return node.nodes.map(this._serializeNode).join('\n');
|
2018-05-21 01:43:42 +03:00
|
|
|
} else if (node.type == 'emoji') {
|
2018-05-14 05:02:12 +03:00
|
|
|
return node.data.get('emojiUnicode');
|
2018-05-12 22:04:58 +03:00
|
|
|
} else if (node.type == 'pill') {
|
2018-07-24 14:18:10 +03:00
|
|
|
const completion = node.data.get('completion');
|
|
|
|
// over the wire the @room pill is just plaintext
|
|
|
|
if (completion === '@room') return completion;
|
|
|
|
|
2018-05-12 22:04:58 +03:00
|
|
|
switch (this.pillFormat) {
|
|
|
|
case 'plain':
|
2018-07-24 14:18:10 +03:00
|
|
|
return completion;
|
2018-05-12 22:04:58 +03:00
|
|
|
case 'md':
|
2018-07-24 14:18:10 +03:00
|
|
|
return `[${ completion }](${ node.data.get('href') })`;
|
2018-05-12 22:04:58 +03:00
|
|
|
case 'id':
|
2018-07-24 14:18:10 +03:00
|
|
|
return node.data.get('completionId') || completion;
|
2018-05-12 22:04:58 +03:00
|
|
|
}
|
2018-05-21 01:43:42 +03:00
|
|
|
} else if (node.nodes) {
|
2018-05-12 22:04:58 +03:00
|
|
|
return node.nodes.map(this._serializeNode).join('');
|
2018-05-21 01:43:42 +03:00
|
|
|
} else {
|
2018-05-12 22:04:58 +03:00
|
|
|
return node.text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*
|
|
|
|
* @type {PlainWithPillsSerializer}
|
|
|
|
*/
|
|
|
|
|
2018-05-21 01:43:42 +03:00
|
|
|
export default PlainWithPillsSerializer;
|