-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-template-sync.html
More file actions
195 lines (174 loc) · 8.47 KB
/
example-template-sync.html
File metadata and controls
195 lines (174 loc) · 8.47 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Template Auto-Sync Example</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.example { margin: 20px 0; padding: 15px; border: 1px solid #ddd; }
.source-data { background: #e8f4f8; padding: 10px; margin: 10px 0; }
.rendered-area { background: #f0f8e8; padding: 10px; margin: 10px 0; }
.person-card, .product-card {
border: 1px solid #ccc;
padding: 10px;
margin: 5px 0;
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.controls { margin: 10px 0; }
button { padding: 5px 10px; margin: 0 5px; }
.highlight { background: yellow; }
.instructions { background: #f9f9f9; padding: 10px; margin: 10px 0; }
</style>
</head>
<body>
<h1>Template Auto-Synchronization Example</h1>
<div class="example">
<h2>Person Directory with Live Sync</h2>
<div class="instructions">
<p><strong>Instructions:</strong> Edit the source data below and watch the rendered cards update automatically!</p>
</div>
<h3>Source Data (Editable)</h3>
<div class="source-data">
<div id="person1" itemscope itemtype="https://schema.org/Person">
<label>Name: <input itemprop="name" value="Alice Johnson"></label><br>
<label>Email: <input itemprop="email" value="alice@example.com"></label><br>
<label>Title: <input itemprop="jobTitle" value="Software Engineer"></label>
</div>
<div id="person2" itemscope itemtype="https://schema.org/Person">
<label>Name: <input itemprop="name" value="Bob Smith"></label><br>
<label>Email: <input itemprop="email" value="bob@example.com"></label><br>
<label>Title: <input itemprop="jobTitle" value="Product Manager"></label>
</div>
</div>
<h3>Rendered Output (Auto-Synced)</h3>
<div class="rendered-area" data-contains="https://schema.org/Person">
<template itemscope itemtype="https://schema.org/Person">
<div class="person-card">
<h4 itemprop="name"></h4>
<p>Email: <a itemprop="email" href=""></a></p>
<p>Title: <span itemprop="jobTitle"></span></p>
</div>
</template>
</div>
<div class="controls">
<button onclick="addPerson()">Add New Person</button>
<button onclick="removePerson()">Remove Last Person</button>
<button onclick="updateRandomPerson()">Update Random Person</button>
</div>
</div>
<div class="example">
<h2>Product Catalog with Two-Way Sync</h2>
<div class="instructions">
<p><strong>Two-way sync:</strong> Edit either the source data OR the rendered cards - changes sync both ways!</p>
</div>
<h3>Product Source Data</h3>
<div class="source-data">
<div id="product1" itemscope itemtype="https://schema.org/Product">
<span itemprop="name">Laptop Pro</span> -
$<span itemprop="price">1299</span>
<meta itemprop="description" content="High-performance laptop">
</div>
<div id="product2" itemscope itemtype="https://schema.org/Product">
<span itemprop="name">Wireless Mouse</span> -
$<span itemprop="price">49</span>
<meta itemprop="description" content="Ergonomic wireless mouse">
</div>
</div>
<h3>Product Display (Editable)</h3>
<div class="rendered-area" data-contains="https://schema.org/Product">
<template itemscope itemtype="https://schema.org/Product">
<div class="product-card">
<h4><input itemprop="name" style="font-size: 1.2em; font-weight: bold;"></h4>
<p>Price: $<input itemprop="price" type="number" step="0.01" style="width: 80px;"></p>
<p><textarea itemprop="description" rows="2" style="width: 100%;"></textarea></p>
</div>
</template>
</div>
</div>
<div class="example">
<h2>Dynamic List with Array Properties</h2>
<div class="source-data">
<div id="team" itemscope itemtype="https://schema.org/Organization">
<h3 itemprop="name">Development Team</h3>
<div class="controls">
<button onclick="addSkill()">Add Skill</button>
</div>
<ul>
<li itemprop="knowsAbout">JavaScript</li>
<li itemprop="knowsAbout">Python</li>
<li itemprop="knowsAbout">DevOps</li>
</ul>
</div>
</div>
<h3>Rendered Skills</h3>
<div class="rendered-area" data-contains="https://schema.org/Organization">
<template itemscope itemtype="https://schema.org/Organization">
<div style="border: 1px solid #ddd; padding: 10px;">
<h4 itemprop="name"></h4>
<p>Team Skills:</p>
<div style="display: flex; flex-wrap: wrap; gap: 5px;">
<span itemprop="knowsAbout[]" style="background: #007bff; color: white; padding: 5px 10px; border-radius: 15px;"></span>
</div>
</div>
</template>
</div>
</div>
<script type="module">
import './index.mjs';
// Make functions global for button onclick
window.personCount = 2;
window.addPerson = function() {
window.personCount++;
const div = document.createElement('div');
div.id = `person${window.personCount}`;
div.setAttribute('itemscope', '');
div.setAttribute('itemtype', 'https://schema.org/Person');
div.innerHTML = `
<label>Name: <input itemprop="name" value="New Person ${window.personCount}"></label><br>
<label>Email: <input itemprop="email" value="person${window.personCount}@example.com"></label><br>
<label>Title: <input itemprop="jobTitle" value="New Employee"></label>
`;
document.querySelector('.source-data').appendChild(div);
};
window.removePerson = function() {
const lastPerson = document.querySelector('.source-data > div:last-child');
if (lastPerson && lastPerson.id.startsWith('person')) {
lastPerson.remove();
}
};
window.updateRandomPerson = function() {
const people = document.querySelectorAll('[itemtype="https://schema.org/Person"]');
if (people.length > 0) {
const random = people[Math.floor(Math.random() * people.length)];
const nameInput = random.querySelector('[itemprop="name"]');
if (nameInput) {
const newValue = `Updated ${Date.now()}`;
// Use setAttribute so MutationObserver can detect the change
nameInput.setAttribute('value', newValue);
nameInput.value = newValue; // Also update the property for display
nameInput.classList.add('highlight');
setTimeout(() => nameInput.classList.remove('highlight'), 1000);
}
}
};
window.addSkill = function() {
const team = document.getElementById('team');
const ul = team.querySelector('ul');
const li = document.createElement('li');
li.setAttribute('itemprop', 'knowsAbout');
li.textContent = prompt('Enter new skill:') || 'New Skill';
ul.appendChild(li);
};
// Demonstrate programmatic updates
setTimeout(() => {
console.log('Template sync is active. Try:');
console.log('- Editing the input fields in source data');
console.log('- Editing the rendered product cards');
console.log('- Using the buttons to add/remove items');
console.log('- Checking document.microdata to see all items');
}, 1000);
</script>
</body>
</html>