This repository has been archived by the owner on Sep 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4527da4
commit 5cd046a
Showing
7 changed files
with
5,954 additions
and
4,556 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import {Circle, createScene, Rect, Widget} from 'newcar' | ||
import {Circle, Color, createScene, move, Rect, Widget, useFont} from 'newcar' | ||
import TextEditor from "@newcar/mod-text-editor"; | ||
import * as nc from "newcar"; | ||
|
||
await nc.useFont('./Roboto-Regular.ttf') | ||
await useFont('./default.ttf') | ||
|
||
export default createScene( | ||
new TextEditor('Text', { | ||
dragable: true | ||
}) | ||
new TextEditor('Hello', { | ||
x: 100, | ||
y: 100, | ||
}) | ||
// new Widget() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,62 @@ | ||
import {click, Line, mouseDown, mouseMove, TextOptions, TextStyle} from '@newcar/basic' | ||
import { Text, mouseMoveOnCanvas } from '@newcar/basic' | ||
import type { Ref } from '@newcar/core' | ||
import { ref } from '@newcar/core' | ||
import {CanvasKit} from "canvaskit-wasm"; | ||
import type { TextOptions, TextStyle } from '@newcar/basic' | ||
import { Text } from '@newcar/basic' | ||
import { changed } from '@newcar/core' | ||
import type { Canvas, CanvasKit } from 'canvaskit-wasm' | ||
|
||
export interface TextEditorOptions extends TextOptions { | ||
style?: TextEditorStyle | ||
editable?: boolean | ||
selectable?: boolean | ||
} | ||
|
||
export interface TextEditorStyle extends TextStyle {} | ||
|
||
export default class TextEditor extends Text { | ||
editable: Ref<boolean> | ||
selectable: Ref<boolean> | ||
|
||
private isInText = false | ||
private cursor: Line | ||
private mouseStart: [number, number] | ||
private mouseEnd: [number, number] | ||
element: HTMLInputElement = document.createElement('input') | ||
|
||
constructor(originText: string, options?: TextEditorOptions) { | ||
super(originText, options) | ||
} | ||
|
||
this.editable = ref(options.editable ?? true) | ||
this.selectable = ref(options.selectable ?? true) | ||
init(ck: CanvasKit) { | ||
super.init(ck) | ||
|
||
this.cursor = new Line([0, 0], [0, 100]).hide() | ||
this.add(this.cursor) | ||
this.element.style.width = `${this.calculateRange()[2]}px` | ||
this.element.style.height = `${this.calculateRange()[3]}px` | ||
} | ||
|
||
this.on(mouseMoveOnCanvas, (widget, x, y) => { | ||
if (this.calculateIn(x, y)) { | ||
console.log('move in!') | ||
document.body.style.cursor = 'text' | ||
} | ||
else { | ||
document.body.style.cursor = 'auto' | ||
} | ||
setElement(element: HTMLCanvasElement) { | ||
super.setElement(element) | ||
element.parentElement.appendChild(this.element) | ||
this.element.style.position = 'absolute' | ||
this.element.style.backgroundColor = 'rgba(0,0,0,0)' | ||
this.element.style.left = `${this.x.value + element.getBoundingClientRect().left}px` | ||
this.element.style.top = `${this.y.value + element.getBoundingClientRect().top}px` | ||
this.element.style.border = 'none' | ||
this.element.style.fontSize = `${this.style.fontSize.value}px` | ||
this.element.style.color = this.style.color.toString() | ||
|
||
changed(this.x, (v) => { | ||
this.element.style.left = `${v.value + element.clientLeft}px` | ||
}) | ||
|
||
this.on(mouseDown, () => { | ||
this.isInText = true | ||
console.log('click in!') | ||
changed(this.y, (v) => { | ||
this.element.style.top = `${v.value + element.clientTop}px` | ||
}) | ||
|
||
this.on(click, (widget, x, y) => { | ||
this.cursor.from.value = [x, this.calculateRange()[1]] | ||
this.cursor.to.value = [x, this.calculateRange()[3]] | ||
this.cursor.show() | ||
changed(this.text, () => { | ||
this.element.style.width = `${this.calculateRange()[2]}px` | ||
this.element.style.height = `${this.calculateRange()[3]}px` | ||
}) | ||
|
||
this.on(mouseMove, (x, y) => { | ||
this.builder.getText() | ||
this.element.addEventListener('click', () => { | ||
this.element.value = this.text.value | ||
this.hide() | ||
}) | ||
this.element.addEventListener('blur', () => { | ||
this.text.value = this.element.value | ||
this.show() | ||
this.element.value = '' | ||
}) | ||
} | ||
|
||
init(ck: CanvasKit) { | ||
super.init(ck) | ||
draw(canvas: Canvas) { | ||
super.draw(canvas) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.