@atlaskit/editor-plugin-code-block
Version:
Code block plugin for @atlaskit/editor-core
40 lines (38 loc) • 1.15 kB
JavaScript
export const QUOTE_MAP = {
"'": "'",
'"': '"',
'`': '`'
};
export const shouldAutoCloseQuote = (before, after) => {
// when directly before a closing bracket
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
if (/^[}\])]/.test(after)) {
return true;
}
// exclusion: when directly before a non-whitespace character
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
if (/^[^\s]/.test(after)) {
return false;
}
// exclusion: when directly after a letter or quote
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
if (/[A-Za-z0-9]$/.test(before) || /[\'\"\`]$/.test(before)) {
return false;
}
return true;
};
export const getAutoClosingQuoteInfo = (before, after) => {
const left = Object.keys(QUOTE_MAP).find(item => before.endsWith(item));
const right = left ? QUOTE_MAP[left] : undefined;
const hasTrailingMatchingQuote = right ? after.startsWith(right) : false;
return {
left,
right,
hasTrailingMatchingQuote
};
};