сохранение и удаление элемента
This commit is contained in:
parent
4a7b3f14fc
commit
40cfa3e74c
2
dist/bundle.js
vendored
2
dist/bundle.js
vendored
File diff suppressed because one or more lines are too long
@ -54,7 +54,7 @@ export class ImageBlock extends Block {
|
||||
toHTML() {
|
||||
const { alt, styles } = this.options
|
||||
return row(
|
||||
col(`<img src="./src/assets/${this.value}" alt="${alt}" height="250">`),
|
||||
col(`<img src="${this.value}" alt="${alt}" height="250">`),
|
||||
styles
|
||||
)
|
||||
}
|
||||
@ -66,6 +66,7 @@ export class CustomBlock extends Block {
|
||||
}
|
||||
|
||||
toHTML() {
|
||||
return row(this.value)
|
||||
const { options, value } = this
|
||||
return row(col(value), options.styles)
|
||||
}
|
||||
}
|
||||
|
@ -15,16 +15,18 @@ import {
|
||||
} from './forms.js'
|
||||
|
||||
export class SideBar {
|
||||
constructor(selector, update) {
|
||||
constructor(selector, update, clear) {
|
||||
this.$el = document.querySelector(selector)
|
||||
this.update = update
|
||||
|
||||
this.clear = clear
|
||||
this.init()
|
||||
}
|
||||
|
||||
init() {
|
||||
this.$el.addEventListener('submit', this.addBlock.bind(this))
|
||||
this.$el.innerHTML = this.template
|
||||
this.addButton('Очистить', this.clear, 'btn btn-primary btn-sm')
|
||||
this.addButton('Скачать', this.download, 'btn btn-primary btn-sm')
|
||||
}
|
||||
|
||||
get template() {
|
||||
@ -37,6 +39,14 @@ export class SideBar {
|
||||
].join('')
|
||||
}
|
||||
|
||||
addButton(title, onClick, className) {
|
||||
var button = document.createElement('button')
|
||||
button.innerHTML = title
|
||||
button.onclick = onClick
|
||||
button.className = className
|
||||
this.$el.appendChild(button)
|
||||
}
|
||||
|
||||
addBlock(event) {
|
||||
event.preventDefault()
|
||||
|
||||
@ -57,7 +67,7 @@ export class SideBar {
|
||||
newBlock = new TextBlock(value, { styles })
|
||||
break
|
||||
case 'textColumns':
|
||||
newBlock = new TextColumnsBlock(value.split(','), { styles })
|
||||
newBlock = new TextColumnsBlock(value.split('\n\n'), { styles })
|
||||
break
|
||||
case 'image':
|
||||
newBlock = new ImageBlock(value, { styles, alt })
|
||||
@ -72,4 +82,42 @@ export class SideBar {
|
||||
event.target.value.value = ''
|
||||
event.target.styles.value = ''
|
||||
}
|
||||
|
||||
download() {
|
||||
let site = document.querySelector('#site').innerHTML
|
||||
let template = `
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<link
|
||||
rel="stylesheet"
|
||||
href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
|
||||
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2"
|
||||
crossorigin="anonymous"
|
||||
/>
|
||||
<title>JavaScript Constructor</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app" class="container-fluid">
|
||||
<div id="site" class="content">${site}</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
var element = document.createElement('a')
|
||||
element.setAttribute(
|
||||
'href',
|
||||
'data:text/plain;charset=utf-8,' + encodeURIComponent(template)
|
||||
)
|
||||
element.setAttribute('download', 'index.html')
|
||||
|
||||
element.style.display = 'none'
|
||||
document.body.appendChild(element)
|
||||
|
||||
element.click()
|
||||
|
||||
document.body.removeChild(element)
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
export class Site {
|
||||
constructor(selector) {
|
||||
constructor(selector, remove) {
|
||||
this.$el = document.querySelector(selector)
|
||||
this.$el.addEventListener('click', this.removeBlock.bind(this))
|
||||
this.$el.addEventListener('dblclick', this.removeBlock.bind(this))
|
||||
this.remove = remove
|
||||
}
|
||||
|
||||
removeBlock(event) {
|
||||
event.preventDefault()
|
||||
|
||||
console.log(event.target)
|
||||
this.remove(event.target.innerHTML || event.target.alt)
|
||||
}
|
||||
|
||||
render(model) {
|
||||
|
16
src/index.js
16
src/index.js
@ -3,12 +3,22 @@ import { model } from './model.js'
|
||||
import { Site } from './classes/site.js'
|
||||
import { SideBar } from './classes/sidebar.js'
|
||||
|
||||
const site = new Site('#site')
|
||||
|
||||
const updateCallback = (newBlock) => {
|
||||
model.push(newBlock)
|
||||
site.render(model)
|
||||
}
|
||||
|
||||
const clearCallback = () => {
|
||||
model = []
|
||||
site.render(model)
|
||||
}
|
||||
|
||||
const removeCallback = (value) => {
|
||||
model = model.filter((item) => !item.toHTML().includes(value))
|
||||
site.render(model)
|
||||
}
|
||||
|
||||
const site = new Site('#site', removeCallback)
|
||||
|
||||
site.render(model)
|
||||
new SideBar('#panel', updateCallback)
|
||||
new SideBar('#panel', updateCallback, clearCallback)
|
||||
|
20
src/model.js
20
src/model.js
@ -7,13 +7,16 @@ import {
|
||||
} from './classes/blocks.js'
|
||||
import { css } from './utils'
|
||||
|
||||
export const model = [
|
||||
new CustomBlock(`<div id="leaves">
|
||||
export let model = [
|
||||
new CustomBlock(
|
||||
`<div id="leaves">
|
||||
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
|
||||
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
|
||||
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
|
||||
<i></i><i></i><i></i><i></i><i></i><i></i>
|
||||
</div>`),
|
||||
</div>`,
|
||||
{ styles: css({ margin: 0 }) }
|
||||
),
|
||||
new TitleBlock('Алексей Плещеев', {
|
||||
styles: css({
|
||||
color: '#fff',
|
||||
@ -36,10 +39,13 @@ export const model = [
|
||||
'padding-top': '20px'
|
||||
})
|
||||
}),
|
||||
new ImageBlock('golden_autumn.jpg', {
|
||||
styles: css({ 'text-align': 'center', 'padding-top': '20px' }),
|
||||
alt: 'Иван Иванович Шишкин – Ранняя осень 1889'
|
||||
}),
|
||||
new ImageBlock(
|
||||
'https://upload.wikimedia.org/wikipedia/commons/f/fb/%D0%97%D0%BE%D0%BB%D0%BE%D1%82%D0%B0%D1%8F_%D0%BE%D1%81%D0%B5%D0%BD%D1%8C_%28%D0%A8%D0%B8%D1%88%D0%BA%D0%B8%D0%BD%29.jpg',
|
||||
{
|
||||
styles: css({ 'text-align': 'center', 'padding-top': '20px' }),
|
||||
alt: 'Иван Иванович Шишкин – Ранняя осень 1889'
|
||||
}
|
||||
),
|
||||
new TextColumnsBlock(
|
||||
[
|
||||
` Осень наступила,
|
||||
|
@ -28,6 +28,10 @@
|
||||
margin-left: 30px;
|
||||
}
|
||||
|
||||
button {
|
||||
margin: 2px;
|
||||
}
|
||||
|
||||
input:invalid {
|
||||
border: 0.5px solid skyblue;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user