Skip to content

Commit

Permalink
feat: line placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
zzxming committed Jan 10, 2025
1 parent 366332b commit 631e9b4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ export class QuillQuickInsert {
this.currentMenu = this.options.menuItems;
this.menuSorter = this.createMenuItemsSorter(this.options.menuItems);

this.placeholderDisplay();

this.quill.on(Quill.events.COMPOSITION_START, () => {
this.placeholderDisplay();
});
this.quill.on(Quill.events.EDITOR_CHANGE, (type: string) => {
if (type === Quill.events.SELECTION_CHANGE) {
this.placeholderDisplay();
}
});
this.quill.on(Quill.events.TEXT_CHANGE, () => {
const range = this.quill.getSelection();
if (range) {
Expand Down Expand Up @@ -49,6 +59,7 @@ export class QuillQuickInsert {

resolveOptions(options: Partial<QuillQuickInsertInputOptions>): QuillQuickInsertOptions {
const result = Object.assign({
placeholder: 'Input / recall menu',
menuItems: [] as any[],
}, options);
result.menuItems = result.menuItems.map((item) => {
Expand All @@ -60,6 +71,21 @@ export class QuillQuickInsert {
return result;
}

placeholderDisplay() {
const placeholders = Array.from(this.quill.root.querySelectorAll(':scope > p[data-placeholder]')) as HTMLElement[];
for (const item of placeholders) {
delete item.dataset.placeholder;
}

const range = this.quill.getSelection(true);
if (range && range.length === 0) {
const [line] = this.quill.getLine(range.index);
if (line && line.length() === 1) {
line.domNode.dataset.placeholder = this.options.placeholder;
}
}
}

createMenuItemsSorter(items: Menu) {
const list: MenuItems[] = [];
for (const item of items) {
Expand Down
23 changes: 23 additions & 0 deletions src/style/index.scss
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
@use './menu.scss' as *;

.ql-editor {
> p[data-placeholder] {
position: relative;
&::before {
content: attr(data-placeholder);
position: absolute;
top: 50%;
transform: translateY(-50%);
padding-left: 4px;
user-select: none;
pointer-events: none;
white-space: nowrap;
font-size: 14px;
color: #d8dad9;
}
&.ql-align-right {
&::before {
right: 4px;
}
}
}
}
3 changes: 2 additions & 1 deletion src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ export interface MenuItemsGroup {
}
export type Menu = (MenuItems | MenuItemsGroup)[];

export interface QuillQuickInsertInputOptions {
export interface QuillQuickInsertInputOptions extends Omit<QuillQuickInsertOptions, 'menuItems'> {
menuItems: ((Omit<MenuItems, 'type'> & { type?: 'item' }) | MenuItemsGroup)[];
}

export interface QuillQuickInsertOptions {
menuItems: (MenuItems | MenuItemsGroup)[];
placeholder: string;
}

0 comments on commit 631e9b4

Please sign in to comment.