To complement Cerbrus's answer, I have to say that when we license our work to Stack Exchange on the terms of CC-by-SA license (version 4.0 at the time of this writing), the onus is on them to provide appropriate attribution, which they do (every history item, post, comment, etc is properly attributed).
However, when we copy something, the onus is on us to provide attribution where necessary. If we are working on a project it is our problem to make sure we do not violate any applicable license. You might argue that the proposed feature will help ensure the above, but as everywhere in our craft, the question is: what the tradeoffs are?
Firstly, this is bad for business given the Stack Overflow model (after all, we are not a photo stock where authors get royalties off the licensed work). If attribution is forced upon users, they will gradually move to content providers that do not do that.
Secondly, this treats users like people with profound intellectual disability (read: idiots) instead of trying to educate them about the importance of attribution and possible legal ramifications in case of commercial products and restrictive licenses in general.
Finally, the proposal assumes the code is copied from Stack Overflow only for use in projects, but that's not true: a lot of times the copied content is a terminal command (i.e. a particular GIT incantation). You are welcome to try the demo0 feature out and see how "convenient" it would be:
(() => {
const makeLinePrompt = () => {
const prompt = document.createElement("div");
prompt.classList.add("terminal-prompt");
prompt.textContent = "$";
return prompt;
};
const makeLineInput = (editable = true) => {
const input = document.createElement("div");
input.classList.add("terminal-input");
input.contentEditable = editable;
return input;
};
const makeLine = (editable = true) => {
const line = document.createElement("div");
line.classList.add("terminal-line");
line.append(
makeLinePrompt(),
makeLineInput(editable)
);
return line;
};
const clearTerminal = (terminal) => {
const prevLines = terminal.querySelectorAll(".terminal-line");
prevLines.forEach((line, idx) => {
if (idx === prevLines.length - 1) return;
line.remove();
});
};
const getCurrLine = (terminal) => terminal.querySelector(".terminal-input:focus");
const clearLine = (terminal) => {
const currLine = getCurrLine(terminal);
if (currLine) currLine.textContent = "";
};
const switchLineToReadonly = (line) => {
const prompt = line.querySelector(".terminal-prompt");
prompt && prompt.remove();
const input = line.querySelector(".terminal-input");
input.contentEditable = false;
input.innerText = input.innerText.replace(/(?:\r?\n)+$/, "");
};
document.addEventListener("keydown", (event) => {
const terminal = document.getElementById("terminal");
const {
key,
ctrlKey,
metaKey
} = event;
if (ctrlKey || metaKey) {
const ctrlMap = {
"L": clearTerminal,
"U": clearLine
};
const action = ctrlMap[key.toUpperCase()];
if (!action) return;
event.preventDefault();
action(terminal);
}
});
document.addEventListener("keyup", ({
target,
currentTarget,
key,
ctrlKey,
metaKey,
shiftKey
}) => {
if (target === currentTarget) return;
const terminal = document.getElementById("terminal");
if (key === "Enter" && !shiftKey) {
const prevLines = terminal.querySelectorAll(".terminal-line");
prevLines.forEach(switchLineToReadonly);
const line = makeLine();
terminal.append(line);
line.querySelector(".terminal-input").focus();
}
});
document.addEventListener("copy", ({
target,
currentTarget,
clipboardData,
}) => {
if (target === currentTarget || target.tagName.toUpperCase() !== "CODE") return;
const text = document.getSelection().toString().trim();
const now = new Date().toISOString();
const attributed = `${text} by Oleg Valter on ${now}, CC BY-SA 4.0`;
clipboardData.setData("text/plain", attributed);
event.preventDefault();
});
})();
* {
box-sizing: border-box;
}
code {
background-color: grey;
color: white;
border-radius: 2px;
padding: 10px;
}
code::before {
content: "$"
}
.terminal {
padding: 10px;
padding-bottom: 2px;
background-color: black;
color: white;
}
z .terminal-line {
padding: 2px;
}
.terminal-prompt,
.terminal-input {
display: inline-block;
vertical-align: top;
padding-bottom: 4px;
font-family: 'Source Code Pro', sans-serif;
line-height: 16px;
font-size: 12px;
}
.terminal-prompt {
width: 10px;
}
.terminal-input {
outline: none !important;
min-width: 1px;
}
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<code>
git rebase -i --empty=keep --keep-empty --allow-empty-message HEAD~5
</code>
<p>
Copy the command above and paste in the terminal below:
</p>
<div id="terminal" class="terminal">
<div class="terminal-line">
<div class="terminal-prompt">$</div>
<div class="terminal-input" contenteditable="true"></div>
</div>
</div>
0 I didn't give the snippet much thought, so if there are issues, let me know
https://meta.stackoverflow.com/questions/407016/should-stack-overflow-implement-automated-link-attribution-on-copy-paste#comment838739_407016