- Fixed 'JSON cannot stringify cyclic structures' web console error

- Changed undo key command to meta+Z or ctrl+z

- Fixed a bug stopping drawings from switching correctly.
This commit is contained in:
louisnw
2023-07-17 15:05:11 +01:00
parent 95607c6fa4
commit 27b8268799
4 changed files with 47 additions and 39 deletions

View File

@ -1,7 +1,7 @@
project = 'lightweight-charts-python' project = 'lightweight-charts-python'
copyright = '2023, louisnw' copyright = '2023, louisnw'
author = 'louisnw' author = 'louisnw'
release = '1.0.14' release = '1.0.14.1'
extensions = ["myst_parser"] extensions = ["myst_parser"]

View File

@ -567,7 +567,7 @@ The following hotkeys can also be used when the Toolbox is enabled:
* Alt+T: Trendline * Alt+T: Trendline
* Alt+H: Horizontal Line * Alt+H: Horizontal Line
* Alt+R: Ray Line * Alt+R: Ray Line
* Meta+Z: Undo * Meta+Z or Ctrl+Z: Undo
___ ___

View File

@ -21,6 +21,12 @@ if (!window.ToolBox) {
this.subscribeHoverMove() this.subscribeHoverMove()
} }
toJSON() {
// Exclude the chart attribute from serialization
const {chart, ...serialized} = this;
return serialized;
}
makeToolBox() { makeToolBox() {
let toolBoxElem = document.createElement('div') let toolBoxElem = document.createElement('div')
toolBoxElem.style.position = 'absolute' toolBoxElem.style.position = 'absolute'
@ -63,7 +69,7 @@ if (!window.ToolBox) {
this.drawings.splice(this.drawings.length - 1) this.drawings.splice(this.drawings.length - 1)
} }
this.chart.commandFunctions.push((event) => { this.chart.commandFunctions.push((event) => {
if (event.metaKey && event.code === 'KeyZ') { if ((event.metaKey || event.ctrlKey) && event.code === 'KeyZ') {
commandZHandler(this.drawings[this.drawings.length - 1]) commandZHandler(this.drawings[this.drawings.length - 1])
return true return true
} }
@ -73,10 +79,13 @@ if (!window.ToolBox) {
} }
makeToolBoxElement(action, keyCmd, paths) { makeToolBoxElement(action, keyCmd, paths) {
let elem = document.createElement('div') let icon = {
elem.style.margin = '3px' elem: document.createElement('div'),
elem.style.borderRadius = '4px' action: action,
elem.style.display = 'flex' }
icon.elem.style.margin = '3px'
icon.elem.style.borderRadius = '4px'
icon.elem.style.display = 'flex'
let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); let svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("width", "29"); svg.setAttribute("width", "29");
@ -87,54 +96,53 @@ if (!window.ToolBox) {
group.setAttribute("fill", this.iconColor) group.setAttribute("fill", this.iconColor)
svg.appendChild(group) svg.appendChild(group)
elem.appendChild(svg); icon.elem.appendChild(svg);
elem.addEventListener('mouseenter', () => { icon.elem.addEventListener('mouseenter', () => {
elem.style.backgroundColor = elem === this.chart.activeIcon ? this.activeBackgroundColor : this.hoverColor icon.elem.style.backgroundColor = icon === this.chart.activeIcon ? this.activeBackgroundColor : this.hoverColor
document.body.style.cursor = 'pointer' document.body.style.cursor = 'pointer'
}) })
elem.addEventListener('mouseleave', () => { icon.elem.addEventListener('mouseleave', () => {
elem.style.backgroundColor = elem === this.chart.activeIcon ? this.activeBackgroundColor : this.backgroundColor icon.elem.style.backgroundColor = icon === this.chart.activeIcon ? this.activeBackgroundColor : this.backgroundColor
document.body.style.cursor = this.chart.cursor document.body.style.cursor = this.chart.cursor
}) })
elem.addEventListener('click', () => { icon.elem.addEventListener('click', () => {
if (this.chart.activeIcon) { if (this.chart.activeIcon) {
this.chart.activeIcon.style.backgroundColor = this.backgroundColor this.chart.activeIcon.elem.style.backgroundColor = this.backgroundColor
group.setAttribute("fill", this.iconColor) group.setAttribute("fill", this.iconColor)
document.body.style.cursor = 'crosshair' document.body.style.cursor = 'crosshair'
this.chart.cursor = 'crosshair' this.chart.cursor = 'crosshair'
action(false) this.chart.activeIcon.action(false)
if (this.chart.activeIcon === icon) {
return this.chart.activeIcon = null
} }
if (this.chart.activeIcon === elem) {
this.chart.activeIcon = null
return
} }
this.chart.activeIcon = elem this.chart.activeIcon = icon
group.setAttribute("fill", this.activeIconColor) group.setAttribute("fill", this.activeIconColor)
elem.style.backgroundColor = this.activeBackgroundColor icon.elem.style.backgroundColor = this.activeBackgroundColor
document.body.style.cursor = 'crosshair' document.body.style.cursor = 'crosshair'
this.chart.cursor = 'crosshair' this.chart.cursor = 'crosshair'
action(true) this.chart.activeIcon.action(true)
}) })
this.chart.commandFunctions.push((event) => { this.chart.commandFunctions.push((event) => {
if (event.altKey && event.code === keyCmd) { if (event.altKey && event.code === keyCmd) {
if (this.chart.activeIcon) { if (this.chart.activeIcon) {
this.chart.activeIcon.style.backgroundColor = this.backgroundColor this.chart.activeIcon.elem.style.backgroundColor = this.backgroundColor
group.setAttribute("fill", this.iconColor) group.setAttribute("fill", this.iconColor)
document.body.style.cursor = 'crosshair' document.body.style.cursor = 'crosshair'
this.chart.cursor = 'crosshair' this.chart.cursor = 'crosshair'
action(false) this.chart.activeIcon.action(false)
} }
this.chart.activeIcon = elem this.chart.activeIcon = icon
group.setAttribute("fill", this.activeIconColor) group.setAttribute("fill", this.activeIconColor)
elem.style.backgroundColor = this.activeBackgroundColor icon.elem.style.backgroundColor = this.activeBackgroundColor
document.body.style.cursor = 'crosshair' document.body.style.cursor = 'crosshair'
this.chart.cursor = 'crosshair' this.chart.cursor = 'crosshair'
action(true) this.chart.activeIcon.action(true)
return true return true
} }
}) })
return elem return icon.elem
} }
onTrendSelect(toggle, ray = false) { onTrendSelect(toggle, ray = false) {
@ -152,7 +160,7 @@ if (!window.ToolBox) {
if (!toggle) { if (!toggle) {
this.chart.chart.unsubscribeClick(this.chart.clickHandler) this.chart.chart.unsubscribeClick(this.clickHandler)
return return
} }
let crosshairHandlerTrend = (param) => { let crosshairHandlerTrend = (param) => {
@ -206,7 +214,7 @@ if (!window.ToolBox) {
}, 10); }, 10);
} }
this.chart.clickHandler = (param) => { this.clickHandler = (param) => {
if (!this.makingDrawing) { if (!this.makingDrawing) {
this.makingDrawing = true this.makingDrawing = true
trendLine.line = this.chart.chart.addLineSeries({ trendLine.line = this.chart.chart.addLineSeries({
@ -235,29 +243,29 @@ if (!window.ToolBox) {
trendLine.line.setMarkers([]) trendLine.line.setMarkers([])
this.drawings.push(trendLine) this.drawings.push(trendLine)
this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerTrend) this.chart.chart.unsubscribeCrosshairMove(crosshairHandlerTrend)
this.chart.chart.unsubscribeClick(this.chart.clickHandler) this.chart.chart.unsubscribeClick(this.clickHandler)
document.body.style.cursor = 'default' document.body.style.cursor = 'default'
this.chart.cursor = 'default' this.chart.cursor = 'default'
this.chart.activeIcon.style.backgroundColor = this.backgroundColor this.chart.activeIcon.elem.style.backgroundColor = this.backgroundColor
this.chart.activeIcon = null this.chart.activeIcon = null
} }
} }
this.chart.chart.subscribeClick(this.chart.clickHandler) this.chart.chart.subscribeClick(this.clickHandler)
} }
onHorzSelect(toggle) { clickHandlerHorz = (param) => {
let clickHandlerHorz = (param) => {
let price = this.chart.series.coordinateToPrice(param.point.y) let price = this.chart.series.coordinateToPrice(param.point.y)
let lineStyle = LightweightCharts.LineStyle.Solid let lineStyle = LightweightCharts.LineStyle.Solid
let line = new HorizontalLine(this.chart, 'toolBox', price, null, 2, lineStyle, true) let line = new HorizontalLine(this.chart, 'toolBox', price, null, 2, lineStyle, true)
this.drawings.push(line) this.drawings.push(line)
this.chart.chart.unsubscribeClick(clickHandlerHorz) this.chart.chart.unsubscribeClick(this.clickHandlerHorz)
document.body.style.cursor = 'default' document.body.style.cursor = 'default'
this.chart.cursor = 'default' this.chart.cursor = 'default'
this.chart.activeIcon.style.backgroundColor = this.backgroundColor this.chart.activeIcon.elem.style.backgroundColor = this.backgroundColor
this.chart.activeIcon = null this.chart.activeIcon = null
} }
this.chart.chart.subscribeClick(clickHandlerHorz) onHorzSelect(toggle) {
!toggle ? this.chart.chart.unsubscribeClick(this.clickHandlerHorz) : this.chart.chart.subscribeClick(this.clickHandlerHorz)
} }
onRaySelect(toggle) { onRaySelect(toggle) {

View File

@ -5,7 +5,7 @@ with open('README.md', 'r', encoding='utf-8') as f:
setup( setup(
name='lightweight_charts', name='lightweight_charts',
version='1.0.14', version='1.0.14.1',
packages=find_packages(), packages=find_packages(),
python_requires='>=3.8', python_requires='>=3.8',
install_requires=[ install_requires=[