- let canvasInstance;
- function registerCanvasInstance(dotnetHelper) {
- canvasInstance = dotnetHelper;
- }
- async function updateItemPosition(id, x, y) {
- await canvasInstance.invokeMethodAsync('UpdateItemPosition', id, x, y)
- }
- document.addEventListener('DOMContentLoaded', function () {
- let currentMode = 'move';
- const colors = ['red', 'blue', 'green', 'yellow'];
- let colorIndex = 0;
- window.addNote = (noteId, position) => {
- const canvas = document.getElementById('canvas');
- const note = document.createElement('div');
- note.id = noteId;
- note.classList.add('note', 'draggable');
- note.textContent = noteId;
- note.style.top = `${position.y}px`;
- note.style.left = `${position.x}px`;
- canvas.appendChild(note);
- interact(note).draggable({
- modifiers: [
- interact.modifiers.restrictRect({
- restriction: 'parent',
- endOnly: true
- })
- ],
- listeners: {
- start(event) {
- if (currentMode === 'move') {
- event.target.classList.add('dragging');
- }
- },
- move(event) {
- if (currentMode === 'move') {
- const target = event.target;
- const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
- const y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
- target.style.transform = `translate(${x}px, ${y}px)`;
- target.setAttribute('data-x', x);
- target.setAttribute('data-y', y);
- canvasInstance.invokeMethodAsync("UpdateItemPosition", target.id, x, y);
- }
- },
- end(event) {
- if (currentMode === 'move') {
- event.target.classList.remove('dragging');
- checkOverlap(event.target);
- }
- }
- }
- });
- note.addEventListener('click', () => {
- if (currentMode === 'color') {
- note.style.backgroundColor = colors[colorIndex];
- colorIndex = (colorIndex + 1) % colors.length;
- } else if (currentMode === 'delete') {
- note.remove();
- }
- });
- };
- window.setMode = (mode) => {
- currentMode = mode;
- };
- function checkOverlap(note) {
- const notes = document.querySelectorAll('.note');
- notes.forEach(otherNote => {
- if (note !== otherNote && isOverlapping(note, otherNote)) {
- resolveOverlap(note, otherNote);
- }
- });
- }
- function isOverlapping(note1, note2) {
- const rect1 = note1.getBoundingClientRect();
- const rect2 = note2.getBoundingClientRect();
- return (Math.abs(rect1.right - rect2.right) < 20 &&
- Math.abs(rect1.top - rect2.top) < 20);
- }
- function resolveOverlap(note, otherNote) {
- const rect1 = note.getBoundingClientRect();
- const rect2 = otherNote.getBoundingClientRect();
- const deltaX = rect1.left - rect2.left;
- const deltaY = rect1.top - rect2.top;
- if (Math.abs(deltaX) < Math.abs(deltaY)) {
- // Przesuń w poziomie
- if (deltaX > 0) {
- note.style.transform = `translate(${parseFloat(note.getAttribute('data-x')) + 10}px, ${parseFloat(note.getAttribute('data-y'))}px)`;
- } else {
- note.style.transform = `translate(${parseFloat(note.getAttribute('data-x')) - 10}px, ${parseFloat(note.getAttribute('data-y'))}px)`;
- }
- } else {
- // Przesuń w pionie
- if (deltaY > 0) {
- note.style.transform = `translate(${parseFloat(note.getAttribute('data-x'))}px, ${parseFloat(note.getAttribute('data-y')) + 10}px)`;
- } else {
- note.style.transform = `translate(${parseFloat(note.getAttribute('data-x'))}px, ${parseFloat(note.getAttribute('data-y')) - 10}px)`;
- }
- }
- note.setAttribute('data-x', parseFloat(note.getAttribute('data-x')) + (deltaX > 0 ? 10 : -10));
- note.setAttribute('data-y', parseFloat(note.getAttribute('data-y')) + (deltaY > 0 ? 10 : -10));
- }
- });
- @page "/dashboard"
- @using System.Diagnostics
- @using Microsoft.JSInterop;
- @inject IJSRuntime JS
- @rendermode InteractiveServer
- <PageTitle>Dashboard</PageTitle>
- <h3>Dashboard</h3>
- <div>
- <button class="btn btn-primary" @onclick="AddNote">Dodaj kartkę</button>
- <button class="btn btn-primary" @onclick="@( () => SetMode("move") )">Tryb przesuwania</button>
- <button class="btn btn-primary" @onclick="@( () => SetMode("color") )">Tryb zmiany koloru</button>
- <button class="btn btn-primary" @onclick="@( () => SetMode("delete") )">Tryb usuwania</button>
- </div>
- <div id="canvas" class="canvas"></div>
- @code {
- private int _noteCounter = 0;
- private int _offsetX = 0;
- private int _offsetY = 0;
- private List<Card> Cards = new List<Card>();
- private async Task AddNote()
- {
- _noteCounter++;
- var noteId = $"note-{_noteCounter}";
- var position = new { X = _offsetX, Y = _offsetY };
- _offsetX += 20;
- _offsetY += 20;
- Cards.Add(new Card(){Id = noteId, X = _offsetX, Y = _offsetY});
- await JS.InvokeVoidAsync("addNote", noteId, position);
- }
- private async Task SetMode(string mode)
- {
- await JS.InvokeVoidAsync("setMode", mode);
- }
- [JSInvokableAttribute("UpdateItemPosition")]
- public void UpdateItemPosition(string id, int x, int y)
- {
- Debug.Print($"id: {id}, x: {x}, y: {y}");
- var item = Cards.FirstOrDefault(i => i.Id == id);
- item.X = x;
- item.Y = y;
- StateHasChanged();
- }
- public class Card
- {
- public string Id { get; set; }
- public int X { get; set; }
- public int Y { get; set; }
- public bool IsMoved { get; set; }
- }
- }
[text] BoardCode
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: