테이블 헤더 노드 타입

카테고리: 구조적 • 뷰 연동 노트를 포함한 상세 구현 가이드

스키마 정의

테이블 헤더 노드 타입의 스키마 정의:

{
  tableHeader: {
    content: 'block+',
    tableRole: 'header_cell',
    isolating: true,
    attrs: {
      colspan: { default: 1 },
      rowspan: { default: 1 }
    },
                      node.attrs.rowspan > 1 ? { rowspan: node.attrs.rowspan } : {}, 0]
  }
}

모델 표현

모델 표현 예제:

{
  type: 'tableHeader',
  attrs: { colspan: 1, rowspan: 1 },
  children: [
    {
      type: 'paragraph',
      children: [{ type: 'text', text: '헤더' }]
    }
  ]
}

HTML 직렬화

모델을 HTML로 변환:

function serializeTableHeader(node) {
  const attrs = {};
  if (node.attrs.colspan > 1) attrs.colspan = node.attrs.colspan;
  if (node.attrs.rowspan > 1) attrs.rowspan = node.attrs.rowspan;
  return '<th' + serializeAttrs(attrs) + '>' + 
         serializeChildren(node.children) + '</th>';
}

HTML 역직렬화

HTML을 모델로 파싱:

function parseTableHeader(domNode) {
  return {
    type: 'tableHeader',
    attrs: {
      colspan: parseInt(domNode.getAttribute('colspan')) || 1,
      rowspan: parseInt(domNode.getAttribute('rowspan')) || 1
    },
    children: Array.from(domNode.childNodes)
      .map(node => parseNode(node))
      .filter(Boolean)
  };
}

뷰 연동

뷰 연동 노트: 이 노드 타입을 뷰 레이어에서 구현할 때 contenteditable 동작, 선택 처리, 이벤트 관리에 특히 주의하세요.

뷰 연동 코드:

// 테이블 헤더 렌더링
const th = document.createElement('th');
th.contentEditable = 'true';
if (node.attrs.colspan > 1) th.colSpan = node.attrs.colspan;
if (node.attrs.rowspan > 1) th.rowSpan = node.attrs.rowspan;
node.children.forEach(child => {
  th.appendChild(renderNode(child));
}

일반적인 문제

일반적인 함정: 이 노드 타입을 구현할 때 자주 발생하는 문제들입니다. 구현 전에 주의 깊게 검토하세요.

일반적인 문제 및 해결 방법:

// 문제: 헤더 스타일링
// 해결: 일관된 헤더 스타일 적용
function styleTableHeader(th) {
  th.style.fontWeight = 'bold';
  th.style.textAlign = 'center';
}

// 문제: Scope 속성
// 해결: 접근성을 위해 scope 설정
function setHeaderScope(th, scope) {
  th.setAttribute('scope', scope); // 'col' 또는 'row'
}

구현

완전한 구현 예제:

class TableHeaderNode {
  constructor(attrs, children) {
    this.type = 'tableHeader';
    this.attrs = {
      colspan: attrs?.colspan || 1,
      rowspan: attrs?.rowspan || 1
    };
    this.children = children || [];
  }
  
  toDOM() {
    const th = document.createElement('th');
    if (this.attrs.colspan > 1) th.colSpan = this.attrs.colspan;
    if (this.attrs.rowspan > 1) th.rowSpan = this.attrs.rowspan;
    this.children.forEach(child => {
      th.appendChild(child.toDOM());
    });
    return th;
  }
  
  static fromDOM(domNode) {
    return new TableHeaderNode(
      {
        colspan: parseInt(domNode.getAttribute('colspan')) || 1,
        rowspan: parseInt(domNode.getAttribute('rowspan')) || 1
      },
      Array.from(domNode.childNodes)
        .map(node => parseNode(node))
        .filter(Boolean)
    );
  }
}