Skip to content

Commit fa01d81

Browse files
authored
fix(core): handle triple-click #244 #180 (#253)
1 parent 4ef6370 commit fa01d81

File tree

7 files changed

+43
-5
lines changed

7 files changed

+43
-5
lines changed

.changeset/twenty-geckos-roll.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"slate-angular": patch
3+
---
4+
5+
handle triple-click
6+
remove the attribute of editable='false' in void element to void strange behavior when click before image element
7+
refer to:
8+
https://github.com/ianstormtaylor/slate/pull/4588
9+
https://github.com/ianstormtaylor/slate/pull/4965

demo/app/components/image/image-component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { SlateChildren } from '../../../../packages/src/components/children/chil
55

66
@Component({
77
selector: 'demo-element-image',
8-
template: `<slate-children [children]="children" [context]="childrenContext" [viewContext]="viewContext"></slate-children>
9-
<img [src]="element.url" alt="" [class.outline]="selection" /> `,
8+
template: `<img [src]="element.url" alt="" [class.outline]="selection" /> `,
109
host: {
1110
class: 'demo-element-image'
1211
},

packages/src/components/editable/editable.component.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import { SlateDefaultLeaf } from '../leaf/default-leaf.component';
6868
import { SLATE_DEFAULT_LEAF_COMPONENT_TOKEN } from '../leaf/token';
6969
import { BaseElementComponent, BaseLeafComponent, BaseTextComponent } from '../../view/base';
7070
import { ListRender } from '../../view/render/list-render';
71+
import { TRIPLE_CLICK } from '../../utils/constants';
7172

7273
// not correctly clipboardData on beforeinput
7374
const forceOnDOMPaste = IS_SAFARI;
@@ -859,6 +860,22 @@ export class SlateEditable implements OnInit, OnChanges, OnDestroy, AfterViewChe
859860
const startVoid = Editor.void(this.editor, { at: start });
860861
const endVoid = Editor.void(this.editor, { at: end });
861862

863+
if (event.detail === TRIPLE_CLICK && path.length >= 1) {
864+
let blockPath = path;
865+
if (!(Element.isElement(node) && Editor.isBlock(this.editor, node))) {
866+
const block = Editor.above(this.editor, {
867+
match: n => Element.isElement(n) && Editor.isBlock(this.editor, n),
868+
at: path
869+
});
870+
871+
blockPath = block?.[1] ?? path.slice(0, 1);
872+
}
873+
874+
const range = Editor.range(this.editor, blockPath);
875+
Transforms.select(this.editor, range);
876+
return;
877+
}
878+
862879
if (startVoid && endVoid && Path.equals(startVoid[1], endVoid[1])) {
863880
const range = Editor.range(this.editor, start);
864881
Transforms.select(this.editor, range);

packages/src/plugins/angular-editor.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,21 @@ export const AngularEditor = {
685685
const anchor = AngularEditor.toSlatePoint(editor, [anchorNode, anchorOffset]);
686686
const focus = isCollapsed ? anchor : AngularEditor.toSlatePoint(editor, [focusNode, focusOffset]);
687687

688-
return { anchor, focus };
688+
let range: Range = { anchor: anchor as Point, focus: focus as Point };
689+
// if the selection is a hanging range that ends in a void
690+
// and the DOM focus is an Element
691+
// (meaning that the selection ends before the element)
692+
// unhang the range to avoid mistakenly including the void
693+
if (
694+
Range.isExpanded(range) &&
695+
Range.isForward(range) &&
696+
isDOMElement(focusNode) &&
697+
Editor.void(editor, { at: range.focus, mode: 'highest' })
698+
) {
699+
range = Editor.unhangRange(editor, range, { voids: true });
700+
}
701+
702+
return range;
689703
},
690704

691705
isLeafBlock(editor: AngularEditor, node: Node): boolean {

packages/src/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const TRIPLE_CLICK = 3;

packages/src/view/context.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export interface SlateElementAttributes {
5454
'data-slate-node': 'element';
5555
'data-slate-void'?: boolean;
5656
'data-slate-inline'?: boolean;
57-
contenteditable?: boolean;
5857
'data-slate-key'?: string;
5958
dir?: 'rtl';
6059
}

packages/src/view/render/list-render.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ export function getContext(
183183
}
184184
if (isVoid) {
185185
elementContext.attributes['data-slate-void'] = true;
186-
elementContext.attributes.contenteditable = false;
187186
}
188187
return elementContext;
189188
} else {

0 commit comments

Comments
 (0)