XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEServerManager

【Java】我的世界XFE服务器管理器

公开
关注 0 Fork 0 Star 0
UTF-8
import { useEffect, useRef, type ReactNode } from 'react';
import { X } from 'lucide-react';
import { ModalPortal } from './modal-portal';
import { Button } from './ui';

/** Transactional node editor; portals keep it outside the workspace's scroll/disabled containers. */
export function TriggerNodeDialog({ children, zh, writable, error, onApply, onClose }: {
  children: ReactNode; zh: boolean; writable: boolean; error?: string;
  onApply: () => void; onClose: () => void;
}) {
  const dialog = useRef<HTMLElement>(null);
  const close = useRef<HTMLButtonElement>(null);
  const l = (chinese: string, english: string) => zh ? chinese : english;
  useEffect(() => {
    const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null;
    const overflow = document.body.style.overflow;
    document.body.style.overflow = 'hidden';
    close.current?.focus();
    const focus = (event: FocusEvent) => {
      const target = event.target;
      // The rich-text variable picker owns focus while its nested portal is open.
      const nested = document.querySelector<HTMLElement>('.variable-browser[role="dialog"]');
      if (nested && target instanceof Node && !nested.contains(target)) {
        nested.querySelector<HTMLElement>('button:not(:disabled),input:not(:disabled)')?.focus(); return;
      }
      if (target instanceof Element && !target.closest('[role="dialog"]')) close.current?.focus();
    };
    document.addEventListener('focusin', focus);
    return () => {
      document.removeEventListener('focusin', focus);
      document.body.style.overflow = overflow;
      if (previous?.isConnected) previous.focus();
    };
  }, []);
  return <ModalPortal><div className="trigger-node-dialog-backdrop" onMouseDown={(event) => {
    if (event.target === event.currentTarget) onClose();
  }}><section ref={dialog} className="trigger-node-dialog" role="dialog" aria-modal="true"
    aria-label={l('编辑节点', 'Edit node')} onKeyDown={(event) => {
      if (!(event.target instanceof Node) || !dialog.current?.contains(event.target)
        || document.querySelector('.variable-browser[role="dialog"]')) return;
      if (event.key === 'Escape') { event.preventDefault(); event.stopPropagation(); onClose(); }
      if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === 's') {
        event.preventDefault(); event.stopPropagation(); if (writable) onApply();
      }
      if (event.key !== 'Tab') return;
      const focusable = [...dialog.current.querySelectorAll<HTMLElement>(
        'button,input,select,textarea,[contenteditable="true"],[tabindex="0"]')]
        .filter((element) => !element.matches(':disabled') && !element.closest('details:not([open])'));
      const first = focusable[0]; const last = focusable.at(-1);
      if (event.shiftKey && document.activeElement === first) { event.preventDefault(); last?.focus(); }
      else if (!event.shiftKey && document.activeElement === last) { event.preventDefault(); first?.focus(); }
    }}>
    <header className="trigger-node-dialog__header"><div><h2>{l('编辑节点', 'Edit node')}</h2>
      <p>{writable ? l('修改仅在应用后写入程序,可一次撤销;保存触发器后才会提交服务器。',
        'Apply changes to update the program in one undo step. Save the trigger to submit to the server.')
        : l('只读查看,当前账户不能修改此程序。', 'Read-only inspection. This account cannot change the program.')}</p></div>
      <button ref={close} type="button" className="trigger-node-dialog__close"
        aria-label={l('关闭节点编辑', 'Close node editor')} onClick={onClose}><X size={20} /></button></header>
    <div className="trigger-node-dialog__body">{children}</div>
    <footer className="trigger-node-dialog__footer">{error && <p role="alert">{error}</p>}
      <Button type="button" variant="secondary" onClick={onClose}>{l('取消', 'Cancel')}</Button>
      <Button type="button" disabled={!writable} onClick={onApply}>{l('应用修改', 'Apply changes')}</Button></footer>
  </section></div></ModalPortal>;
}