Skip to content

Commit

Permalink
feat(floating-toolbar): rename config.range to config.target; support…
Browse files Browse the repository at this point in the history
… node type
  • Loading branch information
luolonghao committed Oct 17, 2024
1 parent f2ad65a commit 20ff903
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/models/box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { toBase64 } from '../utils/to-base64';
import { fromBase64 } from '../utils/from-base64';
import { query } from '../utils/query';
import { Nodes } from './nodes';
import { Range } from './range';
import { FloatingToolbar } from '../ui/floating-toolbar';

const framework = safeTemplate`
Expand Down Expand Up @@ -148,10 +147,8 @@ export class Box {
if (this.toolbar) {
this.toolbar.unmount();
}
const range = new Range();
range.selectNodeContents(this.node);
this.toolbar = new FloatingToolbar({
range,
target: this.node,
items,
});
this.toolbar.render();
Expand Down
11 changes: 9 additions & 2 deletions src/ui/floating-toolbar.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { editors } from '../storage/editors';
import { isVisible } from '../utils/is-visible';
import { Nodes } from '../models/nodes';
import { Range } from '../models/range';
import { Toolbar, ToolbarConfig } from './toolbar';

type FloatingToolbarConfig = ToolbarConfig & {
range: Range;
target: Nodes | Range;
};

export class FloatingToolbar extends Toolbar {
Expand All @@ -13,7 +14,13 @@ export class FloatingToolbar extends Toolbar {

constructor(config: FloatingToolbarConfig) {
super(config);
this.range = config.range;
if (config.target instanceof Nodes) {
const range = new Range();
range.selectNodeContents(config.target);
this.range = range;
} else {
this.range = config.target;
}
this.container.removeClass('lake-toolbar');
this.container.addClass('lake-popup');
this.container.addClass('lake-floating-toolbar');
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/floating-toolbar-ui.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { boxes } from '../../src/storage/boxes';
import { icons } from '../../src/icons';
import { query } from '../../src/utils';
import { Range } from '../../src/models/range';
import { Box } from '../../src/models/box';
import { FloatingToolbar } from '../../src/ui/floating-toolbar';
import { Editor } from '../../src';
Expand Down Expand Up @@ -106,10 +105,8 @@ describe('ui / floating-toolbar-ui', () => {
editor.setValue('<p style="text-align: center;"><lake-box type="inline" name="floatingToolbarUiTestBox"></lake-box></p>');
const box = new Box(editor.container.find('lake-box'));
box.render();
const range = new Range();
range.selectNodeContents(box.node);
const floatingToolbar = new FloatingToolbar({
range,
target: box.node,
items: [
{
name: 'open',
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/floating-toolbar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('ui / floating-toolbar', () => {
const range = new Range();
range.selectNodeContents(editor.container.find('p'));
const floatingToolbar = new FloatingToolbar({
range,
target: range,
items: [
{
name: 'open',
Expand Down Expand Up @@ -67,7 +67,7 @@ describe('ui / floating-toolbar', () => {
const range = new Range();
range.selectNodeContents(editor.container.find('p'));
const floatingToolbar = new FloatingToolbar({
range,
target: range,
items: [
{
name: 'align',
Expand Down

0 comments on commit 20ff903

Please sign in to comment.