@codemirror/view#showPanel TypeScript Examples

The following examples show how to use @codemirror/view#showPanel. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TemplateEditorTextArea.tsx    From backstage with Apache License 2.0 5 votes vote down vote up
/** A wrapper around CodeMirror with an error panel and extra actions available */
export function TemplateEditorTextArea(props: {
  content?: string;
  onUpdate?: (content: string) => void;
  errorText?: string;
  onSave?: () => void;
  onReload?: () => void;
}) {
  const { errorText } = props;
  const classes = useStyles();

  const panelExtension = useMemo(() => {
    if (!errorText) {
      return showPanel.of(null);
    }

    const dom = document.createElement('div');
    dom.classList.add(classes.errorPanel);
    dom.textContent = errorText;
    return showPanel.of(() => ({ dom, bottom: true }));
  }, [classes, errorText]);

  useKeyboardEvent(
    e => e.key === 's' && (e.ctrlKey || e.metaKey),
    e => {
      e.preventDefault();
      if (props.onSave) {
        props.onSave();
      }
    },
  );

  return (
    <div className={classes.container}>
      <CodeMirror
        className={classes.codeMirror}
        theme="dark"
        height="100%"
        extensions={[StreamLanguage.define(yamlSupport), panelExtension]}
        value={props.content}
        onChange={props.onUpdate}
      />
      {(props.onSave || props.onReload) && (
        <div className={classes.floatingButtons}>
          <Paper>
            {props.onSave && (
              <Tooltip title="Save file">
                <IconButton
                  className={classes.floatingButton}
                  onClick={() => props.onSave?.()}
                >
                  <SaveIcon />
                </IconButton>
              </Tooltip>
            )}
            {props.onReload && (
              <Tooltip title="Reload file">
                <IconButton
                  className={classes.floatingButton}
                  onClick={() => props.onReload?.()}
                >
                  <RefreshIcon />
                </IconButton>
              </Tooltip>
            )}
          </Paper>
        </div>
      )}
    </div>
  );
}