Skip to content

Commit

Permalink
fix(placeholder): fix placeholder cannot follow the font size
Browse files Browse the repository at this point in the history
  • Loading branch information
pubuzhixing8 committed Jan 18, 2022
1 parent 92e7ff7 commit 004015c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/breezy-lies-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slate-angular": patch
---

correct placeholder position, placeholder span must be inside text node
8 changes: 5 additions & 3 deletions packages/src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
opacity: 0.333;
user-select: none;
text-decoration: none;
top: 0px;
top: 0;
}
[data-slate-node='element'] {
&.element-placeholder {
& [data-slate-leaf="true"] {
&.leaf-with-placeholder {
position: relative;
display: inline-block;
width: 100%;
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions packages/src/view/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class BaseLeafComponent extends BaseComponent<SlateLeafContext> implement

placeholderElement: HTMLSpanElement;

@HostBinding('attr.data-slate-leaf') isSlateLeaf = true
@HostBinding('attr.data-slate-leaf') isSlateLeaf = true

get text(): Text {
return this.context && this.context.text;
Expand All @@ -82,31 +82,26 @@ export class BaseLeafComponent extends BaseComponent<SlateLeafContext> implement
renderPlaceholder() {
// issue-1: IME input was interrupted
// issue-2: IME input focus jumping
// Issue occurs when the placeholder node is removed (in leaf span)
// So add a placeholder span to the block element root node
// Issue occurs when the span node of the placeholder is before the slateString span node
if (this.context.leaf['placeholder']) {
if (!this.placeholderElement) {
this.placeholderElement = document.createElement('span');
this.placeholderElement.innerText = this.context.leaf['placeholder'];
this.placeholderElement.contentEditable = 'false';
this.placeholderElement.setAttribute('data-slate-placeholder', 'true');
this.nativeElement.closest('[data-slate-node="element"]')?.classList.add('element-placeholder');
this.nativeElement.closest('[data-slate-node="element"]')?.appendChild(this.placeholderElement);
this.nativeElement.classList.add('leaf-with-placeholder');
this.nativeElement.appendChild(this.placeholderElement);
}
} else {
if (this.placeholderElement) {
this.placeholderElement.remove();
this.placeholderElement = null;
this.nativeElement.closest('[data-slate-node="element"]')?.classList.remove('element-placeholder');
}
this.destroyPlaceholder();
}
}

destroyPlaceholder() {
if (this.placeholderElement) {
this.placeholderElement.remove();
this.placeholderElement = null;
this.nativeElement?.closest('[data-slate-node="element"]')?.classList.remove('element-placeholder');
this.nativeElement.classList.remove('leaf-with-placeholder');
}
}
}
Expand Down

0 comments on commit 004015c

Please sign in to comment.