-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
156 lines (139 loc) · 6.01 KB
/
Copy pathexample.js
File metadata and controls
156 lines (139 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Initialize the engine and renderer
const engine = new BlockEngine();
const renderer = new BlockRenderer(engine, 'blocks-container');
// Status updates
function updateStatus(message) {
document.getElementById('status').textContent = message;
setTimeout(() => {
document.getElementById('status').textContent = '';
}, 3000);
}
// Create a new block
function createNewBlock() {
const content = prompt('Enter block content:');
if (content !== null) {
const block = engine.createBlock(content);
updateStatus(`Block ${block.id.substring(0, 8)}... created`);
}
}
// Link selected blocks
function linkSelectedBlocks() {
const linkType = document.getElementById('linkTypeSelect').value;
renderer.linkSelected(linkType);
updateStatus(`Blocks linked with ${linkType} connection`);
}
// Auto arrange blocks
function arrangeBlocks() {
engine.arrangeBlocks(3);
updateStatus('Blocks arranged');
}
// Set view mode
function setViewMode(mode) {
renderer.setViewMode(mode);
document.getElementById('freeViewBtn').classList.toggle('active', mode === 'free');
document.getElementById('gridViewBtn').classList.toggle('active', mode === 'grid');
updateStatus(`Switched to ${mode} view`);
}
// Search blocks
function searchBlocks() {
const query = document.getElementById('searchInput').value;
if (query) {
const results = engine.searchBlocks(query);
updateStatus(`Found ${results.length} blocks`);
// Highlight search results
document.querySelectorAll('.block').forEach(el => {
const blockId = el.dataset.blockId;
const isResult = results.some(block => block.id === blockId);
el.style.opacity = isResult ? '1' : '0.3';
});
} else {
// Reset opacity
document.querySelectorAll('.block').forEach(el => {
el.style.opacity = '1';
});
}
}
// Export data
function exportData() {
const data = engine.exportToJSON();
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `blocks_${new Date().toISOString().slice(0, 10)}.json`;
a.click();
URL.revokeObjectURL(url);
updateStatus('Data exported');
}
// Import data
function importData() {
document.getElementById('importFile').click();
}
function handleImport(event) {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
if (engine.importFromJSON(e.target.result)) {
updateStatus('Data imported successfully');
} else {
updateStatus('Import failed');
}
};
reader.readAsText(file);
}
}
// Keyboard shortcuts
document.addEventListener('keydown', (e) => {
// Ctrl/Cmd + N: New block
if ((e.ctrlKey || e.metaKey) && e.key === 'n') {
e.preventDefault();
createNewBlock();
}
// Ctrl/Cmd + L: Link selected
if ((e.ctrlKey || e.metaKey) && e.key === 'l') {
e.preventDefault();
linkSelectedBlocks();
}
// Delete: Delete selected blocks
if (e.key === 'Delete' && !e.target.contentEditable) {
const selected = renderer.getSelectedBlocks();
if (selected.length > 0 && confirm(`Delete ${selected.length} blocks?`)) {
selected.forEach(block => engine.deleteBlock(block.id));
renderer.selectedBlocks.clear();
}
}
// Escape: Clear selection
if (e.key === 'Escape') {
renderer.selectedBlocks.clear();
renderer.render();
}
});
// Create some example blocks with different link types
window.addEventListener('DOMContentLoaded', () => {
// Create sample blocks
const block1 = engine.createBlock('Project Overview\n\nThis is the main project hub with links to all components.', 'note', {x: 400, y: 100});
const block2 = engine.createBlock('Task 1: Design UI\n\nCreate mockups and wireframes for the user interface.', 'task', {x: 100, y: 300});
const block3 = engine.createBlock('Task 2: Backend API\n\nDevelop RESTful API endpoints for data management.', 'task', {x: 400, y: 300});
const block4 = engine.createBlock('Task 3: Testing\n\nWrite unit tests and integration tests.', 'task', {x: 700, y: 300});
const block5 = engine.createBlock('Resources\n\n- Documentation\n- API Reference\n- Design Guidelines', 'note', {x: 250, y: 500});
const block6 = engine.createBlock('Dependencies\n\nExternal libraries and frameworks used in the project.', 'note', {x: 550, y: 500});
// Create different types of links
engine.linkBlocks(block1.id, block2.id, 'single'); // Project → Task 1
engine.linkBlocks(block1.id, block3.id, 'single'); // Project → Task 2
engine.linkBlocks(block1.id, block4.id, 'single'); // Project → Task 3
engine.linkBlocks(block2.id, block3.id, 'single'); // Task 1 → Task 2
engine.linkBlocks(block3.id, block4.id, 'single'); // Task 2 → Task 3
engine.linkBlocks(block5.id, block1.id, 'reverse'); // Resources ← Project
engine.linkBlocks(block6.id, block3.id, 'double'); // Dependencies ↔ Task 2
updateStatus('Welcome! Double-click on arrows to edit link directions.');
// Hide help text after 10 seconds
setTimeout(() => {
const helpText = document.querySelector('.help-text');
if (helpText) {
helpText.style.opacity = '0';
helpText.style.transition = 'opacity 0.5s';
setTimeout(() => helpText.remove(), 500);
}
}, 10000);
});