storybook-addon-playground
Version:
A playground to enable consumers learn how to use the component library or to reproduce bugs
39 lines (36 loc) • 1.45 kB
JavaScript
import { EditorSelection } from '@uiw/react-codemirror';
import { getEditorStateInfo, parseTagFromLineText } from '../utils/extensions-utils.js';
function insertAutoClosingTagCommand(view) {
const { state, dispatch } = view;
const { cursorPos, fullLineText, lineTextUpToCursor, lineTextAfterCursor } = getEditorStateInfo(state);
const tagName = parseTagFromLineText(lineTextUpToCursor);
if (!tagName) {
return false;
}
if (isSelfClosingTag(fullLineText)) {
return false;
}
if (isInsertingContentInsideTags(lineTextAfterCursor)) {
return false;
}
if (lineTextUpToCursor.trim().endsWith("/") ||
lineTextAfterCursor.startsWith(">")) {
return false;
}
// +1 to move in between the inserted closing bracket and the closing tag
const newCursorPos = EditorSelection.cursor(cursorPos + 1);
dispatch(state.update({
changes: { from: cursorPos, insert: `></${tagName}>` },
selection: newCursorPos,
}));
return true;
}
function isSelfClosingTag(lineText) {
return /<\w+(\s+\w+="[^"]*")*\s*\/>$/.test(lineText);
}
function isInsertingContentInsideTags(lineTextAfterCursor) {
// check if there's a closing tag after the cursor (meaning the inserted `>` is part of the content)
return /<\/[a-zA-Z0-9]+>/.test(lineTextAfterCursor);
}
export { insertAutoClosingTagCommand as default };
//# sourceMappingURL=closing-bracket.js.map