100 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-03-20 16:12:05 +01:00
import { trimEnd, trimStart } from 'lodash';
2018-03-13 21:39:27 +01:00
import styles from './styles.scss';
/**
* Override the editor css
* @param {[type]} block [description]
* @return {[type]} [description]
*/
2018-03-13 13:12:06 +01:00
export function getBlockStyle(block) {
switch (block.getType()) {
case 'blockquote':
return styles.editorBlockquote;
case 'code-block':
return styles.editorCodeBlock;
2018-03-22 12:01:11 +01:00
default:
return null;
2018-03-13 13:12:06 +01:00
}
}
export function getBlockContent(style) {
switch (style) {
case 'IMG':
return {
innerContent: 'link',
endReplacer: ')',
startReplacer: '![text](',
};
case 'code-block':
return {
innerContent: 'code block',
endReplacer: '`',
startReplacer: '`',
};
case 'blockquote':
return {
innerContent: 'quote',
endReplacer: '',
startReplacer: '> ',
};
2018-03-20 15:34:27 +01:00
case 'BOLD':
return {
innerContent: 'bold text',
endReplacer: '*',
startReplacer: '*',
};
case 'ITALIC':
return {
innerContent: 'italic text',
endReplacer: '*',
startReplacer: '*',
};
case 'STRIKED':
return {
innerContent: 'striked out',
endReplacer: '~',
startReplacer: '~',
};
case 'UNDERLINE':
return {
innerContent: 'underlined text',
endReplacer: '_',
startReplacer: '_',
};
case 'LINK':
return {
innerContent: 'link',
endReplacer: ')',
startReplacer: '[text](',
};
default:
return {
innerContent: '',
endReplacer: '',
startReplacer: '',
};
}
}
2018-03-13 21:39:27 +01:00
2018-03-22 12:01:11 +01:00
export const getDefaultSelectionOffsets = (
content,
startReplacer,
endReplacer,
initPosition = 0,
) => ({
anchorOffset: initPosition + content.length - trimStart(content, startReplacer).length,
focusOffset: initPosition + trimEnd(content, endReplacer).length,
});
2018-03-20 16:12:05 +01:00
2018-03-13 21:39:27 +01:00
/**
* Get the start and end offset
* @param {Object} selection
* @return {Object}
*/
export function getOffSets(selection) {
return {
end: selection.getEndOffset(),
start: selection.getStartOffset(),
};
}