콜아웃 노드 타입

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

스키마 정의

콜아웃 노드 타입의 스키마 정의:

{
  callout: {
    content: 'block+',
    group: 'block',
    attrs: {
      type: { default: 'info' }
    },
  }
}

모델 표현

모델 표현 예제:

{
  type: 'callout',
  attrs: { type: 'info' },
  children: [
    {
      type: 'paragraph',
      children: [{ type: 'text', text: '콜아웃 콘텐츠' }]
    }
  ]
}

HTML 직렬화

모델을 HTML로 변환:

function serializeCallout(node) {
  return '<div data-type="callout" data-callout-type="' + node.attrs.type + '">' + 
         serializeChildren(node.children) + '</div>';
}

HTML 역직렬화

HTML을 모델로 파싱:

function parseCallout(domNode) {
  return {
    type: 'callout',
    attrs: { type: domNode.getAttribute('data-callout-type') || 'info' },
    children: Array.from(domNode.childNodes)
      .map(node => parseNode(node))
      .filter(Boolean)
  };
}

뷰 연동

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

뷰 연동 코드:

// 콜아웃 렌더링
const callout = document.createElement('div');
callout.setAttribute('data-type', 'callout');
callout.setAttribute('data-callout-type', node.attrs.type);
callout.className = 'callout callout-' + node.attrs.type;
callout.contentEditable = 'true';
node.children.forEach(child => {
  callout.appendChild(renderNode(child));
});

일반적인 문제

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

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

// 문제: 콜아웃 타입 검증
// 해결: 콜아웃 타입 검증
function validateCalloutType(type) {
  const validTypes = ['info', 'warning', 'error', 'success'];
  return validTypes.includes(type) ? type : 'info';
}

// 문제: 콜아웃 스타일링
// 해결: 타입별 스타일 적용
function styleCallout(callout, type) {
  callout.className = 'callout callout-' + type;
  // 타입별 색상, 아이콘 등 적용
}

구현

완전한 구현 예제:

class CalloutNode {
  constructor(attrs, children) {
    this.type = 'callout';
    this.attrs = { type: attrs?.type || 'info' };
    this.children = children || [];
  }
  
  toDOM() {
    const callout = document.createElement('div');
    callout.setAttribute('data-type', 'callout');
    callout.setAttribute('data-callout-type', this.attrs.type);
    callout.className = 'callout callout-' + this.attrs.type;
    this.children.forEach(child => {
      callout.appendChild(child.toDOM());
    });
    return callout;
  }
  
  static fromDOM(domNode) {
    return new CalloutNode(
      { type: domNode.getAttribute('data-callout-type') || 'info' },
      Array.from(domNode.childNodes)
        .map(node => parseNode(node))
        .filter(Boolean)
    );
  }
}