Fancy Front End Fancy Front End
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

让有意义的事变得有意思,让有意思的事变得有意义
  • 开始上手
  • 基础
  • 调度器(Scheduler)
  • 更新器(Updater)
  • 渲染器(Render)
  • 更新周期
  • hooks 原理
  • 总结
  • 📙 React源码漂流记
  • 开始上手
  • 基础
  • reactivity
  • runtime-core
  • runtime-dom
  • Awesome Web
  • Awesome NodeJS
话题
  • 导航
  • Q&A
  • 幻灯片
  • 关于
  • 分类
  • 标签
  • 归档
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • typescript-utility

  • single-spa源码

  • qiankun源码

  • webpack

  • axios

  • solid

  • vite源码

  • jquery源码

  • snabbdom

  • am-editor

    • 开始阅读
    • engine

      • index
      • engine-basic
      • engine
      • container
        • 目录
        • Container
      • change
      • range
  • html2canvas

  • express

  • acorn源码

  • immutable.js源码

  • web
  • am-editor
  • engine
jonsam
2022-04-14
目录

container

# Container 容器

# 目录

  • 目录
  • Container

# Container

class Container {
  // 容器配置
 private options: Options;
  // 容器节点
 private node: NodeInterface;
  // 是否 focus
 private _focused: boolean = false;
 #styleElement?: Element;

 constructor(selector: Selector, options: Options) {
  this.node = $(selector);
  this.options = options;
  this._init();
    // 初始化容器 focus
  this._focused =
   document.activeElement !== null &&
   this.node.equal(document.activeElement);
 }

 _init() {
  const { lang, tabIndex, className } = this.options;
    // 设置 container 的 data-element 元素类型为 root
  this.node.attributes(DATA_ELEMENT, ROOT);
    // 设置 container 的通用属性
  this.node.attributes({
   contenteditable: 'true',
   role: 'textbox',
   autocorrect: lang === 'en-US' ? 'on' : 'off',
   autocomplete: 'off',
   spellcheck: lang === 'en-US' ? 'true' : 'false',
   'data-gramm': 'false',
  });

  if (tabIndex !== undefined) {
   this.node.attributes('tabindex', tabIndex);
  }

  if (!this.node.hasClass('am-engine')) {
   this.node.addClass('am-engine');
  }

  if (isMobile) this.node.addClass('am-engine-mobile');

  // className 支持 array 和 string
    if (className !== undefined) {
   (Array.isArray(className)
    ? className
    : className.split(/\s+/)
   ).forEach((name) => {
    if (name.trim() !== '') this.node.addClass(name);
   });
  }
 }

 init() {
  const { engine } = this.options;
    // 监听 container 的 input 事件
  this.node.on('input', (e) => {
      // 预览模式不作处理
   if (engine.readonly) {
    return;
   }

   // card: card model instance
      // 不处理卡片中的输入事件
      if (engine.card.find(e.target)) {
    return;
   }
   const range = engine.change.range.get();
   range.handleBr(true);
  });
  // 编辑器文档尾部始终保持一行
  this.node.on('click', (event: MouseEvent) => {
   if (event.target && $(event.target).isEditable()) {
    // 获取到编辑器内最后一个子节点
    const block = this.node.last();
    if (block) {
     //不是卡片不处理
     if (!block.isCard()) return;
     //节点不可见不处理
     if (
      (block.get<HTMLElement>()?.offsetTop || 0) +
       (block.get<Element>()?.clientHeight || 0) >
      event.offsetY
     )
      return;
    }
    const node = $('<p><br /></p>');
    this.node.append(node);
    const range = engine.change.range.get();
        // 聚焦光标在最后一行
    range.select(node, true).collapse(false);
    engine.change.apply(range);
   }
  });
  let isMousedown = false;
  this.node.on(isMobile ? 'touchstart' : 'mousedown', () => {
   isMousedown = true;
   setTimeout(() => {
    if (!this._focused) {
     this._focused = true;
          // 鼠标按下触发 focus 事件
     engine.trigger('focus');
    }
    isMousedown = false;
   }, 10);
  });
  this.node.on('focus', () => {
   isMousedown = false;
   this._focused = true;
   engine.trigger('focus');
  });
  this.node.on('blur', () => {
   if (isMousedown) return;
   isMousedown = false;
   this._focused = false;
   engine.trigger('blur');
  });
 }

 isFocus() {
  return this._focused;
 }

 getNode() {
  return this.node;
 }

 setReadonly(readonly: boolean) {
  this.node.attributes('contenteditable', readonly ? 'false' : 'true');
 }

 showPlaceholder() {
  const { placeholder } = this.options;
  if (placeholder) {
   if (this.#styleElement && this.#styleElement.parentNode)
    document.body.removeChild(this.#styleElement);
   this.#styleElement = document.createElement('style');
   //const left = this.node.css('padding-left');
   //const top = this.node.css('padding-top');
      // 创建 placeholder 伪元素
   const styleText = document.createTextNode(`.am-engine:before {
                content: attr(data-placeholder);
                pointer-events: none;
                position: absolute;
                color: #bbbfc4;
                height: 0;
            }`);
   this.#styleElement.appendChild(styleText);
   document.body.appendChild(this.#styleElement);
   this.node.attributes({
    'data-placeholder': placeholder,
   });
  } else if (this.#styleElement && this.#styleElement.parentNode)
   document.body.removeChild(this.#styleElement);
 }

 hidePlaceholder() {
  this.node.removeAttributes('data-placeholder');
 }

 destroy() {
  const { className, engine } = this.options;
  this.node.removeAttributes(DATA_ELEMENT);
  this.node.removeAttributes('contenteditable');
  this.node.removeAttributes('role');
  this.node.removeAttributes('autocorrect');
  this.node.removeAttributes('autocomplete');
  this.node.removeAttributes('spellcheck');
  this.node.removeAttributes('data-gramm');
  this.node.removeAttributes('tabindex');
  this.node.removeAttributes('data-placeholder');
  if (this.#styleElement) document.body.removeChild(this.#styleElement);
  if (this.options.className) {
   (Array.isArray(className)
    ? className
    : (className || '').split(/\s+/)
   ).forEach((name) => {
    if (name.trim() !== '') this.node.removeClass(name);
   });
  }

  if (engine.card.closest(this.node)) this.node.removeClass('am-engine');
  this.node.removeAllEvents();
 }
}

export default Container;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
编辑 (opens new window)
上次更新: 2022/04/28, 23:58:26
engine
change

← engine change→

最近更新
01
渲染原理之组件结构与 JSX 编译
09-07
02
计划跟踪
09-06
03
开始上手
09-06
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy Front End | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式