|
| 1 | + |
| 2 | +/* IMPORT */ |
| 3 | + |
| 4 | +import * as _ from 'lodash'; |
| 5 | + |
| 6 | +/* FONT */ |
| 7 | + |
| 8 | +const Font = { |
| 9 | + |
| 10 | + boldRe: /(\*)(.+)(\*)/, |
| 11 | + italicRe: /(_)(.+)(_)/, |
| 12 | + strikethroughRe: /(~)(.+)(~)/, |
| 13 | + boldItalicRe: /(\*_)(.+)(_\*)|(_\*)(.+)(\*_)/, |
| 14 | + boldStrikethroughRe: /(\*~)(.+)(~\*)|(~\*)(.+)(\*~)/, |
| 15 | + italicStrikethroughRe: /(_~)(.+)(~_)|(~_)(.+)(_~)/, |
| 16 | + boldItalicStrikethroughRe: /(\*_~)(.+)(~_\*)|(\*~_)(.+)(_~\*)|(_\*~)(.+)(~\*_)|(_~\*)(.+)(\*~_)|(~_\*)(.+)(\*_~)|(~\*_)(.+)(_\*~)/, |
| 17 | + |
| 18 | + getTokens () { |
| 19 | + |
| 20 | + const repeat = ( arr, times ) => _.concat ( [], ..._.range ( times ).map ( () => _.cloneDeep ( arr ) ) ), |
| 21 | + makeTokens = ( classNames, times = 1 ) => { |
| 22 | + const tokens = [`${classNames} font-token`, classNames, `${classNames} font-token`]; |
| 23 | + return repeat ( tokens, times ); |
| 24 | + }; |
| 25 | + |
| 26 | + return { |
| 27 | + start: [ |
| 28 | + { regex: Font.boldItalicStrikethroughRe, token: makeTokens ( 'bold italic strikethrough', 6 ) }, |
| 29 | + { regex: Font.boldItalicRe, token: makeTokens ( 'bold italic', 2 ) }, |
| 30 | + { regex: Font.boldStrikethroughRe, token: makeTokens ( 'bold strikethrough', 2 ) }, |
| 31 | + { regex: Font.italicStrikethroughRe, token: makeTokens ( 'italic strikethrough', 2 ) }, |
| 32 | + { regex: Font.boldRe, token: makeTokens ( 'bold' ) }, |
| 33 | + { regex: Font.italicRe, token: makeTokens ( 'italic' ) }, |
| 34 | + { regex: Font.strikethroughRe, token: makeTokens ( 'strikethrough' ) } |
| 35 | + ] |
| 36 | + }; |
| 37 | + |
| 38 | + }, |
| 39 | + |
| 40 | + toggleToken ( cm, prefix, suffix = prefix, content = '' ) { |
| 41 | + |
| 42 | + cm.doc.replaceSelections ( cm.doc.getSelections ().map ( selection => { |
| 43 | + |
| 44 | + const hasToken = selection.slice ( 0, prefix.length ) === prefix && selection.slice ( - suffix.length ) === suffix; |
| 45 | + |
| 46 | + return hasToken ? selection.slice ( prefix.length, - suffix.length ) : `${prefix}${selection || content}${suffix}`; |
| 47 | + |
| 48 | + }), 'around' ); |
| 49 | + |
| 50 | + }, |
| 51 | + |
| 52 | + toggleBold ( cm ) { |
| 53 | + |
| 54 | + Font.toggleToken ( cm, '*', '*', 'Bold' ); |
| 55 | + |
| 56 | + }, |
| 57 | + |
| 58 | + toggleItalic ( cm ) { |
| 59 | + |
| 60 | + Font.toggleToken ( cm, '_', '_', 'Italic' ); |
| 61 | + |
| 62 | + }, |
| 63 | + |
| 64 | + toggleStrikethrough ( cm ) { |
| 65 | + |
| 66 | + Font.toggleToken ( cm, '~', '~', 'Strikethrough' ); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | +}; |
| 71 | + |
| 72 | +/* EXPORT */ |
| 73 | + |
| 74 | +export default Font; |
0 commit comments