Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple editor instances conflict #205

Closed
xavier711 opened this issue Jun 14, 2023 · 0 comments
Closed

fix: multiple editor instances conflict #205

xavier711 opened this issue Jun 14, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@xavier711
Copy link
Contributor

version

latest

Link to minimal reproduction

No response

Steps to reproduce

import Editor from "@hufe921/canvas-editor"
const editor1 = new Editor(document.querySelector('#editor1'), [{
    value: 'first editor'
}])
const editor2 = new Editor(document.querySelector('#editor2'), [{
     value: 'second editor'
}])

Then execute editor1.command.getValue(), it returned data from the second editor.

What is expected?

data from editor1

What is actually happening?

data from editor2
Snipaste_1

System Info

No response

Any additional comments?

Here is the solution that I think will work:

// original code
class Command {
  private static execute: CommandAdapt['execute']
  constructor(adapt: CommandAdapt) {
    Command.execute = adapt.execute.bind(adapt)
  }
  public execute() {
    return Command.execute()
  }
}

// solution 1
// class Command {
//   private static execute: CommandAdapt['execute']
//   constructor(adapt: CommandAdapt) {
//     this.execute = adapt.execute.bind(adapt)
//   }
//   public execute() {
//     return Command.execute()
//   }
// }

// solution 2
// class Command {
//   private adapt: CommandAdapt
//   constructor(adapt: CommandAdapt) {
//     this.adapt = adapt
//   }
//   public execute() {
//     return this.adapt.execute()
//   }
// }

class CommandAdapt {
  private draw: Draw
  constructor(draw: Draw) {
    this.draw = draw
  }
  public execute() {
    console.log(this.draw.execute())
  }
}

class Draw {
  public data: string[]
  constructor(data: string[]) {
    this.data = data
  }
  public execute() {
    return this.data
  }
}

class Editor {
  public command: Command
  constructor(data: string[]) {
    const draw = new Draw(data)
    this.command = new Command(new CommandAdapt(draw))
  }
}

const editor1 = new Editor(['editor-1'])
const editor2 = new Editor(['editor-2'])

editor1.command.execute()
editor2.command.execute()
// expect console [ 'editor-1' ]
// actual console [ 'editor-2' ]
@Hufe921 Hufe921 added the bug Something isn't working label Jun 14, 2023
@Hufe921 Hufe921 changed the title multiple editors on one page will always use the instance of the last fix: multiple editor instances conflict Jun 14, 2023
@Hufe921 Hufe921 closed this as completed Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants